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

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

问题描述

我遇到了一些我无法找到解决方案的问题。我有摆动平台(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天全站免登陆