Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Custom gravity implementation?

Discussion in 'Physics' started by HAlbera, Feb 25, 2016.

  1. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    Hello all!

    First time posting in the physics forum, maths is not my strongest subject, so here goes.

    I'm looking for a way to calculate the drop of a projectile due to gravity.

    I've decided against using unitys own physics system because it's just not accurate enough for what I need here.

    So far, I have a projectile system that works fine, I'm calculating the drag just fine, and raycasting towards the new position for collisions every fixed update. I'm purely calculating the new position and Translating the object rather than having any rigidbody.

    The drag simulation works beautifully, and my current implementation of gravity is 'okay' but it's simply this.

    Code (CSharp):
    1. //gravity
    2.         newPosition.y -= gravity * Time.fixedDeltaTime;
    Obviously this just simulates a constant downward force without acceleration.

    I've looked around the web for gravity formula, but my little brain can't seem to find anything that makes sense to me.

    Thanks in advance!

    Halbera.
     
  2. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
  3. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    Well I'm not sure if I can use the built in gravity simulation with what I need to collision detection and the accuracy I need with projectile physics.

    I'm using ballistics drag calculations and updating the transforms position manually, I'm not sure if I can somehow use the gravity without a rigid body.

    I dunno if you've ever tried to use unity's physics with a high speed gameobject, but it just doesn't work.

    All I need is to calculate the acceleration of gravity on the object, rather than the constant force I have right now.
     
  4. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    What's the problem you're facing? Object going through one another (missing collisions?) Did you turn on continuous collision detection on for the fast moving object? there are also other mechanisms you can use ( http://wiki.unity3d.com/index.php?title=DontGoThroughThings ) ..

    Maybe look here too: http://forum.unity3d.com/threads/dontgothroughthings-still-goes-through-things.11353/
     
  5. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    I have a mechanism for collision detection, I'm just trying to solve for gravity.

    As far as I'm aware, continuous collision detection isn't enough for objects travelling fast. I'm looking at 400+ units per second at the muzzle, with the drag calculations that slows quite rapidly though.

    I need calculate the acceleration of gravity on a bullet, taking into account terminal velocity, and applying that in a way I can use on a fixedupdate basis in unity.

    'vi = gt' is a start, where v is velocity, g is gravity in meters per sec per sec, and t is time.

    I could use that by keeping the time the projectile started falling to the current time and applying it as drop on the bullet, but it would infinitely increase in speed. Which isn't right. I just don't know how to factor in terminal velocity,
     
  6. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    A google search for: calculating terminal velocity

    Yielded this: (was combed from this page: http://www.wikihow.com/Calculate-Terminal-Velocity )

    Use the terminal velocity formula, v = the square root of ((2*m*g)/(ρ*A*C)).
    1. m = mass of the falling object.
    2. g = the acceleration due to gravity. ...
    3. ρ = the density of the fluid the object is falling through.
    4. A = the projected area of the object. ...
    5. C = the drag coefficient.

    Looks like a reasonably simple equation, I expect it's just a case of finding the ρ, A and C values for your simulation.
     
  7. dnkham12

    dnkham12

    Joined:
    May 18, 2021
    Posts:
    1
    I like your post