box2D flash:选择性恢复
我正在尝试做一个排球比赛并基于box2d模拟每个动作。我想做的是当球员击球和球撞到墙上时有不同的球恢复。因此,在第一种情况下,球应该飞得更快,而在第二种情况下,球应该飞得更慢。
但是,如果我尝试以不同的方式为玩家和墙壁对象设置恢复,我也会注意到玩家本身从墙上弹起......有没有办法以选择性的方式做到这一点?例如,击中地板不应导致玩家弹跳......但如果玩家击中球,它应该弹跳很多。
I am trying to do a volleyball game and simulating every action based on box2d. What I want to do is to have different ball restitution when player hit the ball, and ball hit the wall. So in first case ball should fly quicker, and in second case slower.
But if I am trying to set restitution for a player and for wall objects in a different way I also notice that player itself is bouncing from the wall... Is there a way I can do it in selective way? E.g. hitting floor should not cause a player to bounce.... But if player hit the ball it should bounce a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,你是对的,Box2D 没有这个设置。
相反,您可以做的是监听与玩家和球匹配的接触侦听器事件。当这种情况发生时,你会对球施加很大的力。
编辑
这是我凭空想出来的,我不确定它是否完全正确。
这是怎么回事。
您需要创建此类的实例并将其注册到世界,这可能类似于
world.SetContactListener(new MyContactListener)
或其他内容。当两个物体接触时,Add() 方法将被触发。施加的力沿接触法线方向(将您从一个物体带到另一个物体)。
由于接触侦听器系统的设置方式,球或玩家有可能成为 b2ContactPoint 结构中的主体 #1,因此您需要针对这种可能性进行编码。法线的方向取决于哪一个是主体#1(因此有负号)。我实际上不记得它的方向,所以你可能需要反转力(将减号放在另一个分支中)。
除此之外,它应该相当清楚。 Box2D 在这里似乎并不那么出名,因此您可能在 Box2D 论坛上运气好一点(http://www .box2d.org/forum/)
Unfortunately, you're right, Box2D hasn't got this setting.
What you can do instead is listen for a contact listener event which matches both the player and the ball. When that happens you apply a very large force to the ball.
edit
I typed this off the top of my head, I'm not sure it's exactly right.
What's going on here.
You need to create an instance of this class and register it with the world which is probably something like
world.SetContactListener(new MyContactListener)
or something.The Add() method fires when two bodies come into contact. The force applied is in the direction of the contact normal (which takes you from one body to the other).
Because of the way the contact listener system is set up, it's possible for either the ball or the player to be body #1 in the b2ContactPoint structure, so you need to code for that possibility. The direction of the normal depends on which one is body #1 (hence the minus sign). I can't actually remember which way it goes so you might need to reverse the force (put the minus sign in the other branch).
Other than that it should be reasonably clear. Box2D doesn't seem that well known here so you might have a bit more luck at the Box2D forums (http://www.box2d.org/forum/)