简体   繁体   中英

iPhone cocos2d box2d body collision detection without applying force

I am writing Cocos2D box2d game for iPhone. I've 2 dynamic bodies, and I hope they are applied force from outside, but they don't apply force each other and detect their collision. How can I achieve this?

And also I hope they move together at the same position after collision. How can I do this?

they don't apply force each other and detect their collision

Sounds like you might want to look at collision filtering. This answer has a bit of code that changes the collision filtering index of a body dynamically https://stackoverflow.com/a/11283206/735204

they move together at the same position after collision

Probably some kind of joint (eg weldjoint?)

From the manual: http://www.box2d.org/manual.html

Joints are used to constrain bodies to the world or to each other. Typical examples in games include ragdolls, teeters, and pulleys. Joints can be combined in many different ways to create interesting motions.

Some joints provide limits so you can control the range of motion. Some joint provide motors which can be used to drive the joint at a prescribed speed until a prescribed force/torque is exceeded.

Joint motors can be used in many ways. You can use motors to control position by specifying a joint velocity that is proportional to the difference between the actual and desired position. You can also use motors to simulate joint friction: set the joint velocity to zero and provide a small, but significant maximum motor force/torque. Then the motor will attempt to keep the joint from moving until the load becomes too strong.

Sorry about last answer, just checking that I can write it.

What about this?

bodyDef.isSensor = true;

and use ContactListener to detect collision. Box2d for collision detection

Also you can use box2d filters. For example:

REMEMBER: if groupIndex < 0, same bodies never collide with each other. That is what you need.

b2Filter bodyFilter;
 bodyFilter.groupIndex = -1; 
 bodyFilter.categoryBits = 0x0002;



 fixtureDef.filter = bodyFilter;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM