Predict where a KinematicBody will end without actually moving it
Asked Answered
C

10

0

I need to know (based on input data that I have, like speed, direction, delta, etc, etc) where a KinematicBody will end without actually moving it.

For example, if I have the function:

func move_2_steps(origin):
    return origin + 2

I know that, If I were to give an input of let say 10, the function would return 12, and then I could move the character.

But with kinematic bodies there seems to be no way, If I have the starting position, the velocity and delta, is there any way I could retrieve where will it end, without having to call move_and_slide or move_and_collide?

Just in case i'm implementing Client Side Prediction, so I have an array of inputs that I want to apply inside a for loop, something like this:

Vector3 endPos = Vector3.Zero;
foreach(Input input in array)
{
    endPos += SimulateMovement(input.direction, input.speed, someDelta)
}

Thanks!

Celeriac answered 19/1, 2023 at 19:54 Comment(0)
C
0

Using the "test" parameter of move_and_collide almost works, it will indeed report collisions without moving the KinematicBody, but it wont work since it will only check collitions based on the actual KinematicBody position, and I don't want to move it until the for loop has finished!

Also having a second, "hidden" KinematicBody could work, but I really really want to avoid doing that

Celeriac answered 19/1, 2023 at 21:18 Comment(0)
B
0

AFAIK neither bullet nor godot physics are deterministic, so you might want to try using some other physics engines. There's at least a couple of projects I think I've seen before. Quick search brought up this as the first result:
https://mcmap.net/q/2024/godot-getting-started-with-sg-physics-2d-and-deterministic-physics-in-godot-good-for-rollback-netcode

Bugger answered 19/1, 2023 at 21:56 Comment(0)
C
0

Bugger KinematicBodies aren't deterministic?!

Celeriac answered 20/1, 2023 at 0:30 Comment(0)
B
0

Just a heads up for if you are planing on doing anything that might make some use of the physics server.

Bugger answered 20/1, 2023 at 0:44 Comment(0)
W
0

Celeriac KinematicBodies aren't deterministic?!

The physics server does not do a perfect simulation of real world physics.

Washington answered 20/1, 2023 at 15:34 Comment(0)
C
0

Washington I don't care about replicating real world physics, I just need to know the results in advance, even if they are not deterministic or nor "real world"

Celeriac answered 20/1, 2023 at 16:34 Comment(0)
W
0

Deterministic means that an object moving at speed S for T seconds will travel a distance S * T, so you can predict the result accurately. The physics server may not be that accurate.

Washington answered 20/1, 2023 at 16:43 Comment(0)
B
0

Celeriac even if they are not deterministic

So the significance of it is that for the 'prediction' you shoot a ray or for example a projectile and display that as if it's a prediction, but then the final shot might land somewhat differently. Not that that couldn't work for you but if you just say predict people will likely think 'with precision' implying determinism.

So with that in mind in your example of a character you could create a duplicate of the character and move it, thus having your prediction/preview. It might not be deterministic in that the final move might move a bit differently but still end up at the same approximate spot. If that is fine then this is likely the way to go.

Bugger answered 20/1, 2023 at 16:51 Comment(0)
C
0

Washington I don't need accuracy or determinism, I only need to know in advance where the body will end, even if given the same inputs it lands on a slightly different place, and I need to apply this inputs on a for loop

Celeriac answered 20/1, 2023 at 17:6 Comment(0)
B
0

Celeriac Assuming the input is mouse input you could do a raycast from the characters current position in the directions of the mouse position and wherever the raycast intersects first you position a placeholder/duplicate node as the preview.

Alternatively as mentioned before, make a duplicate instance of the character and use the move_and_collide test parameter with the duplicate.

For a better answer than this, we really need to see/know more about your project scenetree setup and perhaps see some actual scipts/code.

Bugger answered 20/1, 2023 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.