Swift-按角度设置物理体速度 [英] Swift-Setting a physics body velocity by angle

查看:31
本文介绍了Swift-按角度设置物理体速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能让 SKNode 朝着特定方向前进,但只有一个因素.我知道施加脉冲和设置物理体的速度,但它们都由两个因素决定;dx 和 dy.我也知道使用 SKActions 旋转到一个角度.但是,一旦设置了一个角度,是否有可能使对象简单地向前移动"?或者只用一个因素来设置它的速度?

I was wondering if it was at all possible to make an SKNode move forward in a particular direction, but with only one factor. I'm aware of both applying an impulse and setting the velocity of a physics body, but they're both determined by two factors; dx and dy. I also know of rotating to an angle with SKActions. But is it possible to make an object simply "move forward" once it has been set on an angle? Or set its velocity with just one factor?

提前致谢.

推荐答案

是的,就是您问题的答案.

Yes, is the answer to your question.

我认为你在寻找什么 = 推力......对吗?

What I think you're looking for = THRUST... right?

您想要的是船"能够向任何方向旋转,并正确施加推力,从船的屁股"向外移动,以船的方式向前移动.

What you want is for the "ship" to be able to rotate in any direction, and the thrust to be applied correctly, out of the "arse" of the ship, moving it forward, in ship terms.

这绝对有可能,但确实需要一些虚拟"技巧.

This is absolutely possible, but does require a little "dummy" trick.

但我让你感到困惑.

SKPhysicsBody 的局部空间是相对于其父级的.我相信.

The local space of a SKPhysicsBody is relative to its parent. I presume.

还有投机部分.我正在猜测.这个我没试过.

And there's the speculative part. I'm guessing. I haven't tried this.

但是...大多数 PhysicsBody 都是以场景为父级的 SKNode 的孩子.

But... most physicsBodys are the child of an SKNode that's parented to the scene.

如果您为了旋转而将您的飞船作为虚拟节点的父节点,然后旋转虚拟节点,您应该能够让您的飞船在不改变推力矢量的情况下绕圈飞行,只需简单地旋转虚拟节点即可.

If you parent your ship to a dummy node for the purposes of rotation, and then rotate the dummy node, you should be able to make your spaceship fly in circles without ever changing the thrust vector, by simply rotating the dummy node.

像这个可怕的伪代码这样的东西可能有助于开始......也许.

Something like this horrible pseudo code might help to start... maybe.

let dummy = SKNode()
let ship = SKSPriteNode()
dummy.addchild(ship)

ship.Physicsbody(add how you want here...)
ship.PhysicsBody.applyForce (vector that's only X, for example)

rotate dummy with action over time...

这篇关于Swift-按角度设置物理体速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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