Calculate linear velocity of a mesh ?
Asked Answered
H

4

0

Hi, I have a multiplayer authorative game, where the clients have no physics but needs the linear velocity of the body, I am currently sending the velocity from server to client, but in order to save band, I think I could calculate it locally, do anyone knows how to calculate it ? This is what I got so far, but I think I need some fixing in the calculation :

func _frameFlips(delta):
   var velocityVector:Vector3 = velocityFromMesh(carMesh,delta)

var oldmpos:Utils.TVector3
func velocityFromMesh(m:MeshInstance3D, delta)->Vector3:
	if(oldmpos==null):
		oldmpos = Utils.TVector3.new(m.global_position)
		return Vector3.ZERO
	else:
		var ret:Vector3 = oldmpos.vector3() - m.global_position
		oldmpos.setit(m.global_position)
		return ret * delta
Hyonhyoscine answered 30/9, 2023 at 14:21 Comment(0)
P
0

Hyonhyoscine The velocity is a derivative of position with respect to time. So numeric approximation would be delta postition divided by delta time.

Psychosocial answered 30/9, 2023 at 14:29 Comment(0)
H
0

Psychosocial i tried that, something is still not right, I am comparing the velocity of the physicbody with this and its completely different, just to have sure I understood you :

return ret / delta

Hyonhyoscine answered 30/9, 2023 at 15:4 Comment(0)
H
0

Hyonhyoscine Maybe it will allways be different because of rotation or other factors but the length will be the same ?
Those are the comparations :

val from ballbody=(-8.177293, 0.346106, 12.13281) vel from mesh=(0, 0, 0)
val from ballbody=(-8.429603, -0.528678, 12.28732) vel from mesh=(14.04476, 0, -20.46375)
val from ballbody=(-8.689482, -0.886039, 12.42926) vel from mesh=(14.47887, 0, -20.70541)
val from ballbody=(-8.689482, -0.886039, 12.42926) vel from mesh=(0, 0, 0)
val from ballbody=(-8.955921, -0.931516, 12.55922) vel from mesh=(14.92348, 0, -20.92571)
val from ballbody=(-8.955921, -0.931516, 12.55922) vel from mesh=(0, 0, 0)
val from ballbody=(-8.705107, -0.000099, 12.02771) vel from mesh=(14.51302, 0, -20.12081)
val from ballbody=(-8.757273, -0.000085, 11.88858) vel from mesh=(14.5937, 0, -19.82861)
val from ballbody=(-8.757273, -0.000085, 11.88858) vel from mesh=(0, 0, 0)
val from ballbody=(-8.805488, -0.000085, 11.72703) vel from mesh=(14.60047, 0, -19.45306)

Hyonhyoscine answered 30/9, 2023 at 15:6 Comment(0)
P
0

Hyonhyoscine If mesh origin doesn't coincide with rigid body's center of mass then there will be discrepancies if angular momentums are in the picture.

Psychosocial answered 30/9, 2023 at 16:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.