#Calculating a rigidbody's landing point, and how long it will take to get there?

1 messages · Page 1 of 1 (latest)

odd hornet
#

I have a spherical rigidbody, and I have some code to calculate its landing point given its position and its velocity. This is what that looks like:

public Vector3 GetPredictedPointOnArc(Vector3 position, Vector3 velocity, float t)
    {
        Vector3 point = Vector3.zero;
        point.y = 0.5f * Physics.gravity.y * (t * t) + velocity.y * t;
        point.x = velocity.x * t;
        point.z = velocity.z * t;
        return point + position;
    }

This is just done in a for loop for a set number of iterations, where each iteration raycasts towards the next. t represents how far to scan, and ideally is time.deltaTime.

This works to get a position, but how can i figure out the time it would take? what should i do?
This is the current code i'm using, but it's buggy and inaccurate:

time = (predictedLandingInfo.point.x - transform.position.x) / rb.velocity.x;
ripe slate
odd hornet
#

its all just done in a while loop until it either hits the max iterations or it hits a surface

#

i suppose i could do deltaTime multiplied by the loop iterations? but that doesn't sound right

ripe slate
#

Why it doesnt?

odd hornet
#

it doesnt move that fast

ripe slate
#

Ideally youd use Time.fixedDeltaTime, I dont quite understand what you mean by that tho

odd hornet
#

it wouldnt match up with how it'd actually go

ripe slate
#

Is 60th of second "ridiculously" short? Btw the physics system uses 50th of second as default timestep so thats how much time is spent between each physics update

lavish portal
#

There's a feature in Unity C# called physics scene, where basically you create a background scene which you can perform physics simulations of multiple frames in a single frame, and you can simulate time steps too, which would give you the time to land. I suggest you research more on this topic, I think it's what you're looking for.

icy scarab
#

@odd hornet did you just slander the code I helped you with 😭

odd hornet
#

the rest of the code works really well but the thing with doing it on the x is just like idk its dodgy

icy scarab
#

show an example of being inaccurate

#

start a timer upon jump and stop when it hits the ground

#

compare to estimated time

odd hornet
#

that's what i mean

icy scarab
#

THEN SAY THAY OMG

odd hornet
icy scarab
#

is that it? just nan? Or still inaccurate too

odd hornet
#

but only in one specific area

#

sometimes it is inaccurate as well

odd hornet
#

that just works

#

and its really accurate

#

thank you!

ripe slate
odd hornet
icy scarab
#

wait just use x and z

icy scarab
#

alright fine

#

inefficient

odd hornet
lavish portal
# odd hornet update: huh that works

Just want to point this out, but with this approach make sure you're using fixedDeltaTime and not deltaTime. Otherwise the prediction will go wrong if you get a lagspike.