This discussion is archived
5 Replies Latest reply: Sep 4, 2011 11:10 AM by 883608 RSS

Making object face mouse

883608 Newbie
Currently Being Moderated
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
    StanislavL Pro
    Currently Being Moderated
    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
    883608 Newbie
    Currently Being Moderated
    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();
    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|http://ScrnSht.com/scnper]

    Edited by: 880605 on 22.8.2011 9:54
  • 3. Re: Making object face mouse
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    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.

    bye
    TPD
  • 4. Re: Making object face mouse
    883608 Newbie
    Currently Being Moderated
    EDITED

    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
    883608 Newbie
    Currently Being Moderated
    Resolved. I done it myself.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points