truck_town - How to calculate speed?

kkmzerokkmzero Posts: 3Member

I used scripts from this project for controlling the car and it was fairly easy to implement reverse to the car script (vehicle.gd) however it is not working really flawlessly. I can imagine it would work much better if I could get some speed value of the car to decide if the car is moving or not, then I could with same key control if car should brake when moving (increase brake value) or if it's not moving to reverse (increase negative engine force value). Does anybody try to do this before? Is there any reasonable way to know if the car is moving and/or in what speed is car moving?

https://github.com/godotengine/godot-demo-projects/tree/master/3d/truck_town

Answers

  • wombatstampedewombatstampede Posts: 72Member
    Answer ✓

    VehicleBody is derived from RigidBody so you can use the property "linear_velocity" to get a Vector3 with the velocity vector.
    If your VehicleBody is i.e. named "MyCar" then you would get the moving speed with $MyCar.linear_velocity.length().

    If you assume one size unit as one meter then this would be the speed in m/sec. Multiply with 3.6 to get km/h.

  • kkmzerokkmzero Posts: 3Member

    @wombatstampede said:
    VehicleBody is derived from RigidBody so you can use the property "linear_velocity" to get a Vector3 with the velocity vector.
    If your VehicleBody is i.e. named "MyCar" then you would get the moving speed with $MyCar.linear_velocity.length().

    If you assume one size unit as one meter then this would be the speed in m/sec. Multiply with 3.6 to get km/h.

    It seems it's more complicated as I thought before and I would have to count also with angle of the car itself or something. So how this works now is that I press key when velocity is above f.e. 1, then i brake. However, if I brake until velocity is less than 1 and then I start to reverse, the velocity of the car won't go over 1 in reverse mode, obviously.

    if Input.is_action_pressed("ui_down"):
        if (speed < 1):
            throttle_val = -0.5
        else:
            brake_val = 1.0
    
  • wombatstampedewombatstampede Posts: 72Member
    Answer ✓

    "Forward" velocity:
    var fwd_mps = $MyCar.transform.basis.xform_inv($MyCar.linear_velocity).z

    If I remember correctly this should convert the global velocity into a velocity vector which is rotated to your cars local coordinate system.
    So if your car moves along its z-axis then this value should be positive if it moves to z+.

  • kkmzerokkmzero Posts: 3Member

    @wombatstampede said:
    "Forward" velocity:
    var fwd_mps = $MyCar.transform.basis.xform_inv($MyCar.linear_velocity).z

    If I remember correctly this should convert the global velocity into a velocity vector which is rotated to your cars local coordinate system.
    So if your car moves along its z-axis then this value should be positive if it moves to z+.

    It works flawlessly with this value. Thank you really a lot.

Sign In or Register to comment.