Search Unity

AI detect rigid body movement path

Discussion in 'Physics' started by arteetmarte, Nov 14, 2019.

  1. arteetmarte

    arteetmarte

    Joined:
    Mar 6, 2019
    Posts:
    4
    Hello, I need help figuring out two physics based scenarios within my game. I have searched high and low here and on Stack with none of the answers providing what I actually need nor a good solution. I apologize in advance if there is an undiscovered thread on my behalf.

    Regardless, the first scenario is that in my game the AI must “catch” an incoming rigid body; I have blendtrees set up for the different animations (up, left, right, etc) depending on where the rigid body would be coming from. The issue is, actually getting the AI to be aware or know where the object would be relative to them when they are going to attempt a catch (this would depend on the velocity/angle/height of the incoming projectile). So I’ve been basically looking for a solution that would tell the AI exactly the point of intersection before the object is even there to know whether it fires an animation to lunge left to catch, or right, or jump, or middle down etc. including the fact if the object would be beyond their reach.

    I’ve used physics simulation algorithms and all but they either are to slow or not accurate for what I need. I don’t know if it’s due to the fact that the object in question that I’m trying to solve for has a drag value of 0.336 and a mass of 0.146. I currently have it set up that the AI game object has a Distance SqrMagnitude code triggering when the object is near them or incoming to trigger the function and this works well to detect the incoming rigid body but I’m just missing the fine tune part of letting the AI know exactly where it will be once it reaches the AI in order to fire off the correct animation and the AI could actually catch it.

    Side note, I have the code setup that once it enters the trigger sqrmagnitude range the catch animation fires and the AI does catch it BUT, the rigid body visually and collision wise will pass the AI which is in animation and then all of a sudden the object will appear in its hand. Therefore, if the object is in the air and the AI “catches” it, the object keeps going past it and hits the floor or continues past it and then just appears in its hand after. I have them both set to continuous collision detection and all yet it still happens.

    Secondly, this part kind of ties in with the first but not exactly. I’m having an issue as well finding a real good put together solution to know exactly where a rigid body would land on the floor if it’s shot up in the air. So the AI navmesh would run to this spot to try and catch it before it lands; so I am trying to get the vector coordinates of that exact landing spot from as soon as an object is in motion. I’ve tried numerous solutions online with mixed results because most don’t even take into account, like before, the objects drag which I have at a 0.336 value with a mass of 0.146 just as before.

    If anyone could help me out with this in any way, shape or form, including even pointing me to the right place online because I’ve honestly searched everywhere.

    Lastly, really sorry about the length of the post lol.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    6,440
    So from what you wrote, you need trajectory prediction.

    I suggest two options.
    First is to find equation of damping factor in Unity physics. Then of course you may need ballistic equations. Damping is somehow simplified, hence your prediction may be inaccurate. Having that, you can predict trajectory, until hits a ground.

    Second, my preferable choice, is using own rigid body to add force, to simulate gravity and damping factor. This way, I know exactly what affects the trajectory, and can calculate easily future positions.
     
  3. arteetmarte

    arteetmarte

    Joined:
    Mar 6, 2019
    Posts:
    4
    Thanks for the reply. Yes I agree, the thing is, none of the solutions or examples i've found online either work well or give me the results desired. Do you have anything I can go off by? Or if its not too much to ask, a sample script implementing what you mentioned?
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    6,440
    I am on mobile, so I have nothing off hand.
    But for gravity simply add force downward every fixedupdate, with respect to time. See rigid body add force documentation for details.

    Don't forget disable default gravity and set rigid body damping to 0.
     
  5. arteetmarte

    arteetmarte

    Joined:
    Mar 6, 2019
    Posts:
    4
    Ok, I get what you’re saying; basically to take control of the physics entirely in the code applying my own gravity which will in turn let me actually compute the future position. But, while my C# scripting is on point, unfortunately my physics math is not lol. So, how would I turn that into a formula to know where the rigid body would be when it’s at the “catch” distance from the AI GameObject?


    For some pseudo code visualization take a look below to see how I have it in my mind so far.. I believe it’s on the right track with the exception of getting the values I stated above (specially if the object is coming in fast and the values need to be quickly figured out)


    public Transform catchThisObject;

    public float closeDistance = 5.0 f;



    Vector3 offset = catchThisObject.position - transform.position;

    float sqrLen = offset.sqrMagnitude;

    if (sqrLen < closeDistance * closeDistance) {

    //Function fires to calculate the position of the catchThisObject when within this range taking into account the catchThisObjects mass, drag, and velocity

    //Function returns a Vector position for X (if the catchThisObject will be right, left, middle, left middle, right middle

    //Function returns a Vector position for Y (if the catchThisObject will be low, middle, high

    ** From here I know what to do since it’s pretty straight forward I send the float values of X and Y to the animator which fires the appropriate animation from the blendtree I set up.. This part I coded already a long time ago **

    }
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    6,440
    Regarding future position, you need calculate in single frame, all steps in flight, until thrown object hits the ground, or at lest to minimum height of catcher. Then catcher will need check few relevant positions from the traced path. Which is lookup search in array/list.

    For simplication, you can use physics calculation per second, rather per FixedUpdate delta time.

    Again, is worth to look into ballistic equations, rigid body add force, and prediction of the path.

    Look for simple pool games as an example, where balls position prediction is shown.
     
  7. arteetmarte

    arteetmarte

    Joined:
    Mar 6, 2019
    Posts:
    4
    Ok, I kind of get where you’re going which is what I have found online in variations; although none are reliable enough or CPU Usage friendly from my experience thus far.

    So is there no way to simply fire a given function while the rigid body is in “movement” with its current velocity, direction, forces, and so forth, that would simply upon trigger return a Vector3 position of where that Rigidbodys center will be when it nears the target gameObject 0.25f sqrmagnitude? There’s no formula that can be applied or conjured up taking all that into consideration? If not then the only other way is to do the long haul of a physics simulate into the future? I’ve seen varied attempts or solutions for specific needs but never to such a precise issue as mine where custom drag, mass, and current velocity need to all factor in.. Btw, I am a good coder but I simply suck at Physics and anything dealing with its Math; never even took a course or anything on it except what I’ve done in Unity thus far, which is why I’m stumped on how to figure this out because I don’t even know what’s what on the long equations mapped out in explanations I’ve tried reading up on. Being 100% honest here when I say I’m lost looking at those things.
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    6,440
    There are certain tricks for simplification of computing process. But that still doesn't I'll require to apply and compute trajectory points.

    Ballistic trajectory prediction is your really needed to.understand. Equation isn't complex. But it may be at first intimidating. This isn't new problem, including Unity, so there are tons of resources discussing about it.

    There are even source codes snippets.