I am currently implementing a simple gravity system in my game using LibGDX. However, I found that the way you multiply vectors with vectors and vectors with scalars is a bit different.
In Unity, I would implement gravity like this:
public Vector3 velocity;
public Vector3 acceleration;
private void FixedUpdate()
{
transform.position += velocity*Time.fixedDeltaTime;
Vector3 force = Physics.gravity*GetComponent<Rigidbody>().mass;
acceleration = force/GetComponent<Rigidbody>().mass;
velocity = velocity + acceleration*Time.fixedDeltaTime;
}
}
Vector multiplication is very straight forward.
In LibGDX, I am trying the following:
import com.badlogic.gdx.math.Vector2;
final protected Vector2 _gravity = new Vector2(0f, -9.82f);
final protected float _mass = 1f;
protected Vector2 _velocity;
protected Vector2 _acceleration;
public void Update(float deltaTime)
{
// Transform is a class containing a Vector2 called Position
Transform.Position.add(_velocity.scl(deltaTime));
Vector2 force = _gravity.scl(_mass);
_acceleration = force.scl(1f/_mass);
_velocity = _velocity.add(_acceleration.scl(deltaTime));
System.out.println(_velocity);
// _velocity gets smaller and smaller and finally hits 0.
}
This does not work, since the scl() method of Vector2 directly changes the vector itself, as can be seen in the Vector2 class of LibGDX:
public Vector2 scl (float scalar) {
x *= scalar;
y *= scalar;
return this;
}
I assume I want it to return a new Vector2, instead of changing the vector itself directly.
How can I do that? Or am I completely wrong? Is there an easier way in LibGDX to multiply vectors with scalars using the "*" operator?