1

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?

Thanks in advance.

2 Answers2

1

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.

But I'm confusing you.

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.

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.

Theoretically.

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...
Confused
  • 6,048
  • 6
  • 34
  • 75
  • Thank you very much for your response, it certainly sounds like you're closer to what I'm looking for. Unfortunately, after trying and trying, I cannot figure out for the life of me what you mean by most of this. I'm relatively new to Xcode and Swift, so I would be very appreciative if you (or someone else who understands your idea) could provide some example code or further explanation. Thanks again though. – Dan Harward Jones Jan 19 '17 at 19:41
  • Woah if this works, you're the man. Brilliant idea. Thank you – Mugs Jun 11 '20 at 18:14
0

Sure I think what you're talking about is something like this:

Now let's say you have an SKSpriteNode that is called player who eventually has a physicsBody setup.

var player: SKSpriteNode!

You can just set the dx property of their velocity, so lets say you wanted to move them horizontally towards the location where the user tapped on the right hand side of the screen. If you then detect the position of the touch with touchesBegan(_:)

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else { return }
    // Replace with name of node to detect touch
    let touchLocation = touch.location(in: <NAME_OF_NODE_PROPERTY>)

    // Verify it's in front of the player and not behind
    if (touchLocation.x - playerPosition.x) > 0 {
        movePlayerVertically(toward: touchLocation)
    }
}

func movePlayerVertically(toward location: CGPoint) {

    let dx:CGFloat = location.x - player.position.x
    player.physicsBody!.velocity.dx = dx

}

EDIT: -

Since you said you just want to be able to move your player horizontally without knowing the destination, you could do something like this, this is just moving the player forward on the x-axis by 50pts every second, and will repeat it forever. Obviously you would want to tweak it to your liking.

let move = SKAction.moveBy(x: 50, y: 0, duration: 1)
let repeatAction = SKAction.repeatForever(move)
player.run(repeatAction)
Pierce
  • 3,148
  • 16
  • 38
  • Thank you for your response, but that's not quite what I'm looking for. I need a way to set an SKSpriteNode in motion without knowing its destination coordinates. It also ideally needs to be able to move in any angle from 0-359 degrees. It may well not be possible, but hopefully there's a feature I haven't heard of before that enables this. – Dan Harward Jones Jan 15 '17 at 03:16
  • I think what you're looking for is just a simple move action – Pierce Jan 15 '17 at 03:24
  • Not entirely. Not needing to know the destination is ideal, but I really need to have the Node be able to move in any direction, not just horizontally. It seems strange that Xcode has the capability to calculate complicated physics of an object and the direction it moves in and its velocity, but that there appears to be no simple functionality of just setting a node in motion on an angle, without having two factors, like I've mentioned. – Dan Harward Jones Jan 18 '17 at 02:19