相对于另一动态体移动一个动态体 [英] move one dynamic body relatively to other dynamic body

查看:18
本文介绍了相对于另一动态体移动一个动态体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些我找不到解决方案的问题.我有摆动平台(body1)和英雄(body2)站在上面.平台正在摆动,因此英雄在平台上左右移动.

I faced with some problem which I can't find solution for. I have swinging platform (body1) and hero (body2) standing on it. Platform is swinging so hero moving right and left on the platform.

一开始我希望英雄只是站在平台上而不改变它相对于平台的位置.我通过添加 SKPhysicsJointFixed 加入平台和英雄来实现这一点.

In the beginning I want hero to just stand on the platform without changing it's position relatively to platform. I achieved that by adding SKPhysicsJointFixed joining the platform and the hero.

然后在游戏中的某个时刻,我希望英雄沿着平台运行,这意味着 Y 相对于平台应该保持不变,而 X 应该增长.我找到的唯一解决方案是使英雄身体静态并更改其在 didSimulatePhysics 中的位置.这种方法的问题是位置没有准确地改变.与平台位置相比,英雄位置滞后一帧.

Then in some moment in the game I want hero to run along the platform which means that Y should remain the same relatively to platform and X should grow. The only solution that I found is to make hero body static and change it's position in the didSimulatePhysics. The problem with that approach is that position not changing accurately. Hero position lags to one frame in comparison with platform position.

英雄到达平台的右端后,他应该停止移动,再次站在平台上.所以我让英雄身体静止并再次添加固定关节.并且因为英雄位置随着滞后英雄位置相对于平台的变化而变化是不正确的.有时它会在平台表面下方停止一些像素,有时会在表面上方停止一些像素.

After hero reaches the right end of the platform he should stop moving and again just stand on the platform. So I make hero body static and add fixed joint again. And because hero position changes with lags hero position relative to platform is not correct. Sometimes it stops some pixels below the surface of the platform and sometimes some pixels above the surface.

那么有没有其他方法可以让英雄沿着平台移动,英雄身体应该一直保持动态.

So is there some other approach to move the hero along the platform, hero body should remain dynamic all the time.

推荐答案

我有一个移动浮动平台的游戏.获得正确的相对速度的最简单和最动态的方法是简单地调整速度.这是我为实现这一目标所做的简化版本.

I have a game with moving floating platforms. The simplest and most dynamic approach to achieve correct relative velocity is to simply make adjustments to the velocity. Here is a simplified version of what I do to achieve this.

在我的游戏循环中,我有以下内容:

In my game loop I have the following:

if heroOnBridge {
    hero.extraMotion = floatingBridge.physicsBody!.velocity
} else {
    hero.extraMotion = CGVector(dx:0,dy:0)
}

在这里我所做的是不断查看我的英雄是否在移动的桥上,如果是,那么我将我的英雄的额外运动"属性设置为我所在的桥的速度.如果我不在桥上,那么我不会增加任何额外的速度.请注意,我没有展示 heroOnBridge 是如何计算的,因为它实际上取决于您如何定义您是否在平台上.就我而言,如果您与平台框架相交并且在地面上,我假设您在平台上.

Here what I do is constantly look to see if my hero is on a moving bridge, if so then I set my hero's "extra motion" property to the velocity of the bridge that I am on. If I am not on a bridge then I don't add on any extra velocity. Note that I don't show how heroOnBridge is calculated because it really depends on how you define whether you are on a platform. In my case I assume you are on a platform if you intersect the platform frame and are on the ground.

然后在我根据他的方向(左、右、跳跃等)计算我的英雄速度的区域中,我只需将 extraMotion 添加到英雄的速度.

Then in the area where I calculate my hero's velocity depending on his direction (left, right, jumping etc), I simply add on the extraMotion to the hero's velocity.

如果您还没有,我建议您在游戏循环中设置一个区域来处理角色的动作.它将允许您根据某些因素(在空中、跳跃、向左/向右移动、在平台上等)更改英雄的速度.

If you don't already I suggest you have an area in the game loop for processing the motion of you characters. It will allow you to make changes to the velocity of your hero depending on certain factors (in the air, jumping, moving left/right, on a platform, etc.)

如果您在计算速度部分时需要更多帮助,请告诉我,我会发布更多代码.您可能还想在此处阅读我的回答,以便顺利动态地设置速度.

If you need more help with calculating the velocity part let me know and I will post more of my code. You may also want to read my answer here for smoothly and dynamically setting the velocity.

请记住,在大多数情况下,您永远不想直接设置任何东西的位置,请始终尝试设置必要的速度以在一段时间内达到该位置.这可以防止非常紧张和不可预测的运动.

Remember, for the most part you never want to set the position of anything directly, always try to set the necessary velocity to achieve that position over some period of time. This prevents very jittery and unpredictable motion.

这篇关于相对于另一动态体移动一个动态体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆