Finding a point in 3D and rotating towards it

Started by
1 comment, last by Etus 21 years, 5 months ago
Hey, Iv''e got a problem. Consider this picture: http://www.demons.org.il/etus/images/car3d1.jpg I want to do two things: First of all, I want to find the coord of the point - to do that, I want that the mouse cursor will also be able to move along the Z axis - how do I do that? After the click had been made, Iv''e been given the coords of the point from the mouse. What I want to do now, is to rotate the car so it''ll face that point. Like this: http://www.demons.org.il/etus/images/car3d2.jpg How can I do that(and the mouse thing)? Thanks for your help(and sorry about the big pictures), Yuval
Advertisement
Hi Etus

I am not to sure about the mouse thing, but I can help explain the technique in rotating your car toward the point you selected with the mouse.

The following code use functions from Power Render.

Here I am setting up my right vector which is all your car needs
The PR_Globals.Tvector is a user controled vector. What the PR_Transform function does is simply rotates my initial vector using the 3D objects matrix which is Pursuer->orientation.rot_mat. usually the matrix values change when I rotate the object such as your car. The new tranformed vector are stored in Rht1 point structure. You can also extract the each vector such as the forward, up, and right vectors from the objects matrix.

PR_Globals.tvector[0]=-1;PR_Globals.tvector[1]=0;PR_Globals.tvector[2]=0;
PR_Transform(Pursuer->orientation.rot_mat);
rht1.x = PR_Globals.tvector[0];
rht1.y = PR_Globals.tvector[1];
rht1.z = PR_Globals.tvector[2];

The location structure is my position in world space
The next function calculates the distance from your car to the X or target show in your picture.
Dist_To_Target = PR_Distance3D(Pursuer->orientation.location.x, Pursuer->orientation.location.y
, Pursuer->orientation.location.z , Target->orientation.location.x,
Target->orientation.location.y, Target->orientation.location.z);

Here I am calculating a normalize direction vector which is n
n.x = Target->orientation.location.x - Pursuer->orientation.location.x/Dist_To_Target;

n.y = Target->orientation.location.y - Pursuer->orientation.location.y/Dist_To_Target;

n.z = Target->orientation.location.z - Pursuer->orientation.location.z/Dist_To_Target;

Again I am using a PowerRender function of taking the dot product of my direction vector n and the Objects right vector

doty = PR_DotVect (&rht1, &n);

The following routine acts as a switch to change rotation direction of your car.

//Yaw Tracking
if(doty < 0)
yaw = -1;
else
yaw = 1;

Here is another Power Render function for getting the absolute value of a number in other words my number will always be positive.
doty = PRMATH_fabs(doty);

Here is the last equation before rotating your car you have the YawRate that is how fast your car will turn the yaw switch which determines the rotating direction and last the doty value that used to make the rotation transition smooth.

yaw_mod = YawRate*yaw*doty;

And finally the function used to rotate your car
PR_RotateEntity(Pursuer, 0, yaw_mod, 0);

The following is some Power Render code for transforming the objects initial velocity vector Fwd_Rev_vel by thePursuer->orientation.rot_mat maxtrix based the the previous rotation .

PR_Globals.tvector[0] = 0;
PR_Globals.tvector[1] = 0;
PR_Globals.tvector[2] = Fwd_Rev_vel;
PR_Transform(Pursuer->orientation.rot_mat);

The following function simply translate the objects position by and incremental value per frame.
PR_MoveEntity(Move,PR_Globals.tvector[0], PR_Globals.tvector[1], PR_Globals.tvector[2]);

Please understand the above code is based on the Power Render Graphics Engine syntax which I am using to explain the technique of rotating your car toward the point and travling to that point. I sure theres an equivalent set of functions in Directx , OpenGL or what ever engine your using to accomplish the same tasks.

This code is simplified version of a tracking algorithm that is set up for all three axis and alows the pursuing craft to move to the target on an intercept course as well as roll when the target craft is rolling.

Doug

[edited by - sovereign on November 4, 2002 12:01:35 AM]

[edited by - sovereign on November 4, 2002 12:02:39 AM]

[edited by - sovereign on November 4, 2002 12:03:20 AM]
Thanks a lot, I will try it out.
By the way, I''m using OpenGL.

- Yuval

This topic is closed to new replies.

Advertisement