Rotating tank canon to mouse position

Hello still the same game :slight_smile: but another problem.

Now I want to rotate the tank canon to the mouse coordinates. I already have to position of the mouse in the "game" coordinates. I only need an idea how to rotate the canon.

Here is the code of my tank. It has a rotation axis between the down part (wheels) and the rotator (conduit).

       tank =  pS.createDynamicNode();
       canon = pS.createDynamicNode();
      Box tankname = new Box( tankName, new Vector3f(), 0.35f, 0.3f, 0.3f );
      Box rotater = new Box("rotater", new Vector3f(), 0.1f, 0.025f, 0.1f );
      // Tank down part
      tank.attachChild( tankname );
        playerLeftPart = tank.createBox( "left" );
        playerLeftPart.getLocalTranslation().set( 0, 0, 0.15f );
        playerLeftPart.getLocalScale().set( 0.7f, 0.6f, 0.3f );
        playerRightPart = tank.createBox( "right" );
        playerRightPart.getLocalTranslation().set( 0, 0, -0.15f );
        playerRightPart.getLocalScale().set( 0.7f, 0.6f, 0.3f );
        tank.getLocalTranslation().set( currentPosition );
        // Rotator and aim

        playerRotatorPart = canon.createBox("up");
        playerRotatorPart.getLocalTranslation().set( 0, 0, 0.0f );
        playerRotatorPart.getLocalScale().set( 0.2f, 0.05f, 0.2f );
        Vector3f tmp = new Vector3f(currentPosition.clone());
        canon.getLocalTranslation().set( tmp.x + 0.15f, tmp.y + 0.325f, tmp.z );
        Box shooter = new Box("shooter", new Vector3f(), 0.3f, 0.075f, 0.075f );
        shooter.setLocalTranslation(0.25f, 0.1f, 0.0f);
        playerShoter = canon.createBox("up");
        playerShoter.getLocalTranslation().set( 0.25f, 0.1f, 0.0f );
        playerShoter.getLocalScale().set( 0.6f, 0.15f, 0.15f );
        // Collision Group
        Random r = new Random(System.currentTimeMillis());
        cgTank = pS.createCollisionGroup("tankCG" + r.nextInt());
        cgTank.collidesWith(cgTank, false);
      // Joints
      joint_tank_canon = pS.createJoint();   

      // attach to joins
      rotAxis = joint_tank_canon.createRotationalAxis();
      rotAxis.setDirection(new Vector3f(0,1,0));

I am able to move the conduit by pressing a key and using the function 


But I do not want to move the conduit  key. Would be nice to do it with the mouse position.
(So the conduit follows the mouse movement). Can someone help me?

With kind regards


I've the same question. Which way is the best to achive that?

Get the vector for the cannon's z-axis, and the vector pointing from the cannon (world translation) to the target position.

Calculate the angle between them (in the plane you are rotating in).

Rotate left or right depeding on the angle (eg. right for 0-180 degrees, left otherwise).

Right. But rotating is not good idea when you are using physics system. My question was how to do that using forces and torques only (or even using velocities).

Seems like the OP already has a method for rotating using forces, for keyboard input.

That being the case all you need is a method to make the decision of what virtual key the AI would press?

To make it more sensitive you could scale the desired angular velocity according to the difference in angles (ie 180 degrees = max velocity, 0 degrees = no velocity).