0

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?

ttotto
  • 826
  • 3
  • 13
  • 31
  • Im not sure what you are asking. You should probably consider rewriting this or adding more content to the question. – Andres C Aug 17 '12 at 15:57

2 Answers2

0

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.

Community
  • 1
  • 1
Emmett Butler
  • 5,969
  • 2
  • 29
  • 47
  • but I already know them. 1. If I use different category bits and mask bits, how can I detect their collision? 2. I already tried to use distance joint, but sometimes sprite's position is difference from it's body's position. So they are separated sometimes. – ttotto Aug 17 '12 at 18:10
  • oh - for the detection of a collision without having the bodies react to the collision, use a sensor http://www.iforce2d.net/b2dtut/sensors – Emmett Butler Aug 17 '12 at 19:05
  • are you purposefully causing the sprite position and body position to be different? – Emmett Butler Aug 17 '12 at 20:17
0

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;
flowmachine1
  • 111
  • 2
  • 7