5 Replies Latest reply on Sep 4, 2011 6:10 PM by 883608

    Making object face mouse

      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
        • 1. Re: Making object face mouse
          Post the code you already have there. The cube drawing and mouse related code.

          I don't understand the "to set rotation to my cube so it is looking at that gorgeous mouse". May be you can provide screenshots or describe it with more details.
          • 2. Re: Making object face mouse
            Ok I will try to provide some code and screens for better understanding.

            import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
            import com.sun.j3d.utils.universe.*;
            import com.sun.j3d.utils.geometry.*;
            import com.sun.j3d.utils.picking.PickCanvas;
            import com.sun.j3d.utils.picking.PickResult;
            import java.awt.Dimension;
            import java.awt.GraphicsConfiguration;
            import java.awt.event.ActionEvent;
            import java.awt.event.ActionListener;
            import java.awt.event.KeyEvent;
            import java.awt.event.KeyListener;
            import java.awt.event.MouseEvent;
            import java.awt.event.MouseListener;
            import java.awt.event.MouseMotionListener;
            import javax.media.j3d.*;
            import javax.swing.JFrame;
            import javax.swing.Timer;
            import javax.vecmath.*;

            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();
            final Vector3d vec = new Vector3d(0,0,2);
            final TransformGroup viewer = universe.getViewingPlatform().getViewPlatformTransform();
            final Transform3D changeview = new Transform3D();
            final Vector3d viewvec = new Vector3d(0.0,0.0,8.0);
            canvas.setSize(new Dimension(200,200));
            final BranchGroup group = new BranchGroup();
            final ColorCube sp = new ColorCube(0.5);
            canvas.addMouseMotionListener(new MouseMotionListener() {

            public void mouseDragged(MouseEvent e) {

            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))));

            setSize(new Dimension(200,200));

            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|http://ScrnSht.com/scnper]

            Edited by: 880605 on 22.8.2011 9:54
            • 3. Re: Making object face mouse
              1. Use code-tags

              2. This is not compilable due to missing 3rd party libraries!

              3. Please describe the current behavior and in which way it failes your expectation.

              • 4. Re: Making object face mouse

                Well what it does is that it just turns around without intentionaly facing my mouse cursor. As I said, it was just a test so I didn't even thought it would work, I was just desperate and was willing to try anything even if I didn't understand what will it do. I thought about trying method LookAt() and it worked in a way, but not in a way I wanted it to work. It made the cube face my mouse cursor, but it was also moving the cube confusingly on other locations each time I moved my mouse. When I tried to make the cube first LookAt() the cursor and then I Translated its location using Vector3d and it was on location I wanted, but just rotated somehow the cube and it was no longer facing my mouse cursor. I guess that moving it by force was not a good idea, since each time LookAt() was called, it Translated the cube's location so it would face the mouse and when I Translated it again with Vector3d, the LookAt() did not recognized it and was still acting like it thought everything was pointed right. I hope you guys get this because I think it is the right path, but I'm willing to try another if you got suggestions.
                • 5. Re: Making object face mouse
                  Resolved. I done it myself.