0
votes

unfortunately I have a supposedly simple "problem".

The Situation: I want to code my first "space shooter ... or something like that" with SpriteKit So I have (currently :-)): One Node, Three Buttons: 1 button for turning left, 1 for right & 1 for acceleration.

Turning is solved. Using impulse for giving the node a new direction

...The problem is :

Acceleration. I also use the impulse for getting the node "forward". But it don't want to move forward in the direction in which the node was turned.

My ideas solving the Problem: using the radiant of the zRotation, calculating the degrees, figuring out the CGVector via using "rad * cos(degree)" / "rad * sin(degree)".

result

  1. the node is not moving in the direction it should. especially if the rad is a negative figure ... :-D

So question is: How can I set an impulse / velocity for moving my future spaceship forward to the set direction .

thanks for your help!

greetings Sascha

1
What is a space shooter? If it is what I think it is, why does the spaceship even need to move? It's usually the background image that moves.El Tomato
El Tomato is right. This video tutorial might provide you with some helpful information. youtube.com/watch?v=0-lM51yI-PAsangony
Hello both, it's not that kind of a space shoot.Sascha
Hello both, thanks for your support. It's not that kind of a space shooter. I would like to control the spaceship via impulses / thrusters through the space. Means one touch to the button = one little boost with the thrusters. So left and right turn runs quite okay. But moving the ship forward is a bit tricky for me. Current Idea is, creating 2 further nodes within my "spaceship". One in the front, one in the back, for getting the vector of these two nodes. but problem here is, how can I calculate the position of a child node not referenced to the parent node but to the frame /"grandparent"Sascha

1 Answers

0
votes

The below code is taken from this video, where they are making a bullet go in the same direction as the space ship is pointing. The adjusted rotation is required if your sprites texture orientation is pointing up, whereas SpriteKit uses the right direction as 0 rotation.

  let speed: CGFloat = 1
  let adjustedRotation = zRotation + (CGFloat.pi / 2)
  let dx = speed * cos(adjustedRotation)
  let dy = speed * sin(adjustedRotation)
  spaceship?.physicsBody?.applyImpulse(CGVector(dx: dx, dy: dy))