防止box2d玩家在半空中压向墙壁

发布于 2024-12-17 09:19:34 字数 141 浏览 1 评论 0原文

我已经设置了一个带有键盘控制播放器的 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?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡忘如思 2024-12-24 09:19:35

将玩家身体作为一组身体(在左侧和右侧添加固定装置)?并将摩擦力设置为0
在此处输入图像描述

Make player body as group of bodies (add fixtures at left and right sides)? and set friction to 0
enter image description here

冬天的雪花 2024-12-24 09:19:35

您需要改变影响角色身体的方式。你用什么方法让他左/右移动?尝试在身体上应用 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.

羅雙樹 2024-12-24 09:19:35

您可以将播放器的摩擦力设置为 0,然后在每个循环中执行一个函数,当用户未按下移动键时,将 X 中的速度设置为零。在检查事件的方法中,您应该添加:

if (event.type == KeyReleased) {
    if (!isKeyPressed(Keyboard::Left) && !isKeyPressed(Keyboard::Right)) {
        player.stop();
     }
}

在您的player.stop()中,您应该执行类似的操作:

b2Vec2 vel = body->GetLinearVelocity();
vel.x = 0;
body->SetLinearVelocity(vel);

这将使您的玩家与滑冰平台的摩擦力为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:

if (event.type == KeyReleased) {
    if (!isKeyPressed(Keyboard::Left) && !isKeyPressed(Keyboard::Right)) {
        player.stop();
     }
}

And in your player.stop() you should do something like:

b2Vec2 vel = body->GetLinearVelocity();
vel.x = 0;
body->SetLinearVelocity(vel);

This will make your player has friction 0 with platforms with the iceskating

九公里浅绿 2024-12-24 09:19:35

这一切都取决于你的物理学目前的运作方式,因为有很多可能性。在我看来,最简单的方法是将所示的身体分成 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文