Jump to content

Move sprite from origin through a point


charlieRobinson
 Share

Recommended Posts

Hello. I have searched for a way to move a sprite from an X,Y origin through and through another X,Y point but I can't seem to find a good example.

I have found examples that tween a sprite from one point to another, that move a sprite to the mouse pointer, and so on. But I cannot find the best way to start a sprite at X,Y and move it THROUGH a 2nd X,Y so that it keeps going past that point but stays at that angle.

Thank you!

 

EDIT::: 

I have a feeling the solution is  velocityFromAngle or velocityFromRotation ... am I warm?

 

EDIT::::
Here's a pic describing what I am trying to accomplish

Untitled.jpg

Link to comment
Share on other sites

To find angle between points

dx = (endX - startX);
dy = (endY - startY);
tempAngle = Math.atan((dy)/(dx))*180/Math.PI;
movingStar.angle = tempAngle;

To move star towards its angle

movingStar.x += ((Math.cos((Math.PI*(movingStar.angle-180))/180) * movingStar.varSpeed));
movingStar.y += ((Math.sin((Math.PI*(movingStar.angle-180))/180) * movingStar.varSpeed));

 

Link to comment
Share on other sites

55 minutes ago, jjwallace said:

To find angle between points

dx = (endX - startX);
dy = (endY - startY);
tempAngle = Math.atan((dy)/(dx))*180/Math.PI;
movingStar.angle = tempAngle;

To move star towards its angle

movingStar.x += ((Math.cos((Math.PI*(movingStar.angle-180))/180) * movingStar.varSpeed));
movingStar.y += ((Math.sin((Math.PI*(movingStar.angle-180))/180) * movingStar.varSpeed));

 

Thank you very much!
when i use and console.log tempAngle I always get a result of ~40.   ???? I checked my start and end coordinates and they look just fine.

dx = (endX - startX); dy = (endY - startY); tempAngle = Math.atan((dy)/(dx))*180/Math.PI;I was using::: var angleDeg = Math.atan(endY - startY, endX - startX) * 180 / Math.PI;  but always resulted in ~ -90   ????

Aside from those issues, is it ok to change 

movingStar.x += ((Math.cos(...TO

movingStar.body.velocity.x = ((Math.cos(...I dont have this looping to  update each position with a += so the velocity would get the object moving without the loop.

Link to comment
Share on other sites

56 minutes ago, drhayes said:

jjwallace is doing it the correct way.

Here's the lazy way: Phaser.Arcade.accelerateToXY(sprite, x, y, xSpeedMax, ySpeedMax).

( ;

I tried to multiquote these 2 but ...

Thank you! I tried using this but received an error:

Uncaught TypeError: Cannot read property 'accelerateToXY' of undefined
 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...