Skip to main content
replaced http://gamedev.stackexchange.com/ with https://gamedev.stackexchange.com/
Source Link

Well, I generally use to ask this questions in interviews :)

Although I don't know Swift but I can give you a general idea about to achieve this.

You have to play with some trigonometry to get current direction vector according to your current angle.

Here is the pseudo code.

float speed = 0.01 void GameLoop() { // I don't know that how Vector2 works at your end, but I hope you will get this. Vector2 currentPosition = currentPositionOfObject; float currentAngle = currentAngleOfObject; float directionX = Cos (currentAngle); // check if your Cos method takes angle in radians of degrees. float directionY = Sin (currentAngle); // check if your Sin method takes angle in radians of degrees. Vector2 newPosition = (currentPosition.x + (directionX * speed),currentPosition.y + (directionY * speed)); currentPositionOfObject = newPosition; } 

I have already answered the same question but in Unity (C#) herehere. Just for reference if you want more details.

Well, I generally use to ask this questions in interviews :)

Although I don't know Swift but I can give you a general idea about to achieve this.

You have to play with some trigonometry to get current direction vector according to your current angle.

Here is the pseudo code.

float speed = 0.01 void GameLoop() { // I don't know that how Vector2 works at your end, but I hope you will get this. Vector2 currentPosition = currentPositionOfObject; float currentAngle = currentAngleOfObject; float directionX = Cos (currentAngle); // check if your Cos method takes angle in radians of degrees. float directionY = Sin (currentAngle); // check if your Sin method takes angle in radians of degrees. Vector2 newPosition = (currentPosition.x + (directionX * speed),currentPosition.y + (directionY * speed)); currentPositionOfObject = newPosition; } 

I have already answered the same question but in Unity (C#) here. Just for reference if you want more details.

Well, I generally use to ask this questions in interviews :)

Although I don't know Swift but I can give you a general idea about to achieve this.

You have to play with some trigonometry to get current direction vector according to your current angle.

Here is the pseudo code.

float speed = 0.01 void GameLoop() { // I don't know that how Vector2 works at your end, but I hope you will get this. Vector2 currentPosition = currentPositionOfObject; float currentAngle = currentAngleOfObject; float directionX = Cos (currentAngle); // check if your Cos method takes angle in radians of degrees. float directionY = Sin (currentAngle); // check if your Sin method takes angle in radians of degrees. Vector2 newPosition = (currentPosition.x + (directionX * speed),currentPosition.y + (directionY * speed)); currentPositionOfObject = newPosition; } 

I have already answered the same question but in Unity (C#) here. Just for reference if you want more details.

Source Link
Hamza Hasan
  • 3.4k
  • 2
  • 15
  • 24

Well, I generally use to ask this questions in interviews :)

Although I don't know Swift but I can give you a general idea about to achieve this.

You have to play with some trigonometry to get current direction vector according to your current angle.

Here is the pseudo code.

float speed = 0.01 void GameLoop() { // I don't know that how Vector2 works at your end, but I hope you will get this. Vector2 currentPosition = currentPositionOfObject; float currentAngle = currentAngleOfObject; float directionX = Cos (currentAngle); // check if your Cos method takes angle in radians of degrees. float directionY = Sin (currentAngle); // check if your Sin method takes angle in radians of degrees. Vector2 newPosition = (currentPosition.x + (directionX * speed),currentPosition.y + (directionY * speed)); currentPositionOfObject = newPosition; } 

I have already answered the same question but in Unity (C#) here. Just for reference if you want more details.