Jump to content

P2 physics momentum transfer


flow
 Share

Recommended Posts

I'm doing P2 physics collisions with:

sprite.body.collides(collisionGroups, callback, this);

and I'm trying to get some info on the momentum transfer of the collision to calculate damage, but I can't find the right values.

What I've tried is to use the velocity difference like this:

var impactSquare = Math.pow(p2BodyA.velocity.x - p2BodyB.velocity.x, 2) + Math.pow(p2BodyA.velocity.y - p2BodyB.velocity.y, 2);

This works ok, but obviously it gives totally wrong values when the bodies fly towards each other and just barely touch, then deltaV is pretty high but there is basically no momentum transfer.

Since there is no V_old and V_new (as far as I know) to get the change of velocity per sprite (pre and post collision) I've tried to look into using the deltaX values:

dx = sprite.previousPosition.x - sprite.position.x ...

but stopped there because it feels like an over-complicated and unreliable way to get something that should be somewhere in the data anyway -_-

Has anybody an idea where to look?

Another (likely over-complicated) idea was to use the onBeginContact method to get the pre-collision velocity and store that somewhere so we can call it in the collides feedback, but ... well feels like there must be something simpler ...

 

 
Link to comment
Share on other sites

You could use speed instead of velocity as A basic measure:

before.body.speed - after.body.speed

Also don't forget that mass would also play A role (unless the bodies have equal mass), so you could consider A lower (relative) damage for heavier objects.

Link to comment
Share on other sites

 

body.velocity.magnitude I meant. Speed is the magnitude of velocity, it is an absolute value. They are different things. When you are going fast, you say I have high speed, but when you add the extra detail of direction (up and left), you talk in terms of velocity. With speed you don't need to worry about directions, which makes it more convenient in your case. Two cars approaching each other at the same high speed (100 km/h), have completely different (opposite) velocities (100 and -100).

velocity.png

Link to comment
Share on other sites

 When you look at my "impactSquare" value in the post above you can see that I'm actually calculating the absolute value of the vector difference so this is taken into account automatically at least for the resulting velocities AFTER the collision. Problem with "before.body.speed - after.body.speed" is as mentioned:

"Another (likely over-complicated) idea was to use the onBeginContact method to get the pre-collision velocity and store that somewhere so we can call it in the collides feedback, but ... well feels like there must be something simpler ..."

... I don't want to have all the overhead of defining onBeginContact listeners just to get a value that should be somewhere in the collision result ...
 

Is there no value stored in any of the collision callback data that still carries this info??

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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