Find Direction in 3D World

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
ansu832001
Posts: 95
Joined: Thu Mar 01, 2007 6:39 am

Find Direction in 3D World

Post by ansu832001 »

Hi All,

Can anybody help me in finding the direction in 3D world based on coordinates in 2D world.

Suppose i have (x1,y1) & (x2,y2) can i find the direction.i.e on mousemove i get (x1,y1) & (x2,y2),now i have to find whether my mouse moved in X , Y or Z direction in 3D World.

Best Regards.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

So you've got your mouse position in screen coordinates right? And you want to know if your mouse moved x, y or z in the 3d world? Well it obviously can't move into and out of the screen so it's still just a 2d movement really... How are you wanting to map the mouse movement to 3d movement?
Image Image Image
Strong99
Admin
Posts: 687
Joined: Fri Mar 31, 2006 7:06 pm
Location: Netherlands
Contact:

Post by Strong99 »

think best way is to use a maya sort of camera, the mouse is for the rotation and the scroll wheel for zoom and such
ansu832001
Posts: 95
Joined: Thu Mar 01, 2007 6:39 am

Post by ansu832001 »

Please Look at the Pic below

http://allyoucanupload.webshots.com/v/2 ... 5920073075

Hope it is Clear.

Best Regards.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

I'm afraid that image doesn't make it any clearer for me! Makes it less clear i think as it doesn't seem to be what i was thinking! :lol:

Now it looks like you want your 2D mouse movement to be transformed based on the rotation of the object on screen? So if you push forward and the object isn't rotated then it moves forward but if you push forward and the object is rotated then it moves diagonally according to its own rotating so basically it moves according to its own local coordinate system. Is that right?
Image Image Image
ansu832001
Posts: 95
Joined: Thu Mar 01, 2007 6:39 am

Post by ansu832001 »

Yes Exactly JP,you got it right.Now iam bit confused as how to achieve it.

You can look at IrrEdit,there they are makeing transformations,iam trying to acheieve that.

Note : For those who hasn't used IrrEdit.Once we open IrrEdit a Cube
will appear and when one clicks it three axes are shown for
transformation.

Best Regards.
ansu832001
Posts: 95
Joined: Thu Mar 01, 2007 6:39 am

Post by ansu832001 »

No Help :(
sfncook
Posts: 36
Joined: Sun Jul 01, 2007 6:32 pm

Post by sfncook »

I had to do something similar for my game. I can't remember exactly which object it is (check the API - I'm at work right now spending company money writing this message otherwise i would look it up for you) but something like the video driver or irrlicht device or something will return a ray directly into the screen starting from the mouse coords. You use this to intersect it with a plane in the 3d environment and you have your 3d coords. If this seems interesting to you I'll post my code when I get home tonight.

-S
FlyingIsFun1217
Posts: 219
Joined: Fri Apr 13, 2007 8:29 pm
Location: Illinois
Contact:

Post by FlyingIsFun1217 »

Haven't read through all of what you have written, but I know that the Tiled Terrain Scene node by Arras has some way of finding the degree's you are facing (0-359), so you might want to check that out.

FlyingIsFun1217
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

I think the following should do something like what you want. I haven't tested it, but it seems like it should work.

Note: fixed some typos.

Code: Select all

static const core::vector3df up(0, 1, 0); 
static const core::vector3df dn(0,-1, 0); 

// ray from camera through mouse cursor 
const core::position2di mouse(event.MouseInput.X, event.MouseInput.Y);
const core::line3df ray = SceneManager->
  getSceneCollisionManager()->getRayFromScreenCoordinates(mouse); 

// the current position of the scene node. might want to use 
// center of bounding box in world space instead. 
const core::vector3df pos = Selected->getAbsolutePosition(); 
const core::vector3df dir = (ray.end - ray.start).normalize();

core::vector3df out; 
if (core::plane3df(pos, up).getIntersectionWithLine(ray.start, dir, out))
{
  moveSceneNodeToWorldPosition(Selected, out);
}
else 
if (core::plane3df(pos, dn).getIntersectionWithLine(ray.start, dir, out))
{ 
  moveSceneNodeToWorldPosition(Selected, out); 
}

Code: Select all

void moveSceneNodeToWorldPosition(scene::ISceneNode* node, const core::vector3df& pos)
{
  core::vector3df npos(pos);

  scene::ISceneNode* parent = node->getParent();
  if (parent) {
    const core::matrix4 m(parent->getAbsoluteTransformation(),
                          core::matrix4::EM4CONST_INVERSE);
    m.transformVect(npos);
  }

  node->setPosition(npos);
}
Travis
Last edited by vitek on Tue Aug 28, 2007 6:03 pm, edited 1 time in total.
ansu832001
Posts: 95
Joined: Thu Mar 01, 2007 6:39 am

Post by ansu832001 »

Hi All,thanks for your reply,

Vitek,can you plz explain the code you have given and sorry for this,did you look @ irrEdit Transformation.Iam sorry if you are offended but iam not finding words to express .

Best Regards.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

The code basically does what is described by the previous poster. It moves the object around on a plane that is intersected with a ray from the mouse cursor. Basically the node is moved to a point under the cursor. I just realized that is _not_ what you are actually looking for though.

You want some sort of 'move mode' selection, and then you want it so that the node moves to its left/right if you move the mouse left/right and the move mode is left/right, and fore/back if the mode is fore/back. Something more like this should work more like irrEdit. Again, I haven't had a chance to test it, but it looks like it should work.

Code: Select all

// this could go in event receiver or main loop. if you put it in event
// receiver with mouse move messages, you don't need the cursor.
core::position2d<s32> newMousePos = CursorControl->getPosition();
core::vector2d<s32> deltaMousePos(newMousePos.X - OldMousePos.X,
                                  newMousePos.Y - OldMousePos.Y);
OldMousePos = newMousePos;

// you need to have some way to identify if you are moving up or down.
// i'm pretending there is an enum that is set for this purpose.
core::vector3df offset(0, 0, 0);
switch (MovingDirection)
{
  case up_or_down:
    offset.Y = deltaMousePos.Y * .2;
    break;
  case left_or_right:
    offset.X = deltaMousePos.X * .2;
    break;
  case fore_or_back:
    offset.Z = deltaMousePos.X * .2;
    break;
}

// attempt to duplicate buggy irrEdit behavior.
offset += Selected->getPosition();
Selected->setPosition(offset);
IMO, this behavior is wrong becaue if the parent node is rotated, then a child node won't move in the direction that the arrow is pointing. Test this out yourself.

Open up the default scene in irrEdit. Add another cube. Using the scene graph explorer, make the new cube a child of the existing one. Select the original cube and rotate it 45 degrees on any axis. Now move the child node...

Note that the arrows are always aligned with the world axes, but the node moves according to the parent coordinate system. i.e. if you rotate the parent node 45 degrees on Y, and then click the red arrow to move it, the node won't move parallel to the red arrow. If you rotate the child, it won't move parallel to its red arrow or even in a direction that lines up with its physical orientation. It always moves in a direction that is in line with its parent.
ansu832001
Posts: 95
Joined: Thu Mar 01, 2007 6:39 am

Post by ansu832001 »

Hi Vitek,Thanks for your concern,but iam sorry i did not understand from your above post.

My Scenario :

I load a Mesh(it is independent,no parent is there for this).
Now when i click the mesh i show up three Axes (these Axes are also independent)and try to move the mesh based on the direction of my Mouse,suppose i move my mouse in X-Direction i Change my Mesh.position.X,but the problem is when i rotate my camera by 90 degrees(think camera is your eye) my X-Axis is nothing but Z-Axis before moving by 90 Degrees,now if i move my mouse in X-Direction by Holding the now X-axis(which is seen as Z-Axis initially) it should effect my Mesh.Position.Z but not Mesh.Position.X.

I hope iam Clear though my post seem Zig-Zag.

Best Regards
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Say the camera is at (0, 40, 40) and a cube is at (0, 0, 0) [the camera is behind and above the cube]. What happens when the cube is clicked and the mouse is pushed forward? Does the cube move toward (0, 0, 100) [i.e straight away from the camera, but on a plane so that it doesn't go up or down?] If this is the case, then how do you make the cube move up?

Or do you have this setup so that the camera can only look at a scene node from the 3 different views, side, front and top?

If you always want the node to move based on the camera orientation, you should use the camera orientation vectors to determine which direction to move the node.

Code: Select all

const core::vector3df cam = camera->getAbsolutePosition();
const core::vector3df target = camera->getTarget();
const core::vector3df up = camera->getUpVector();

// this vector points from the camera toward the target
core::vector3df view = (target - cam).normalize();

// this vector points out the side of the camera, orthogonal to view, and Y
core::vector3df X = up.crossProduct(view);

// this points up out of the camera, orthogonal to other vectors
core::vector3df Y = X.crossProduct(view);
You should be able to use X and Y to move your scene node. Remember that those vectors are in world coordinates.

Travis
Last edited by vitek on Wed Aug 29, 2007 6:56 am, edited 1 time in total.
ansu832001
Posts: 95
Joined: Thu Mar 01, 2007 6:39 am

Post by ansu832001 »

Hi Vitek,

What is horizontal.Crossproduct(view),i don't find horizontal anywhere in your code and can you please elaborate as how we can use those X and Y to get all three X-Y-Z Axes to get effected,iam sorry ,iam little new to 3D.

Best Regards.
Post Reply