Damage on collision

For questions about using Classic.

Post » Wed Dec 23, 2009 4:51 am

What's the best way to modify a private variable (i.e. health) by a variable amount on a collision, depending on the force of the collision? Here's a cap with sprites that crash into each other: [url:3rm972ed]http://sites.google.com/a/mike-wyatt.com/mike-wyatt-s-blog/PhysicsDemo1.cap?attredirects=0[/url:3rm972ed]. I don't see any physics-related conditions that are fired on collision. I also don't see a way to hook into the process and calculate any kind of change in velocity.

Thoughts?
B
2
G
3
Posts: 20
Reputation: 890

Post » Wed Dec 23, 2009 5:06 am

Well a collision is a collision regardless of the speed (as far as construct is concerned). So you need to do something like,
if sprite1 speed >= 5 AND on collision then subtract 5 from sprite1's hitpoints
else if sprite1 speed >= 10 AND on collision then subtract 20 from sprite1's hitpoints, and so no and so forth. Something like that anyways. (I know construct doesn't write it like that, but its easier to follow the logic).

If you're using physics though it might be more complex.
B
3
S
2
G
3
Posts: 628
Reputation: 2,531

Post » Wed Dec 23, 2009 9:30 am

Force is calculated as velocity * mass. Since velocity in Construct physics objects is separated into X and Y components, I believe you would just add them together. Don't quote me on it, but I'm pretty sure.

But since leftward and upward velocities give negative values you would have to use abs() to make sure you're only adding positive values.

Finally, velocity is returned as a floating point value so you'll probably want to round() or ceil() it into a more usable number. The final expression would look like so:

[code:3gyjlp4s]round((abs(Sprite[Physics].VelocityX) + abs(Sprite[Physics].VelocityY)) * Sprite[Physics].Mass)[/code:3gyjlp4s]

Here's a .cap showing the collision in action:

http://dl.dropbox.com/u/529356/usetheforce.cap

Throw the red ball at the green ball with the mouse. The force of the collision is shown in the text box. Notice that the force can get pretty high rather quickly, so you may want to calculate your final damage as a percentage of the total force instead. Also note that On Collision will keep triggering as long as the two physics objects are rubbing together, so you may want to give a bit of a grace period to your collisions so things aren't drained of all their health by getting pinned or something.

Oh and if I'm doing this totally wrong then someone please speak up :P
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Wed Dec 23, 2009 2:53 pm

[quote="deadeye":3j997sx3]Oh and if I'm doing this totally wrong then someone please speak up [/quote:3j997sx3]

:mrgreen:


You have to use to the Pythagorean theorem to find the hypotenuse of the triangle. The method Deadeye explained works ok most of the time, but you'll get slightly inaccurate results.

[code:3j997sx3]sqrt(Sprite[Physics].VelocityX^2+Sprite[Physics].VelocityY^2) * Sprite[Physics].Mass[/code:3j997sx3] No need for abs because the ^2 makes it positive.
B
25
S
3
G
6
Posts: 1,197
Reputation: 5,620

Post » Wed Dec 23, 2009 5:20 pm

Welp, there you go! Math whiz to the rescue.

I thought I had it down, but there was this sneaking little suspicion in the back of my head that said "no, not quite." Thanks for correcting that :)

Hey, at least I remembered velocity * mass off the top of my head. I did pay some attention in physics class :P.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 0 guests