Mouse, aim

Posted By: rtsgamer706

Mouse, aim - 02/12/11 14:30

I am a programmer working with the free version of lite-c, I am attempting to make a tanks game where the camera gives you a top down view and you aim and shoot with the mouse. Does anyone know how I can get the turret to rotate to my mouse?
Thanks for the help
Rtsgamer706
Posted By: rtsgamer706

Re: Mouse, aim - 02/12/11 18:50

I've made progress!
I found something called mouse_dir3d.
So I added:

action b_gun()
{
set(my,PASSABLE);
while(my)
{
vec_for_vertex(me.x, basic, 365);
vec_to_angle(my.pan,mouse_dir3d);
wait(1);
}
}
this works pretty well as the cannon does aim towards my mouse.
The problem now is that the farther away my mouse is from the cannon the higher up it aims!
Does anyone know how to lock the tilt and roll of the cannon or some other way to fix that problem?
I already tried:

my.tilt = clamp(my.tilt,0,0);

and it doesn't work, =(
Thanks for the help
Rtsgamer706
Posted By: xbox

Re: Mouse, aim - 02/12/11 20:51

not saying it will work, but you can try,

my.tilt = clamp(0, my.tilt, 0);

I suggest this because from my thinking,
my.x = my. pan (x is the first element in the position vector, and pan is the first element in the Angle vector)
my.y = my. tilt (y is the second element in the position vector, and tilt is the second element in the angle vector)
my.z = my. roll

just a thought...
Posted By: rtsgamer706

Re: Mouse, aim - 02/13/11 00:02

no luck,
Thanks for the reply though,
any other suggestions?
Posted By: badapple

Re: Mouse, aim - 02/13/11 04:31

this wouldnt be hard . i work with the free lite c myself , and if you dont figure it out ill make something quick for ya
Posted By: rtsgamer706

Re: Mouse, aim - 02/13/11 12:19

You seem to know what your doing,
so can you just tell me how to fix my problem please?
Posted By: Random

Re: Mouse, aim - 02/13/11 12:45

Like this;

Click to reveal..
function mouse_aim()
{
proc_mode=PROC_LATE;
VECTOR weaponOffset;
while(1)
{
if(mouse_right == 1)// hold it to aim.
{
camera.arc = 50; // zoom alittlebit
//set position
////////////////////////////////
vec_set (weaponOffset.x, vector (?, ?, ?));
vec_rotate (weaponOffset.x, vector (camera.pan, camera.tilt, 0));
vec_add (weaponOffset.x, camera.x);
vec_set (my.x, weaponOffset.x);
my.pan = camera.pan;
my.tilt = camera.tilt;
wait(1);
//end of setting the position
////////////////////////////////
}
else //When your not pressing mouse right.
{
camera.arc = 80;
vec_set(my.x,weaponOffset);
vec_rotate(my.x,camera.pan);
vec_add(my.x,camera.x);
vec_set(my.pan,camera.pan);
my.pan = camera.pan;
my.tilt = camera.tilt;
wait(1);
}
}
}


It`s tested and it works.
You just must play with the numbers to set the right position.
Posted By: rtsgamer706

Re: Mouse, aim - 02/13/11 13:16

Your code probably does work but can you be more specific as of what I should do with it please?
When I added it to my original action or just told the gun to run it by itself, both times the gun just disappeared!
Do you want me to put it after my original action, in my original action, should it replace my original action completely?
thanks for the code, but how do I use it???
Rtsgamer706
Posted By: muffel

Re: Mouse, aim - 02/13/11 13:32

with this code you get the nearest position on the terrain under your cursor. this position is stored in the mouse3d_vec vector
Code:
vec_set(temp,mouse_dir3d);
vec_scale(temp,1000); // set a range
vec_add(temp,mouse_pos3d);
c_trace(mouse_pos3d,temp,IGNORE_MODELS|IGNORE_SPRITES);
vec_set( mouse3d_vec , target );



the code is tested
put it in the mainloop
declare temp as a local vector and mouse3d_vec as a global vector

muffel
Posted By: rtsgamer706

Re: Mouse, aim - 02/13/11 13:37

VECTOR mouse3d_vec;

action b_gun()
{
// set(my,PASSABLE);
VECTOR temp;
while(my)
{
my.tilt = clamp(0, my.tilt, 0);
vec_for_vertex(me.x, basic, 365);
vec_to_angle(my.pan,mouse_dir3d);
vec_set(temp,mouse_dir3d);
vec_scale(temp,1000); // set a range
vec_add(temp,mouse_pos3d);
c_trace(mouse_pos3d,temp,IGNORE_MODELS|IGNORE_SPRITES);
vec_set( mouse3d_vec , target );
wait(1);
}
}

This is what that action currently looks like, it has not changed my problem,
(Turret still aims up and down) what did i do wrong?
thanks
rtsgamer706
Posted By: muffel

Re: Mouse, aim - 02/13/11 13:40

The turret has to aim to mouse3d_vec
after turning you have to do the clamping
Code:
action b_gun()
{
  // set(my,PASSABLE);
  VECTOR temp;
  while(my)
  {
    vec_for_vertex(me.x, basic, 365);
    vec_to_angle(my.pan,mouse_dir3d);
    vec_set(temp,mouse_dir3d);
    vec_scale(temp,1000); // set a range
    vec_add(temp,mouse_pos3d);
    c_trace(mouse_pos3d,temp,IGNORE_MODELS|IGNORE_SPRITES);
    vec_set( mouse3d_vec , target );

    vec_diff( temp , me.x , mouse3d_vec );// may need to exchange me.x and mouse3d_vec
    vec_to_angle( my.pan , temp );

    my.tilt = clamp(0, my.tilt, 0);
    wait(1);
  }
}


muffel
Posted By: rtsgamer706

Re: Mouse, aim - 02/16/11 01:10

It works! It works! It works!
kinda...
I had to take out the following lines:

vec_scale(temp,1000); // set a range
vec_add(temp,mouse_pos3d);
c_trace(mouse_pos3d,temp,IGNORE_MODELS|IGNORE_SPRITES);
vec_set( mouse3d_vec , target );

vec_diff( temp , me.x , mouse3d_vec );// may need to exchange me.x and mouse3d_vec
vec_to_angle( my.pan , temp );

but once I removed them it worked!
The gun always looks at my mouse!
Thank you so much for the help!
rtsgamer706
© 2024 lite-C Forums