applyForce(0, 400) - SpriteKit 不一致 [英] applyForce(0, 400) - SpriteKit inconsistency

查看:19
本文介绍了applyForce(0, 400) - SpriteKit 不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个物体,它有一个physicsBody 并且重力会影响它.它也是动态的.

So I have an object that has a physicsBody and gravity affects it. It is also dynamic.

目前,当用户触摸屏幕时,我运行代码:

Currently, when the users touches the screen, I run the code:

applyForce(0, 400)

applyForce(0, 400)

物体向上移动大约 200 米,然后由于重力而落下.这只会在某些时候发生.其他时候,它会导致对象仅在 Y 方向移动 50ish 个单位.

The object moves up about 200 and then falls back down due to gravity. This only happens some of the time. Other times, it results in the object only moving 50ish units in the Y direction.

我找不到模式...我将我的项目放在 Dropbox 上,这样如果有人愿意查看它就可以打开它.

I can't find a pattern... I put my project on dropbox so it can be opened if anyone is willing to look at it.

https://www.dropbox.com/sh/z0nt79pd0l5psfg/bJTbaS2JpY

这似乎发生在玩家在撞击后稍微从地面弹起片刻时.有没有办法让玩家完全不反弹?

It seems this happens when the player is bouncing off of the ground slightly for a moment after impact. Is there a way I can make it so the player doesn't bounce at all?

编辑 2:我尝试使用摩擦参数来解决这个问题,并且只允许玩家在摩擦 = 0 时跳跃"(你会认为这将是玩家在空中的所有情况)但摩擦似乎是始终大于 0.我还能如何检测玩家是否正在触摸物体(除了使用 y 位置)?

EDIT 2: I tried to solve this using the friction parameter and only allowing the player to "jump" when the friction was = 0 (you would think this would be all cases where the player was airborne) but friction appears to be greater than 0 at all times. How else might I detect if the player is touching an object (other than by using the y location)?

谢谢

推荐答案

建议解决方案

如果您尝试实现跳转功能,我建议您查看 applyImpulse 而不是 applyForce.这是两者之间的区别,如 Sprite Kit编程指南:

If you're trying to implement a jump feature, I suggest you look at applyImpulse instead of applyForce. Here's the difference between the two, as described in the Sprite Kit Programming Guide:

您可以选择施加力或脉冲:

You can choose to apply either a force or an impulse:

根据施加力和处理模拟的下一帧之间经过的模拟时间量,施加力的时间长度.因此,要对物体施加连续的力,您需要在每次处理新帧时进行适当的方法调用.力通常用于连续效果.

A force is applied for a length of time based on the amount of simulation time that passes between when you apply the force and when the next frame of the simulation is processed. So, to apply a continuous force to an body, you need to make the appropriate method calls each time a new frame is processed. Forces are usually used for continuous effects.

脉冲会瞬时改变身体的速度,而与经过的模拟时间无关.脉冲通常用于立即改变身体的速度.

An impulse makes an instantaneous change to the body’s velocity that is independent of the amount of simulation time that has passed. Impulses are usually used for immediate changes to a body’s velocity.

跳跃实际上是身体速度的瞬时变化,这意味着您应该施加冲动而不是力.要使用 applyImpulse: 方法,请计算出所需的瞬时速度变化,乘以身体的质量,并将其用作函数中的 impulse 参数.我想你会看到更好的结果.

A jump is really an instantaneous change to a body's velocity, meaning that you should apply an impulse instead of a force. To use the applyImpulse: method, figure out the desired instantaneous change in velocity, multiply by the body's mass, and use that as the impulse parameter into the function. I think you'll see better results.

意外行为的解释

如果您在 update: 函数之外调用 applyForce:,则发生的情况是,您的力正乘以应用之间经过的时间量力以及何时处理模拟的下一帧.这个乘数不是一个常数,因此每次以这种方式调用 applyForce: 时,您都会看到不同的速度变化.

If you're calling applyForce: outside of your update: function, what's happening is that your force is being multiplied by the amount of time passed between when you apply the force and when the next frame of the simulation is processed. This multiplier is not a constant, so you're seeing a different change in velocity every time you call applyForce: in this manner.

这篇关于applyForce(0, 400) - SpriteKit 不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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