Search Unity

Question Appropriate force to add for collision

Discussion in 'Physics' started by MoonJellyGames, Jul 19, 2021.

  1. MoonJellyGames

    MoonJellyGames

    Joined:
    Oct 2, 2014
    Posts:
    331
    Hey all,

    I have a physics/math problem here. I need to add an appropriate amount of force to an object within OnTriggerEnter2D as if it were a normal collision event that factors in relative velocity of the two objects as well as their masses. I know that these values are necessary for the calculation, but I don't know what to do with them. I've also read that their velocity's "dot products" may also be needed. I gave that a quick Google search, and while the math is simple enough, I don't understand how it fits in here.

    If you're curious why I'd need to do this in the first place, it's due to the fact that my player (space ship) spawns bullets within its own collider. There are variety of bullet sizes and shapes, so placing the spawn point just outside of the ship's collider isn't really practical. It's complicated by the fact that it may, in some cases, be possible for a ship to hit itself by way of portals, so I can't just nix the bullet-to-ship collision. Instead, I have bullets spawn as triggers which cache their owner and don't allow a "hit" to register until a flag is set in OnTriggerExit2D, which also turns the bullets into non-triggers. The push of a collision is important. The thing is, if I shoot at something point-blank I need the "collision" to happen at the right place, so the bullets will register hits with other ships in OnTriggerEnter2D. They deal damage and disable themselves (for reuse) as they're supposed to, but no force is being applied on impact. That's the only missing element.

    Hopefully that was all clear. Thanks!
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Can you not just take the position of the bullet, and use AddForceAtPosition() using ForceMode Impulse to apply the force over a single frame? If you wanted accurate physics, you might have to calculate the normal force of the collision, but calculating it just from its velocity probably wouldn't look too weird.
     
    MoonJellyGames likes this.
  3. MoonJellyGames

    MoonJellyGames

    Joined:
    Oct 2, 2014
    Posts:
    331
    Thanks for the response. That's pretty much what I've been working with, except I've been using AddForce rather than AddForceAtPosition, as I don't know what the position ought to be. Also, I'm not sure what the force value should be. Maybe the magnitude of the relative velocity? But that still doesn't factor in mass.
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Would the Force Position not just be the position of your projectile?

    And for the second part, to factor in your mass you just multiply by mass o_O granted, you might want to add an extra "explosion" force to it as well.
     
    MoonJellyGames likes this.
  5. MoonJellyGames

    MoonJellyGames

    Joined:
    Oct 2, 2014
    Posts:
    331
    Maybe. I thought that the force position would be in the hit object's local space, which means it may not translate correctly. I didn't mess around with it, to be honest. I'll have to try that.

    As for the mass-- fair enough. For some reason, I (wrongly) thought that if I only used the mass of my projectiles, it wouldn't consider the mass of the hit object, but the AddForce or AddForceAtPosition functions would handle that.

    Thanks again.