Jump to content

Simple P2 Question


smatthews1999
 Share

Recommended Posts

Here's my question.

 

I want to make a P2 body move in the direction it is already pointing.

 

I tried moveUp(), moveDown(), etc. but they (not surprisingly) only go up or down.

 

I tried thrust() but it does not seem to work unless it is being applied repeatedly.. as in the Update section.

 

Just for the heck of it I tried this....

 

for (var i = 0; i < 500; i++)        mysprite.body.thrust(500);
 
and it did what I want, but I feel that this is probably not the way I should do this.
 
Is there a better way?
 
Thanks,
Sam
Link to comment
Share on other sites

Sorry, I should have read your questions more closely. From what I've seen in the examples, you need extract the body's direction (i.e., rotation) and apply XY explicitly. E.g., for constant velocity:

bug.body.velocity.x = Math.cos(bug.rotation) * BUG_SPEED;bug.body.velocity.y = Math.sin(bug.rotation) * BUG_SPEED;

To accelerate, there's a good example here http://phaser.io/examples/v2/p2-physics/accelerate-to-object. Below's a snippet:

obj1.body.force.x = Math.cos(angle) * speed;    // accelerateToObject obj1.body.force.y = Math.sin(angle) * speed;
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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