Skip to main content

Rotating object to face mouse

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
2 replies [Last post]
Baly
Offline
Joined: 2011-08-20

I am very sorry to bother you, but I am really lost here.

I'm simply trying to rotate color cube to face my mouse but I failed every attempt. I spent 4 days looking around internet and google probably doesnt even want to see me typing anything related to mouse for a long time. I would be very happy if you could point me to some path because I tried everything I know, even what I didnt know. I am having MouseMotionListener sensing every movement from my mouse and trying to set rotation to my cube so it is looking at that gorgeous mouse. I lately decided it wont be a bad idea to make a bit simple 3D game, so without this I'm not moving anywhere. Peter

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Baly
Offline
Joined: 2011-08-20

Resolved. I done it myself.

Baly
Offline
Joined: 2011-08-20

Ok I will try to provide some code and screens for better understanding.

public class Java3dTest extends JFrame {

public void Java3d() {
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
final Canvas3D canvas = new Canvas3D(config);
final SimpleUniverse universe = new SimpleUniverse(canvas);
final Transform3D t = new Transform3D();
final TransformGroup tg = new TransformGroup();
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
final Vector3d vec = new Vector3d(0,0,2);
t.setTranslation(vec);
tg.setTransform(t);
universe.getViewingPlatform().setNominalViewingTransform();
final TransformGroup viewer = universe.getViewingPlatform().getViewPlatformTransform();
final Transform3D changeview = new Transform3D();
final Vector3d viewvec = new Vector3d(0.0,0.0,8.0);
changeview.setTranslation(viewvec);
viewer.setTransform(changeview);
canvas.setSize(new Dimension(200,200));
getContentPane().add(canvas);
final BranchGroup group = new BranchGroup();
final ColorCube sp = new ColorCube(0.5);
sp.setName("Object1");
tg.addChild(sp);
group.addChild(tg);
canvas.addMouseMotionListener(new MouseMotionListener() {

@Override
public void mouseDragged(MouseEvent e) {
}

@Override
public void mouseMoved(MouseEvent e) {
Transform3D rotate = new Transform3D();
Point2d pos3 = new Point2d(e.getX(),e.getY());
Point3d pos4 = new Point3d();
canvas.getPixelLocationInImagePlate(e.getX(), e.getY(), pos4);
Point3d pos2 = new Point3d(vec.x,vec.y,vec.z);
Point2d pos = new Point2d();
canvas.getPixelLocationFromImagePlate(pos2, pos);
rotate.setRotation(new AxisAngle4d(0.0,0.0,(pos.distance(pos3)),vec.angle(new Vector3d(pos4.x,pos4.y,vec.z))));
t.mul(rotate);
t.setTranslation(vec);
tg.setTransform(t);
}

});
universe.addBranchGraph(group);
setLayout(null);
setSize(new Dimension(200,200));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args) {
new Java3dTest().Java3d();
}
}

The part with MouseMotionListener probably doesn't make much sense. I was trying everything I could and I don't even understand the AxisAngle4d yet.

What am I trying to do, is every time mouse moves (MouseMotionListener->mouseMoved fires) my "Object1" (The color cube) is rotated to face the mouse cursor (with the same particular side each time).
Cube rotation picture: ScrnSht.com/scnper