Sign in to follow this  
smatthews1999

Simple P2 Question

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

Share this post


Link to post
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;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.