0 Favourites

Damage on collision

This forum is currently in read-only mode.
  • 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: http://sites.google.com/a/mike-wyatt.com/mike-wyatt-s-blog/PhysicsDemo1.cap?attredirects=0. 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?

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • 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.

  • 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:

    round((abs(Sprite[Physics].VelocityX) + abs(Sprite[Physics].VelocityY)) * Sprite[Physics].Mass)[/code:3gyjlp4s]
    
    Here's a .cap showing the collision in action:
    
    [url=http://dl.dropbox.com/u/529356/usetheforce.cap]http://dl.dropbox.com/u/529356/usetheforce.cap[/url]
    
    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
  • Oh and if I'm doing this totally wrong then someone please speak up

    <img src="http://dl.dropbox.com/u/1010927/vectors.png">

    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.

    sqrt(Sprite[Physics].VelocityX^2+Sprite[Physics].VelocityY^2) * Sprite[Physics].Mass[/code:3j997sx3] No need for abs because the ^2 makes it positive.
  • 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 .

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)