[SOLVED] How to get RigidBodyControl sphere rolling in a specific direction

Hi, new here but been using jmonkeyengine for a month or so. I haven’t had a problem so far that I needed to post about but this one is doing me in, I cannot figure out how to get the sphere to roll in the intended direction. I have a RigidBodyControl controlling a sphere, and I’m using the applyTorque method to roll the sphere. ball_phy is the RigidBodyControl

@Override
public void simpleUpdate(float tpf) {

if (up) {        
    Vector3f force = new Vector3f(0,0,5);
    ball_phy.applyTorque(force);
}
if (down) {
    Vector3f force = new Vector3f(0,0,-5);
    ball_phy.applyTorque(force);
}
if (left) {
    Vector3f force = new Vector3f(5,0,0);
    ball_phy.applyTorque(force);
}
if  (right) {
    Vector3f force = new Vector3f(-5,0,0);
    ball_phy.applyTorque(force);
}

}

This works and the ball will roll in the proper directions, causing it to move forward, back, left and right but I want the ball to roll towards/relative to the direction the camera is looking. I have tried a few methods, this one is the most successful as it gets the ball to face where the camera looks, but otherwise causes the ball to shoot off into the sky

ball_phy.setPhysicsRotation(cam.getRotation().toRotationMatrix());

I think I need to go back over the maths again, but if anyone has any thoughts I would be very thankful.

1 Like

The direction the camera is looking is simply cam.getDirection(). Assuming your ball rests on the XZ plane, you probably want to limit the torques to the XZ plane, so set the Y-component to zero. It’s likely the ball will roll in a direction 90 degrees away from the axis of the torque, so you may also need to rotate the direction 90 degrees around the Y axis.

1 Like

Hey thanks for the reply. Didn’t completely understand what you meant but it lead to me using

@Override
public void simpleUpdate(float tpf) {

if (up) {

ball_phy.applyTorque(cam.getLeft());

}
if (down) {

ball_phy.applyTorque(cam.getLeft().negate());

}
if (left) {

ball_phy.applyTorque(cam.getDirection().negate());

}
if (right) {

ball_phy.applyTorque(cam.getDirection());

}
}

which is giving me what I wanted. Not sure why, but I’ll go with it.

1 Like