Welcome Guest [Log In] [Register]
We hope you enjoy your visit.


You're currently viewing the Ultimate 3D Community as a guest. This means that you can only read posts, but can not create posts or topics by yourself. To be able to post you need to register. Then you can participate in the community active and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.

Join our community!

If you are already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Model Rotation Towards Mouse; How would I do it?
Topic Started: Sep 24 2007, 08:47 AM (512 Views)
jetjayjay
Stupidity is a crime.
[ *  * ]
Well, as stated in the title, I need to know how I would rotate a model to face the mouse, in a third-person view, as shown below:


|'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''|
|'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''|
|'''''''''''''''''''''''''''''' :ph43r: ''''"""'''''''''''''''''''''''''''|
|'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''|
|'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''|
|'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''|
|'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''|
|'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''|
|'''''''''''''''''''''''''''''' :medieval: '''''''''''''''''"""''''''|
|'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''|

:medieval: = Player
:ph43r: = Mouse

I need to do so in a way which will have the player's back facing the screen.
The model should be able to rotate among both the X and the Y axis.
For example: if the mouse is above the player (see diagram above) the model will be rotated upwards, and if the mouse is to the left of the player, the model will rotate to the left.
Note: I do NOT mean this the way it is shown in Dr Best's collision example with the wolf. In this way, the mouse position is NOT set, and the model is rotated to face the mouse exactly.
My dream is to become a cheesemonger.
Yes, you heard me. A cheesemonger.
What is a cheesemonger, you ask?
Well, if a butcher cuts the meat, and the fishmonger cuts the fish, I think you can imagine what the cheesemonger does.
Offline Profile Quote Post Goto Top
 
Haunebu
Member Avatar
WW2 games developer
[ *  *  * ]
I'd use rotx...rotz=camera.rotx...rotz and set camera position right behid the character.
Or you can find the point of camera ray impact to the terrain or what and make the character look in that direction.
Posted Image
I don't like american heroes.
Offline Profile Quote Post Goto Top
 
jetjayjay
Stupidity is a crime.
[ *  * ]
Haunebu
Sep 24 2007, 02:27 PM
I'd use rotx...rotz=camera.rotx...rotz and set camera position right behid the character.
Or you can find the point of camera ray impact to the terrain or what and make the character look in that direction.

I probably didn't explain very well - what I meant was, that unlike in, say, Dr Best's collision example, I don't want the mouse to control the direction of the camera. Like this:
-----There is a robot. Let us call him Robot. (Creative, huh?) Robot is made of two parts: the head and the rest of the body. Now, Body moves around based on the arrow keys, turning left with Left, right with Right, and going forwards with Up. He can only turn on left and right, not up or down. The camera turn based on the direction he is moving in.
-----Now for the Head. The mouse is not limited to one position, like in the collision example of Dr Best. It can move around within the limits of the screen, which are based on the direction of the body. In other words, it has no control over the direction Body is going in. Now, the Head model faces the direction of the Mouse, so that the model can be rotated among the X and Y axis to face the mouse.

Hope you understood.
I didn't.
My dream is to become a cheesemonger.
Yes, you heard me. A cheesemonger.
What is a cheesemonger, you ask?
Well, if a butcher cuts the meat, and the fishmonger cuts the fish, I think you can imagine what the cheesemonger does.
Offline Profile Quote Post Goto Top
 
Ruud v A
Member Avatar
Programmer ˇ Artist
[ *  *  *  *  * ]
I think he means something like pressing R twice in Blender.

Veniogames
Vēnit, ut mē occidĕret.

I will not use Ultimate3d 2.x.x anymore - I am an Ogre C++ programmer.
Offline Profile Quote Post Goto Top
 
MysteriXYZ
Member Avatar
Master Matrix Masher
[ *  *  *  *  *  * ]
So what you want, is to make the robot head look at a point in space, represented by the position of the mouse cursor, right? For the solution you need 2 things: first, you have to convert the screen coordinates of the mouse into 3D-coordinates, and second, you need to create a vector which points from the pivot of the robot head to that point in space. From that vector, you can derive the needed rotx and roty to rotate the head.

So let's get to it.
1) The point being looked at in space
For this, you use ScreenCoordToVector(...). This function returns a unit-length vector, pointing at whatever it is that the robot is looking at. But because its length is a bit short (exactly 1), the head would most likely be looking at a point behind the robot all the time. To remedy this, you need to scale the vector up with a value that gives the best results using CalculateVectorScalarProduct(...). This value depends on the distance of the robot from the camera, so you'll need to experiment.

2) The vector defining the direction of the robot head
Now you can create the vector between the coordinates of the head and the coordinates of the point being looked at, using CreateVector(...).
From this vector you can derive the rotation angles rotx and roty for the head, using my self-made GetOrientation(...) script:
Code:
 
//GetOrientation(OutputVector,NewXDir,NewYDir)

//This function finds the new rotx, roty and (optionally) rotz, corresponding
//to the provided vectors NewXDir and NewYDir, which were originally pointing
//in the positive X/Y-direction, before one or more transformations were
//applied to them.
//If NewXDir is negative, the new rotz is NOT calculated and set to zero.
//The new angles are stored in OutputVector (rotx,roty,rotz). If OutputVector
//is negative, a new vector is created and its ID gets returned. Never forget
//to call ReleaseVector for every vector you create :P.

var OutputVect,NewXDir,NewYDir,NormXDir,NormYDir,RotX,RotY,RotZ,RotMatr;

OutputVect=argument0;
NewXDir=argument1;
NewYDir=argument2;

//normalize NewXDir (if used) and NewYDir
if (NewXDir > 0)
  NormXDir=CalculateVectorScalarProduct(-1,NewXDir,1/CalculateVectorLength(NewXDir));
NormYDir=CalculateVectorScalarProduct(-1,NewYDir,1/CalculateVectorLength(NewYDir));
RotY=radtodeg(arctan2(GetVector(NormYDir,1),GetVector(NormYDir,2)));
RotMatr=CreateRotationMatrix(-1,0,-RotY,0);
if (NewXDir > 0)
  TransformVector(NormXDir,NormXDir,RotMatr);
TransformVector(NormYDir,NormYDir,RotMatr);
RotX=radtodeg(arctan2(-GetVector(NormYDir,3),GetVector(NormYDir,2)));
if (NewXDir > 0)
{
  CreateRotationMatrix(RotMatr,-RotX,0,0);
  TransformVector(NormXDir,NormXDir,RotMatr);
  RotZ=radtodeg(arctan2(GetVector(NormXDir,3),GetVector(NormXDir,1)));
}
else RotZ=0;

ReleaseMatrix(RotMatr);
if (NewXDir > 0)
  ReleaseVector(NormXDir);
ReleaseVector(NormYDir);

if (OutputVect < 0)
  return CreateVector(-1,RotX,RotY,RotZ);
else CreateVector(OutputVect,RotX,RotY,RotZ);


And here is the main code, called in the Step Event of the Head object:
Code:
 
var LookAtPoint,X,Y,Z,ViewDirection,HeadOrientation;

//1) define the vector from the camera to the point in space
//that the robot head is looking at
LookAtPoint=ScreenCoordToVector(-1,mouse_x,mouse_y);
//use a value "Distance" to scale up this vector so it points
//at a location in front of the robot
CalculateVectorScalarProduct(LookAtPoint,LookAtPoint,Distance);

//2) create the vector from the head to the point being looked at
X=GetVector(LookAtPoint,1)+camera.x-x;
Y=GetVector(LookAtPoint,2)+camera.y-y;
Z=GetVector(LookAtPoint,3)+camera.z+camera.height-z;
ViewDirection=CreateVector(-1,X,Y,Z);
//retrieve the rotx/y angles corresponding to the direction
//of this vector and store them in another vector that
//represents the orientation of the head
HeadOrientation=GetOrientation(-1,-1,ViewDirection);
rotx=GetVector(HeadOrientation,1);
roty=GetVector(HeadOrientation,2);

ReleaseVector(LookAtPoint);
ReleaseVector(ViewDirection);
ReleaseVector(HeadOrientation);


Simple, isn't it ;)? At least I hope it solves your problem :).
U3D is like candy; after extensive consumption, it's Best to brush.
Offline Profile Quote Post Goto Top
 
jetjayjay
Stupidity is a crime.
[ *  * ]
Wow... lol... you´re smart! I didn´t understand a thing, but it worked really well! Thank you MysteriXYZ!
:D :D :D :D
My dream is to become a cheesemonger.
Yes, you heard me. A cheesemonger.
What is a cheesemonger, you ask?
Well, if a butcher cuts the meat, and the fishmonger cuts the fish, I think you can imagine what the cheesemonger does.
Offline Profile Quote Post Goto Top
 
« Previous Topic · Questions about Ultimate 3D · Next Topic »
Add Reply