Page 1 of 1

Moving to the mouse

Posted: Sun Feb 14, 2010 5:46 am
by GMer
Is there any way I could get a pawn to follow the mouse cursor?

Re: Moving to the mouse

Posted: Mon Feb 15, 2010 2:12 am
by paradoxnj
Do you mean point and click movement like Diablo or Dungeon Siege?

Re: Moving to the mouse

Posted: Mon Feb 15, 2010 3:43 am
by GMer
Yes, exactly like in those games.

Re: Moving to the mouse

Posted: Tue Feb 16, 2010 8:12 pm
by paradoxnj
I'm not sure that RF can do this. QoD???

Re: Moving to the mouse

Posted: Tue Feb 16, 2010 8:17 pm
by zany_001
I know that RF can have the cursor changed so that it's not fixed to the center, lemme find the topic...

Hmm, best I can find is SCURF: viewtopic.php?f=11&t=2071 but i'm sure there was a better topic about RTS games.

Re: Moving to the mouse

Posted: Tue Feb 16, 2010 10:56 pm
by GMer
All well, if all else fails I can learn blender and Unity 3d.

*edit*
On second thought, I could go the route of Halo Wars, with the use of a crosshair in the center of the screenie to select troups.

Re: Moving to the mouse

Posted: Wed Feb 17, 2010 12:24 am
by zany_001
Don't worry, it's possible. Just wait for someone who knows how to post.

Re: Moving to the mouse

Posted: Wed Feb 17, 2010 12:34 am
by GMer
:D

Re: Moving to the mouse

Posted: Wed Feb 17, 2010 10:31 pm
by Juutis
There are (at least) two ways to do this. In theory it may sound simple, but actually it's pretty complicated to implement. Both methods require a pawn positioned at the camera:

1.) Shoot a pawn from the camera to the spot where the mouse is pointing. When the pawn collides with a wall, that's the spot where the mouse was clicked and you can easily get its coordinates from the pawn's coordinates.

2.) Use the pawn at the camera to measure the distance to the point where the mouse was clicked (GetCollideDistance), and then calculate the coordinates of that point.

And then the challenging part: you have to find a way to transform the 2D-coordinates of the mouse (X,Y) to an angle (yaw, pitch). For example, if the mouse is at (100,200) and you click, you have to somehow figure out which direction the "projectile pawn" should be launched at (or where exactly you should aim the GetCollideDistance method). Needless to say, this all involves a lot of trigonometry.

You're gonna have to find out how the field of view system works in RF. AFAIK, the default camera FOV is 2. That is in radians, I believe. So that would mean the angle between a line that goes straight through the center of the screen and a point near the edge of the screen would be 1 radian (the angle between two points at the opposite sides of the screen is 2).

I know this is a very shallow and unclear explanation, but it's the best I can do. If you still wanna give it a shot, I can try to draw a picture to demonstrate.

Re: Moving to the mouse

Posted: Wed Feb 17, 2010 10:33 pm
by Allanon
I have never wrote a script for RF but after looking at the script functions it looks like you can use the GetMousePosX() and GetMousePosY() functions to get the position of the cursor. It also looks like you can use the CheckArea() function to see if the pawn is close to the cursor. And you can probably use the Points and Orders functions such as SetTargetPoint() to set a target for the pawn and use the MoveToTarget() function to move the pawn to the target.

Re: Moving to the mouse

Posted: Thu Feb 18, 2010 6:08 pm
by GMer
Ok, I don't have the right terminology, but I think that the general idea is there (time to look at the RF manual):

(Mouse position is arranged so it is relative to the center)

Angle 1 = (((180/pi)*1)/((screen_resolution_x/2)/mouse_x+0.001)))
Angle 2 = (((180/pi)*0.75)/((screen_resolution_y/2)/mouse_y+0.001)))

About the numbers
180/pi - convert from radians to degrees
1 - screen is 2 radians "wide", so half is 1
0.75 - the screen 1.5 radians "tall", so half is 0.75 (correct me if I'm wrong)
2 - divide the screen in half
0.001 - make sure that the angle rational, as one cannot divide by 0

I'm going to slash out the script tonight (hopefully), where a projectile (using the built in system) will fire from the camera, towards the mouse, but move through 3d space.

Re: Moving to the mouse

Posted: Thu Feb 18, 2010 10:49 pm
by paradoxnj
Here is code to detect collision with the floor of the world and get you the exact point in world space. Camera should be rotated 45 degrees on the X and Y axis. The ceiling needs to be defined as sky for the ray collision to work. You don't need a skybox texture.

Code: Select all

POINT MousePos;
GetCursorPos(&MousePos);
GE_Collision MouseRayCollision;
geVec3d dir;
geVec3d start, end;
start = CameraBaseForm.Translation;

geCamera_ScreenPointToWorld (PrimaryCamera, MousePos.x, MousePos.y, &dir);

geVec3d_AddScaled (&start, &dir, 10000.0f, &end);

BOOL Result = geWorld_Collision (CurrentLevel, NULL, NULL, &start, &end, GE_CONTENTS_CANNOT_OCCUPY, GE_COLLIDE_ALL, 0xffffffff, NULL, NULL, &MouseRayCollision);