Search Unity

Add physics to a kinematic rigidbody (or something else)

Discussion in 'Physics' started by Ambrose998800, Nov 30, 2019.

  1. Ambrose998800

    Ambrose998800

    Joined:
    Jun 23, 2015
    Posts:
    32
    Hi there!

    I've an issue that drives me crazy. In my asteroid-fighter game I have exploding asteroids. The fractured Parts are instatiated and should take over the parents movement and rotation (parent = intact asteroid). They shouldn't rotate around their own center, but around the center of the former asteroid.

    Now, the parts only take over the movement and rotation when they are child and set to kinematic. But they need also to be blasted away by the explosion (physics). As soon I set them to non-kinematic, they stop movement and start to wobble like flummy:



    I tried (I think) everything; manipulate center of mass, add torque or force to the parts as child or stay alone objects, non physics rotation, time directed child dismiss, different relations between child and parent objects with or without sub-objects. It just wont work.

    Best would be, the fractured parts are non-childs with tookover rotation and movement by the center of the former asteroid...

    Any ideas?

    Thanks, Ambrose
     
  2. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    50
    Can you tell me more about the effect you'd like to achieve? In case you want the asteroid to slowly disintegrate, you'll need to simulate some gravitational pull towards the former position of the parent asteroid.

    I'd go about this like follows:
    1. Have one object for the asteroid with a rigidbody. As soon as the player starts to shoot at it, create disabled gameobjects for the child fragments with a rigidbody each.

    2. When it's about to explode, unparent all the children from it, set their child.rb.velocity to their parent.rb.GetPointVelocity(child.rb.transform.position) and copy the parent angularVelocity (child.rb.angularVelocity = parent.rb.angularVelocity). Finally, put the parent on a different layer that doesn't render and doesnt collide with anything.

    3. Keep a reference of all the children. Add a gravitational force to all the children towards the center of mass of the parent asteroid that still there, but not rendered or colliding with anything. The force you need can be calculated as follows:
    Bildschirmfoto 2019-12-02 um 12.43.14.png
    https://en.wikipedia.org/wiki/Centripetal_force
    Where m is the mass of the fragment, v^2 tangential velocity of the fragment relative to the velocity of the former parent and r the distance between the two center of masses (fragment to parent).

    Now fade out this force and you'll see the asteroid slowly disintegrate.

    Please note that the physically "correct" behaviour would be just 1. and 2. because even with asteroids on the scale of kilometers diameter, there is almost no gravity at all.
     
  3. Ambrose998800

    Ambrose998800

    Joined:
    Jun 23, 2015
    Posts:
    32
    Gravitation is already working, but disabled right now.
    The disintegration itself isn't the problem, but in combination with the rotation around the former center of mass.

    I want the explosion (like in first video) with the childs still rotationg like in following example (Note: here the asteroid is fractured too, but the pieces are still in place cause I've set their rigidbodies to kinematic that they take over the rotation from parent and not just wobbling like before):



    But the "parent.rb.GetPointVelocity(child.rb.transform.position)" will maybe help to get closer to what I want... I'll try that out.

    Thanks for your effort.

    Edit:

    Well, with that line of code (GetPointVelocity) I didn't know, I'm close to what I wanted (after a small delay seen here):



    The final tweaks should not be to hard to make. Thanks!
     
    Last edited: Dec 2, 2019
  4. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    50
    Looks really nice!