Search code examples
javalibgdx

Using force to move an object in Libgdx Java


I have the task of creating my own Physics Engine for collision in java, using the LibGdx game engine. It is a top down, so ignore gravity. I am stuck at figuring out how to apply a force to a body. I am finding the acceleration by finding the change in velocity over a time. Here is how the acceleration for x-component is found:

    Vector2 velX = new Vector2();
public void update(){
    timer += Gdx.graphics.getDeltaTime();

    movementSpeed = 5 * Gdx.graphics.getDeltaTime();
    coordinates.x += vel.x;
    coordinates.y += vel.y;
    if( timer >= .5f && timer < 1){
        velX.x = vel.x;
    }
    if(timer >= 1){
        velX.y = vel.x;
        acceleration.x = (float) ((velX.y - velX.x)/ (1 - .5));
        timer = 0;
    }

vel.x is the x velocity, and velX is a Vector2 for storing the x component of the velocity at different times ( I am using the Mean-Value Theorem). Essentially, I have found the acceleration for the x component. Now if I apply a force, I do the acceleration * the mass. Here is where I am completely confused.

F = ma. Yes, so I am able to get a force variable. But how do I now use this force to affect the velocity appropriately. Essentially, after a force is applied, the velocity should chance. How do I change the velocity from some value of force when they are two completely different variables ( Newtons and Meters/Second).

Any help with this is extremely appreciated.


Solution

  • With the initial velocity v1 and mass m1 your initial momentum p1 = v1m1. (m1 + m2)vf = m1v1 is also true, assuming that your initial velocity of the second object is 0. It means that the final speed of both objects will be lets call it final velocity vf = m1 * v1 / m1 + m2.

    If you are planning to collide moving objects with realistic physics i suggest you some advanced physics lessons / tutorials.