I don't know your code, but the approach itself definitely works fine. Have a look at this modified mouse following example. Maybe it helps:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.threed.jpct.*;
import com.threed.jpct.util.*;
public class MouseFollower extends JFrame implements MouseMotionListener {
private final static long serialVersionUID=1L;
private Graphics g = null;
private FrameBuffer fb = null;
private World world = null;
private Object3D plane = null;
private Object3D cube = null;
private boolean doloop = true;
private int x=320;
private int y=240;
private SimpleVector target=new SimpleVector();
public MouseFollower() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setSize(640, 480);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
this.addMouseMotionListener(this);
g = getGraphics();
}
public void mouseMoved(MouseEvent m) {
x=m.getX();
y=m.getY();
}
public void mouseDragged(MouseEvent m) {}
private void initStuff() {
fb = new FrameBuffer(640, 480, FrameBuffer.SAMPLINGMODE_NORMAL);
world = new World();
fb.enableRenderer(IRenderer.RENDERER_SOFTWARE);
plane = Primitives.getPlane(1, 400);
plane.rotateX((float) Math.PI / 2f);
// Not a cube anymore, but a cone...
cube = Primitives.getCone(15);
cube.rotateZ((float)Math.PI/2f);
cube.rotateY((float)Math.PI/2f);
cube.rotateMesh(); // Make the cone a pointer
cube.setRotationMatrix(new Matrix());
plane.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
plane.setVisibility(false);
world.addObject(plane);
world.addObject(cube);
cube.translate(-50, -10, -50);
world.setAmbientLight(100, 100, 100);
Light light = new Light(world);
light.setPosition(new SimpleVector(0, -80, 0));
light.setIntensity(150, 150, 150);
world.buildAllObjects();
target=cube.getTransformedCenter();
}
private void follow() {
SimpleVector ray=Interact2D.reproject2D3D(world.getCamera(), fb, x, y);
if (ray!=null) {
SimpleVector norm=ray.normalize();
Matrix mat=world.getCamera().getBack();
mat=mat.invert3x3();
norm.matMul(mat);
SimpleVector ws=new SimpleVector(ray);
ws.matMul(world.getCamera().getBack().invert3x3());
ws.add(world.getCamera().getPosition());
plane.setVisibility(true);
float f=world.calcMinDistance(world.getCamera().getPosition(), norm, 1000);
plane.setVisibility(false);
if (f!=Object3D.COLLISION_NONE) {
norm.scalarMul(f);
SimpleVector mn=new SimpleVector(norm);
mn.add(world.getCamera().getPosition());
target=new SimpleVector(mn);
}
}
SimpleVector m=new SimpleVector(target);
m=m.calcSub(cube.getTransformedCenter());
if (m.length()>0.5f) {
cube.translate(m.normalize());
cube.setRotationMatrix(m.getRotationMatrix());
}
}
private void doIt() throws Exception {
Camera cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 100);
cam.moveCamera(Camera.CAMERA_MOVEUP, 160);
cam.lookAt(plane.getTransformedCenter());
world.getLights().setOverbrightLighting(Lights.OVERBRIGHT_LIGHTING_DISABLED);
while(doloop) {
follow();
fb.clear();
world.renderScene(fb);
world.draw(fb);
fb.update();
fb.display(g);
Thread.sleep(10);
}
fb.disableRenderer(IRenderer.RENDERER_OPENGL);
System.exit(0);
}
public static void main(String[] args) throws Exception {
MouseFollower cd = new MouseFollower();
cd.initStuff();
cd.doIt();
}
}