cocos2d 中掉落的精灵
我有 2 个水平移动的精灵,当它们撞到墙上时,我需要它们自然掉落。 我知道如何以一种简单的方式来改变每一帧的位置,但这似乎并不自然,而且可能不是正确的方式。
当考虑到他们的速度和y轴时,有没有办法发现碰撞然后让他们掉下来?
我只是找不到一种简单的方法来使用 box2d 或chipmunk 来做到这一点。
任何方向都会很棒。 谢谢。
i have 2 sprites that moving horizontly and i need them to fall down naturally ,when they hit a wall .
i know how to do that in a simple way of changing position every frame, but this is not seems natural, and probably not the right way.
There is a way to discover the collision and then make them fall down, when thier speed and y axis is taking into consideration ?
i just couldnt find a simple way to do that with box2d or chipmunk .
any direction would be great.
thanx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先将自定义联系人侦听器添加到您的 b2World。
这依赖于您将所有主体的用户数据设置为保存它的类。即雪碧、墙。您也可以以相反的方式进行操作,即始终施加重力并施加相反的力。然后,当您与墙壁接触时,您将停止施加其他力,重力就会生效。
或者像 @iforce2d 所说的那样,只需检查精灵主体的线速度何时接近或等于零,因为这表明它已经击中了阻止它的物体,而不仅仅是墙壁,然后您可以将 hit 设置为真的。 Step 函数将完成剩下的工作。但这是有限的,因为它只需要减速来触发它而不是撞到墙上,这可能是不希望的。
Firstly add a custom contact listener to your b2World.
This relies on you setting the userdata of all the bodies you have as the class that holds it. i.e. Sprite, Wall. You could also do it the opposite way where you apply gravity all the time and also apply an opposing force. Then when you make contact with the wall you stop applying the other force and gravity takes effect.
Or like how @iforce2d puts it just check when the linearvelocity of the body of the sprite is close or equal to zero because that would indicate it has hit something that has stopped it, not just a wall though, then you can just set hit to true. And the step function will do the rest. This is limited though because it just needs it to slow down to trigger it not hit a wall which may not be desired.
使用任何物理引擎只需设置世界中的正确重力(在 box2D 中为 b2World)并设置身体的初始速度。所以自然会掉下来
using any phys engine just set's the correct gravity in the world (b2World in case of box2D) and set the initial velocity of your body. So it will fall naturally
如果您自己设置速度,则由您决定使其看起来自然:)
改变速度的垂直部分不是一个好主意 - 保持原样即可:
If you are setting the velocity yourself, it's up to you to make it look natural :)
Changing the vertical part of the velocity is not a good idea - just leave it how it was: