-1

I have a sprite that needs to follow the command of an arrow sprite that rotates, right now I can only make my sprite move up,down and left,right. How about diagonally? I have no Idea how to do that, the sprite also needs to move always towards outside the screen.

EDIT: So this is what I did:

Vector2 position=new Vector2();

public void update(){

//getAngle() is the return value from another class     
position.set(MathUtils.cosDeg(cannonObj.getAngle()),MathUtils.sinDeg(cannonObj.getAngle()));
    sprite.setPosition(position.x,position.y);
    }

What happens is that when I rotate the arrow the main sprite is just moving like the arrow(moving in a circular motion).

//I also tried this
position.scl(2,2);
 or
position.translate(2,2);
Kevin Bryan
  • 1,846
  • 2
  • 22
  • 45

1 Answers1

1
sprite.x += sin(angle) * amount;
sprite.y += cos(angle) * amount;

I forget whether you have to enter your angle in radians or degrees. In Java you can use the Math class to do the calculations though.

Huang Chen
  • 1,177
  • 9
  • 24
  • Sorry I'm new to java but what you are trying to say is that I should multiply the sin and cos to the angle of the arrow? then multiply again with the amount of pixel I want to move the sprite.. – Kevin Bryan Jul 29 '15 at 14:16
  • no you take the sin/cos of the angle then multiple by the amount. Basic trignometry – Huang Chen Jul 29 '15 at 14:18
  • take the sin/cos of the angle of my sprite or the arrow sprite? – Kevin Bryan Jul 29 '15 at 14:28
  • well if the sprite follows the command of an arrow sprite then the sin/cos of the angle – Huang Chen Jul 29 '15 at 14:30
  • 2
    The angle to use in these formulas is the angle between a right-pointing line and the direction you want it to travel, counterclockwise. If you know the angle in radians, use `MathUtils.cos()` and `MathUtils.sin()`. If you know the angle in degrees, use `MathUtils.cosDeg()` and `MathUtils.sinDeg()`. – Tenfour04 Jul 29 '15 at 16:56