Jump to content

P2 - rotate to velocity?


jeespu
 Share

Recommended Posts

Hi. I'm making a game where you shoot an arrow that uses gravity to fall down. The purpose is to hit birds with that arrow.

Currently I have a system, where you drag mouse on the playfield to adjust the power at which you shoot. This is done in P2 physics by setting x and y velocity for the created arrow object, in relation to the cursor's location and the "firepower"-variable.

First: should I implement that kind of system with "thrust" and "angle" instead of just setting velocities? Is there any actual difference?

Second: my main problem here is the angle. I can set the angle during object creation (to -90 so the arrow points upwards), but it won't change direction until it collides with a bird.

How do I recreate a "rotateToVelocity" (from the weapon plugin) effect in P2? I've been struggling with this issue all day. It shouldn't be this complicated to do.

Here's my code: https://pastebin.com/LShHiLJh

Link to comment
Share on other sites

About the angle I think you can get it via the arcus tangens of the velocity or some variation of that (maybe some offset) :-) assuming of cause that your arrow is flying the right path (due to initial speed and gravity) just the sprite rotation was missing.

Note: it will be radians so you can set sprite.body.rotation (not angle) with it.

[UPDATE]:

This should work:

sprite.body.rotation = Math.atan(sprite.body.velocity.y/sprite.body.velocity.x)+Math.PI/2

[UPDATE2]:
Actually it is a bit more complicated due to the nature of the arcus tanges since it depends on the direction your arrow is flying to. Your offset will need to be set by some if-then decision. The formular on the top should work for arrows that fly to the right ... I guess (hope^^).

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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