If we ask mister Physic, he will talk about kinetic energy (Ek)
Ek = 1/2 *mass*speed^2
So what you would want to know is what is the Ek applied to an object.
And based on it's fragility (which could be an instance variable) you could break it or not.
Well fragility... It could really be just Hit Point. This way you can hit the same object many times until it break.
Also, the mass would be another instance variable.
So we just need to know the speed of the first object relatively to the other one.
Well wait a minute
obj1 has an Ek1 -> Energy impact = Ek1+Ek2 <- obj2 has is own Ek2
well it's true only for a frontal impact.
So how to evaluate non frontal impact?
You probably have to project the velocity vector of obj1 onto the velocity vector of obj2
Yeah I guess it would be like that:
[code]Sprite1: On Collision with Sprite2
Local Variable v1X // Sprite1.Physics.VelocityX (for simplification)
Local Variable v1Y // same
Local Variable v2X // same
Local Variable V2Y // same
Local Variable v1 // Sprite1 velocity projected to Sprite2 velocity
Local Variable v2 // Sprite2 velocity
Local Variable ECollision //Energy generated by the collision
-> System: Set v1X to Sprite1.Physics.VelocityX
-> System: Set v1Y to Sprite1.Physics.VelocityY
-> System: Set v2X to Sprite2.Physics.VelocityX
-> System: Set v2Y to Sprite2.Physics.VelocityY
//we get (more or less) the Magnitude of the vector (v1X,v1Y) projected on v2
-> System: Set v1 to -cos(angle(0,0,v2X,v2Y)-angle(0,0,v1X,v1Y))*distance(0,0,v1X,v1Y)
//Magnitude of the vector (v2X,v2Y) (actual speed)
-> System: Set v2 to distance(0,0,v2X,v2Y)
//Energy of the collision
-> System: Set ECollision to 0.5*Sprite1.mass*v1^2 + 0.5*Sprite2.mass*v2^2[/code]
Then you just have to use the value of ECollision as a Damage value. Decreasing HP of your sprites and breaking them if it's too high.
This calculation should also work if obj2 try to move away from obj1, the damage should be less than if obj1 is immobile.Yann2012-01-02 02:02:43