简体   繁体   English

iPhone cocos2d box2d身体碰撞检测无需施加力

[英]iPhone cocos2d box2d body collision detection without applying force

I am writing Cocos2D box2d game for iPhone. 我正在为iPhone编写Cocos2D box2d游戏。 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. 我有2个动态物体,希望它们从外部施加力,但不要互相施加力并检测到它们的碰撞。 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 这个答案有一些代码可以动态更改物体的碰撞过滤指数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 从手册中: 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. 并使用ContactListener来检测碰撞。 Box2d for collision detection Box2D的碰撞检测

Also you can use box2d filters. 您也可以使用box2d过滤器。 For example: 例如:

REMEMBER: if groupIndex < 0, same bodies never collide with each other. 记住:如果groupIndex <0,则相同的物体永远不会相互碰撞。 That is what you need. 那就是你所需要的。

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



 fixtureDef.filter = bodyFilter;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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