# Aligning setLinearVelocity to [vehicle].getPhysicsLocation without lag?

I am attempting to get the direction that a vehicle is facing and use that direction to set the direction of a projectile. I am firing the projectile with setLinearVelocity, which sort of works, but there seems to be some sort of lag between the direction of the vehicle and the direction of the projectile. What’s the best way to accomplish this task?

The projectile is being fired with this code:

[java]
public void fireCannonBall(Vector3f cannonBallOrigin) {
/** Create a cannon ball geometry and attach to scene graph. /
Geometry ball_geo = new Geometry(“cannon ball”, sphere);
ball_geo.setMaterial(stone_mat);
rootNode.attachChild(ball_geo);
/
* Position the cannon ball */
//Vector3f cannonBallOrigin = cannonBallOriginObject.getLocalTranslation();

``````    //ball_geo.setLocalTranslation(cannonBallOrigin); // works
//    ball_geo.setLocalTranslation(cam.getLocation());
/** Make the ball physcial with a mass &gt; 0.0f */
ball_phy = new RigidBodyControl(1f);
/** Add physical ball to physics space. */
/** Accelerate the physcial ball to shoot it. */
//ball_phy.setLinearVelocity(cam.getDirection().mult(25)); // sort of works (not really)
//ball_phy.setLinearVelocity(cannonBallOrigin.mult(2)); works better than using cam.
// Now we just need to figure out the direction.
//ball_phy.setPhysicsRotation(vehicle.getPhysicsRotation()); // Does this do anything?
ball_phy.setLinearVelocity(vehicle.getPhysicsLocation().mult(2)); // works much better, but lags behind until acceleration occurs

// Can I use the physics rotation quaternion?
``````

}[/java]

Why would setting the velocity to the location of the car make any sense? Its a velocity vector and a location vector, they are different things.

I’m planning to attach a cannon to the top of the car. I want it to fire in the direction that the car is facing. For the example from https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_physics, regarding the line, “ball_phy.setLinearVelocity(cam.getDirection().mult(25));” the comment states, ‘Since you are shooting cannon balls, the last line accelerates the ball <span style=“text-decoration:underline;”>in the direction</span> the camera is looking, with a speed of 25f.’

Instead of accelerating the ball in the direction the camera is looking, I want to accelerate the ball in the direction the vehicle is facing. The cannonball is intended to fire from a node on the top of the vehicle (in the direction the vehicle is facing).

ball_phy.setLinearVelocity(vehicle.getPhysicsLocation().mult(2));

location != rotation
location != facing

But the location of the vehicle is not the direction that the vehicle is facing in… Its the location of the vehicle. You probably want to do something like:

ball_phy.setLinearVelocity(vehicle.getPhysicsRotation().mult(Vector3f.UNIT_Z).mult(2));

1 Like

@pspeed, I’m not sure that I understand what you’re suggesting. You’re saying the location of the cannonball should not equal the rotation or the direction that the vehicle is facing? How would that solve the problem?

@normen, ah thanks! I completely overlooked that.

The problem is that you treat the location and the direction of the vehicle as if they were the same, they are not.

@jMethyl said: @pspeed, I'm not sure that I understand what you're suggesting. You're saying the location of the cannonball should not equal the rotation or the direction that the vehicle is facing? How would that solve the problem?

You are using the LOCATION of the vehicle as the DIRECTION of the vehicle.

Do you understand that the location of the vehicle is not the same as the direction the vehicle is facing?

“Which way do you want to fire the bullet?” “Alaska * 2” It makes no sense.

Edit: Ninja’ed by everyone

This worked:

[java]ball_phy.setLinearVelocity(vehicle.getPhysicsRotation().mult(Vector3f.UNIT_Z).mult(-20));[/java]

Thanks for the help guys.

Looks like your vehicle goes backwards then.

Yes, I was using the example from https://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/test/jme3test/bullet/TestFancyCar.java. It does appear to go backwards.

Yeah, it says so in the example code.