angle2vector?

TwistedMechTwistedMech Member Posts: 408
edited November -1 in Working with GS (Mac)
hi

I want to get an X and Y position based on an Angle+distance from start point. This would be used to fire an object from the player to a direction set using the joystick template (right stick).

This could be used in a game to move an object using interpolate x,y.

I found some code which looks like it performs the calculation but need to find a GameSalad equivalent for "toRadians". It would be nice to have a command Angle2Vector.

Anyone have any ideas?

private Point2D.Double getPoint(double angle, double distance) {
// Angles in java are measured clockwise from 3 o'clock.
double theta = Math.toRadians(angle);
Point2D.Double p = new Point2D.Double();
p.x = center.x + distance*Math.cos(theta);
p.y = center.y + distance*Math.sin(theta);
return p;

Comments

  • Rob2Rob2 Member Posts: 2,402
    well from a quick google

    angle in radians = angle in degrees * Pi / 180

    but if you know your distance and the angle you can use

    x=distance*(cos(angle))
    y=distance*(sin(angle))

    but I might be wrong so just going to check :)
  • RHRH Member Posts: 1,079
    Just basic trig.

    Rob2 said:

    but I might be wrong so just going to check :)

    that's right!
  • tenrdrmertenrdrmer Member, Sous Chef, Senior Sous-Chef Posts: 9,934
    Am I missing something here? GameSalad has Angle2Vector You use it similar to magnitude where you use say (self.position.x-touch.x,Self.position.y-touch.y)

    Or am I just missing the whole point of this thread?
  • Rob2Rob2 Member Posts: 2,402
    @Tendrmer yes :) What Mech wanted was the X and Y coordinates of a point given its angle and distance from another point. What GS has is Vector to Angle which just gives the angle between two points.
  • TwistedMechTwistedMech Member Posts: 408
    it works very well. thank you.
Sign In or Register to comment.