The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
[Originally posted by resplendent]

Please can you help, I have selected an object with by clicking the mouse and
I want to move it when the user drags the mouse.

I can't figure out how the example code works with all the Mappers etc. and I
was wondering if you could help me with the mapping from the mouse
co-ordinates to world space co-ordinates.

I am trying to move a checker around a board, so I could fix the Y co-ordinate
to a set height if it makes things easier. This is what I tried:

public void mouseDragged(MouseEvent e){
int moveX = e.getX() - xStart; // distance since object first selected
int moveY = e.getY() - yStart; // distance since object first selected

Point3d rayOrg = new Point3d();
// transform to world space
Transform3D _xform = new Transform3D();

but here I got stuck, and had to resort to a very approximate:
Vector3f vec = new Vector3f(((float)(moveX))/100, 0f,

Can anyone post a simpler example showing how to get the correct mouse to
world mapping using a combination of getCenterEyeInImagePlate,
getImagePlateToVworld, getLocalToVworld etc. ?
import-bot (20211) [Avatar] Offline
Re: Moving object with the mouse
[Originally posted by author]

As the book mentions on page 285 there are good reasons to have all that stuff
in the framework. Mappers are probably the most useful parts of it when it
comes to intuitively dragging things around relative to the display (DRM) or
in the virtual world (WRM). Your example starts from the very beginning, with
the 2D mouse position. There are a number of issues that have to be addressed
in going from mouse position to an in-scene drag, with the object appearing to
be "stuck" to the cursor. Rather than re-inventing the wheel right off the
bat, why not take a look at some of the general concepts and components used
by the framework to solve this problem. If you still want to code it all
yourself, you can take a look at the pertinent framework source code and
borrow what you need. I can't help but think that being able to poke and prod
and study a working example of what you want will get you to the end point
faster than "slow-walking" your way through many of the same issues and
problems that others have already solved.

What you want, to drag a checker around a checker board, is the WRM technique.
Take a look at section 19.4 (pg 386). There are three types implemented in
the framework. If you are content to just slide the checkers on the plane of
the checker board and not to also pick them up, then you can use pseudo-WRM,
with the checkerboard plane as the static source plane. The critical notion
in WRM is that you are transforming 2D mouse movement in display space to a
drag position in 3D "source" space, which must then be translated into a local
transform relative to the "target" transform node (actuator), which moves the
checker. Take a look at section 19.2 (pg 376) for more about source to target

benglncy (2) [Avatar] Offline
Re: Moving object with the mouse
Author Posts are irritating (even if they were in 2003)! Don't they know that people need the answer NOW, a company won't generally wiat for you to order a book and wiat for it to come so what is the point in advertising it??

I have just started this task, and this code might help you, this very code is letting me drag the object around, with it pinned to the mouse cursor utterly no matter where you are viewing from. It's improvised, but it will get you started.

This code below takes x,y (a click on the window), and finds the corresponding point on the z=0 plane left in the MousePos variable on the last line.

MouseEvent mouseEvent = (MouseEvent)evt;
int x = mouseEvent.getX();
int y = mouseEvent.getY();
PickCanvas pickCanvas = new PickCanvas((Canvas3D) evt.getSource(), _branchGroup);
Canvas3D canvas = pickCanvas.getCanvas();
Point3d eye_pos = new Point3d();
Point3d mouse_pos = new Point3d();
canvas.getPixelLocationInImagePlate(x, y, mouse_pos);
Transform3D motion = new Transform3D();
Vector3d direction = new Vector3d(mouse_pos);
Vector3d straightDown = new Vector3d(0, 0, -1.0f);
double rayLength =;
double rayamount = mouse_pos.z / rayLength;

double test = direction.length();

benglncy (2) [Avatar] Offline
Re: Moving object with the mouse
I forgot to add that the above code only works looking straight down from the sky, or at least it is only tested looking straight down from the sky.