turning an object towards the mouse pointer

Posted By: jonkuhl

turning an object towards the mouse pointer - 02/12/08 19:21

Hello there. I'm an old user who recently got back to the program. Due to money issues, I'm still using A6.

Now the issue I have is I have a tank that moves with wsad and a turret that is suppose to move with the mouse. However, with the code I have the turret only rotates to the positive y axis and doesn't follow the mouse very closely.

Code:
 

function mouse_trace()
{
from.X = MOUSE_POS.X; //set from.x to mouse_pos.x
from.Y = MOUSE_POS.Y; //set from.y to mouse_pos.y
from.Z = 0; //set from.z to zero
vec_set(to,from); //copy from to to
vec_for_screen(from,camera); //convert from to 3d coords
to.Z = 3000; //set to.z to 3000
vec_for_screen(to,camera); //convert to to 3d coords
return(trace(from,to)); //return the distance between from and to
}

action tankTurret
{
turret = my;
while(!player){ wait(1);} //wait for the player entity to load before doing anything
while(player) //once the player exists
{
my.x = player.x; //follow player
my.y = player.y;
my.z = player.z;
target = mouse_trace();
vec_diff(target,target,my.x);
vec_to_angle(my.pan,target); //turn to mouse
if((my.tilt != 0) || (my.roll != 0)) { my.tilt = 0; my.roll = 0;} //stay aligned with horizontal plane
wait(1);
}
}




That is the current code. It follows the body of the tank (player) fine. And the line of code that keeps it from rotating on roll and tilt seems to have no effect on how it turns to the mouse (I commented it out to see if it was interfering, no change)

And just a side note, mouse_trace() isn't strictly mine, it came from the forums about a year ago. I've used it in other projects and I know it works.

So the two issues are that (1) it only rotates to point to the left (even when I drive and turn the model around (2) it doesn't follow the mouse closely.

Is trace() too slow for this sort of thing? Is there a better way to do this?
Posted By: jonkuhl

Re: turning an object towards the mouse pointer - 02/12/08 20:07

Ha ha I found the solution.

I looked more closely at mouse_trace() and found it returns a distance, not a vector so instead I used the to[3] vector to rotate the cannon and it works.

Code:

action tankTurret
{
turret = my;
while(!player){ wait(1);} //wait for the player entity to load before doing anything
while(player) //once the player exists
{
my.x = player.x; //follow player
my.y = player.y;
my.z = player.z;
mouse_trace();
vec_diff(to,to,my.x);
vec_to_angle(my.pan,to);
if((my.tilt != 0) || (my.roll != 0)) { my.tilt = 0; my.roll = 0;} //stay aligned with horizontal plane
wait(1);
}
}




no changes were made to mouse_trace();
© 2024 lite-C Forums