mouse_pos3D

Posted By: rtsgamer706

mouse_pos3D - 09/29/13 14:29

Hi, I am making a top down game with twin stick type controls. I am using the following lines to make the player character look at the mouse cursor:
Code:
while (my)
	{
	   vec_to_angle (my.pan,mouse_pos3d);
   	   my.tilt = 0;
        }


It works fine except for the fact that once I move from x=0, y=0 it doesn't work any more.
I think it's because the command I'm using checks the cursor's position in relation to the origin.
But I don't understand it in depth enough to know hwo to fix it.
Is there a way that I can either chose the reference point as something I can move dynamically.
Or is there another command or something that'll fit my needs better?
Thanks
Posted By: rtsgamer706

Re: mouse_pos3D - 09/29/13 18:35

Here's the entire loop, maybe my problem is with how I'm moving the camera or something

Code:
while (my)
	{
		vec_to_angle (my.pan,mouse_pos3d);
   	my.tilt = 0;
   //-----------Movement-----------
   	camera.x = my.x;
   	camera.y = my.y;
   	if (key_w)
	     c_move (me, nullvector, vector(20*time_step, 0, 0), IGNORE_PASSABLE); // move the player forward
	if (key_s)
	     c_move (me, nullvector, vector(-20*time_step, 0, 0), IGNORE_PASSABLE); // move the player forward
   	wait(1);
	}

Posted By: Superku

Re: mouse_pos3D - 09/30/13 06:18

You need to trace from mouse_pos3d to the ground using a scaled vector copy of mouse_dir(3d), then rotate the player to the trace target. Alternatively you can solve the equation of the line mouse_pos3d + mouse_dir*t so that the z-component of the result is the player height (which would be a better approach than using c_trace).
Posted By: rtsgamer706

Re: mouse_pos3D - 09/30/13 17:28

I think I get the idea of what you're saying but I think I need to clarify something.
Even though I'm using the 3D engine the game is completely top down.
So there's no z component that I'm dealing with.
Is there a more direct way of fixing it since I'm not taking a Z component into account?
Posted By: Superku

Re: mouse_pos3D - 09/30/13 23:24

Sure there is a z-component involved. I assume you don't use an ISOMETRIC camera, correct? Then proceed as follows:

Your line is g(f) = mouse_pos3d + f*mouse_dir3d. You are looking for a specific f so that the z-component of g(f) is 0:

0 = mouse_pos3d.z + f*mouse_dir3d.z;
<=>
f = -mouse_pos3d.z/mouse_dir3d.z; // mouse_dir3d.z is != 0 because you are looking down

var f;
VECTOR dir;
vec_set(dir,mouse_dir3d);
vec_scale(dir,10); // depending on the camera height it can be beneficial to scale the dir vector for increased accuracy (because of the limited accuracy of var)
f = -mouse_pos3d.z/dir.z;
vec_scale(dir,f);
vec_add(dir,mouse_pos3d);

dir is now your target on the ground plane. Rotate your player to dir.
Posted By: rtsgamer706

Re: mouse_pos3D - 10/01/13 00:07

hm, the line

f = -mouse_pos3d.z/dir.z;

seems to be giving me an error, it says "System Crash" whenever I run the program.
The fact that I'm dividing there seems to be the cause but I'm not sure why.
Posted By: Superku

Re: mouse_pos3D - 10/01/13 00:53

Your camera is looking downward (camera.tilt = -90 or 270) and you have your mouse activated in the engine?
Posted By: rtsgamer706

Re: mouse_pos3D - 10/01/13 00:58

Camera.tilt = -90
and I have this in main:

while (1)
{
mouse_pos.x = mouse_cursor.x; // allow the mouse pointer to move
mouse_pos.y = mouse_cursor.y; // on the x and y axis
wait (1);
}

plus

mouse mode = 2;

am I forgetting something?
Posted By: Superku

Re: mouse_pos3D - 10/01/13 01:11

Then debug the code (on your own), check critical values before calculation and the like.

Btw. this is how mouse_dir3d behaves:

Code:
void main()
{
	fps_max = 60;
	level_load(NULL);
	camera.z = 512;
	camera.tilt = -90;
	mouse_mode = 4;
	while(1)
	{
		DEBUG_VAR(mouse_dir3d.x,20);
		DEBUG_VAR(mouse_dir3d.y,40);
		DEBUG_VAR(mouse_dir3d.z,60);
		wait(1);
	}
}



You can try to add a "if(mouse_dir3d.z < 0) {...}" line around your mouse target calculation block.
© 2024 lite-C Forums