Dragging objects

Started by culli, April 30, 2006, 10:35:06 PM

Previous topic - Next topic

culli

Hi, I've been lurking on here for a while and toying with things.  I really like the way JPCT feels.  

Now I'm trying some ideas for a game where part of it is an RTS (starcraft-style) where you're looking down at a terrain and can drag objects around.  To simplify the idea, I'm treating it like checkers.  I want to click and drag a piece from one position to another.  There have been some great things in the forums that have helped with other problems I've had, but the one I can't figure out right now is that the piece doesn't "keep up" with the mouse cursor.  I can scalarMul the ray to help the problem some, but I want it to keep up exactly with the mouse.

Can't say my vector math skills are more than beginner, but I'm reading some books and learning.

Here's what I've got right now in the "mousedragged" event:

(lastPiece is selected on mouse moves)



SimpleVector currentRay = Interact2D.reproject2D3D(theWorld.getCamera(), buffer, e.getX(), e.getY());
               
currentRay.normalize();
//deal with the camera space
Matrix orient2 = theWorld.getCamera().getFront();
float[] dump2 = orient2.getDump();
SimpleVector ray2 = new SimpleVector();
ray2.x = dump2[0] * currentRay.x + dump2[1] * currentRay.y + dump2[2] * currentRay.z + dump2[3] * 1;
ray2.y = dump2[4] * currentRay.x + dump2[5] * currentRay.y + dump2[6] * currentRay.z + dump2[7] * 1;
ray2.z = dump2[8] * currentRay.x + dump2[9] * currentRay.y + dump2[10] * currentRay.z + dump2[11] * 1;
               
ray2.y = lastPiece.getOrigin().y;
ray2.scalarMul(50);
lastPiece.setOrigin(ray2);
lastX = e.getX();
lastY = e.getY();


What am I doing wrong?

Thanks,
Jim

EgonOlsen

The problem with your approach is, that you are ignoring perspective distortion. Extrapolation of the ray in a linear manner is correct in 3D but not when you are actually trying to do it 2D. Do you know the Z-coordinate on the target position? If so, you may use Interact2D.reproject2D3D(...) to get the actual position. If not, you may cast a ray from the mouse to the terrain and use Object3D.calcMinDistance(..) to get the actual position where this ray hits the terrain. Creating this ray involves a little more matrix math...

culli

:idea:  :idea:  :idea:

Ok, I have this working, thanks to Egon, Andrei, and others.

The problems with my code were:

It was generally disorganized.  (it still is, but less now)

The mesh I'm sticking my pieces to was upside-down.  Rotating it +PI/2 instead of -PI/2 fixed that. :oops:

I was abusing translateMesh().  I shouldn't have been using it at all, at least for most things. :roll:

Andrei's code http://www.jpct.net/forum/viewtopic.php?t=226 for getting an absolute position on a surface worked beatifully on a primitive plane, which is when I discovered that things were upside down.  :D

Thanks guys!

Now I can move on to be confused about other things.

Jim