Hello there!
I’m working with this example (asteroids movement): LINK
There is a strange behaviour that blows my brain out. It can be better seen on the attached video.
All I’ve done was a small rotation and then just pressed UP cursor key.
VIDEO
As you can see (with the help of turned on debugger) velocity vector moves (rotates) during fly until it reaches bottom-right corner of the bounding box. Also it force ship to fly wrong, it can be noticed even without debugger. Expected that velocity vector to be applied directly to where ship’s nose is (or more correctly, ship have to move to the opposite side the engines work vector; see the picture).
What can be the solution of this problem? Seems velocityFromRotation() function works not as expected. Or there is something been lost.
Best regards,
Nick Rimer
The example uses setMaxVelocity
to limit the ship’s speed (this is around line 175). The two axes of the velocity are calculated separately. So, when moving at an angle, one axis of the velocity will increase faster than the other and it will stop increasing when it hits the max value. The slower axis will keep changing independently until both axes are equal. This explains why the ship changes direction while moving. You can fix it by using ship.body.setMaxSpeed
instead of ship.setMaxVelocity
; when the speed is limited, the entire velocity vector is scaled, not its individual components.
Even so, the same problem will occur when decelerating because drag is also applied separately by default. You can fix this by making the body use damping instead of linear drag: ship.body.useDamping = true;
You would also need to tweak the drag; as the docs say, albeit a little indirectly, it should be a number less than 1 when using damping.
All of that said, I’m not sure why the example misuses all of these features. It took me a while to figure out why the velocity drifts like that, but the docs outright tell you that damping should be used if your angles change freely.
2 Likes
Whoa! Thank a lot, man. Your answer forced it to work good for now!
And a bit offtopic. Can you recommend something good material to well understand what is what in Phaser? Seems reading the whole documentation up to down is not a good idea. Really wanna understand how to understand what methods or properties to use in what kind of situation/mechanics.
Best regrads,
Nick Rimer