防止box2d玩家在半空中压向墙壁
我已经设置了一个带有键盘控制播放器的 box2d 世界。玩家可以行走和跳跃。如何防止玩家在跳跃和按方向键朝某个物体时“粘”在墙上?
I have setup a box2d world with a keyboard controlled player. The player can walk and jump. How do I prevent the player from "sticking" to walls while jumping and pressing the directional key towards an object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将玩家身体作为一组身体(在左侧和右侧添加固定装置)?并将摩擦力设置为0
Make player body as group of bodies (add fixtures at left and right sides)? and set friction to 0
您需要改变影响角色身体的方式。你用什么方法让他左/右移动?尝试在身体上应用 applyLinearImpulse,并确保身体定义中的摩擦力不是很高。
You need to change the way that you're affecting the body of the character. What method are you using to move him left/right? Try applyLinearImpulse on the body and also ensure that your friction in the body definition isn't really high.
您可以将播放器的摩擦力设置为 0,然后在每个循环中执行一个函数,当用户未按下移动键时,将 X 中的速度设置为零。在检查事件的方法中,您应该添加:
在您的player.stop()中,您应该执行类似的操作:
这将使您的玩家与滑冰平台的摩擦力为0
You can set the friction of your player to 0 and then execute in every loop a function that set your velocity in X to zero when user is not pressing the movement keys. In your method to check events you should add:
And in your player.stop() you should do something like:
This will make your player has friction 0 with platforms with the iceskating
这一切都取决于你的物理学目前的运作方式,因为有很多可能性。在我看来,最简单的方法是将所示的身体分成 4 个边界墙(上、左、右、下),并且只让可行走的地板(顶部)产生摩擦。
It all depends on how your physics is currently working, as there are a lot of possibilities. The simplest way to do it in my opinion, is to split the shown body into 4 boundary walls (top, left, right, bottom) and only have the walk-able floor (top) produce friction.