# SimpleRTS (In which I get sick of the theoretical)

This topic is 2142 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Most of my programming has been hobbyist-theoretical-prototyping and reading more blogs than is probably healthy. I'm sick of it. I wanna make a game.

I'm going to make a game, using my current game engine.

Thanks.

##### Share on other sites
Good luck! Making a finished game is a great way to achieve personal satisfaction and keep moving forward

##### Share on other sites

Good luck! Making a finished game is a great way to achieve personal satisfaction and keep moving forward

you can bookmark such things?
Then i will to!

##### Share on other sites
you can bookmark such things?
Then i will to![/quote]
I just meant bookmarking the link to the journal on my browser to remind to have a look at it every now and then. I don't know if there is a more advanced "follow" feature on gamedev.

##### Share on other sites
Thanks. Up to this point, most of what I've done (even the games) have been exercises. For example, "What if I wrote a roguelike based on the idea of compositing simple VIs?"

For this one, I'm not going to experiment. Simplest methods all the way.

But first, I have to figure out what's up with my Window class in my engine...

##### Share on other sites
Ok, according to Google, an unproject can be accomplished by multiplying a vector containing the mouse XPos and YPos, plus a depth value for the Vector.Z, by the inverse of your ViewProjection matrix.

That is to say, Transform( Vector3( Mouse.XPos, Mouse.YPos, Depth), Camera.ViewProjection.Inverted() ), to use psuedocode.

Is this a Normal transform, or a Point transform? And what do I use for Depth values to get the rayBegin (I'm trying to intersect with the ground plane) and rayEnd vectors?

Google keeps on wanting to tell me about UN projects when I do a search for unproject, even if I add DirectX to the search.

Thanks.

##### Share on other sites
Hmm...If I multiply by the inverse projection, I get really small vectors that don't reflect object space at all. OTOH, just multiplying by the inverse View isn't fully accurate.
 Vector4 mouseVector = new Vector4(input.Mouse.State.XPos - display.ScreenRectangle.Left, input.Mouse.State.YPos - display.ScreenRectangle.Top, 0F, 1); mouseVector.X = (mouseVector.X - (display.ScreenRectangle.Width / 2)) / 2; mouseVector.Y = (mouseVector.Y - (display.ScreenRectangle.Height / 2)) / 2; Vector4 rayBegin2 = new Vector4(mouseVector.X, -mouseVector.Y, 0, mouseVector.W) * scene.GetCamera(mainCameraName).View.Inverted(); Vector4 rayEnd2 = new Vector4(mouseVector.X, -mouseVector.Y, 10000, mouseVector.W) * scene.GetCamera(mainCameraName).View.Inverted(); Vector3 rayBegin = new Vector3(rayBegin2.X / rayBegin2.W, rayBegin2.Y / rayBegin2.W, rayBegin2.Z / rayBegin2.W); Vector3 rayEnd = new Vector3(rayEnd2.X / rayEnd2.W, rayEnd2.Y / rayEnd2.W, rayEnd2.Z / rayEnd2.W); 

##### Share on other sites
Google keeps on wanting to tell me about UN projects when I do a search for unproject, even if I add DirectX to the search.[/quote]
Hmm for me hitting "directx unproject" brings up a bunch of results about how to unproject vectors in DirectX

What are you trying to do here? Are you trying to calculate a camera ray according to where the mouse is? In that case you just need to multiply your screen point as a [color=#ff0000]normalized coordinate between -1 and +1 with depth equal to 1, with the inverse of the view matrix. This gives you the location in screen space of the "virtual screen pixel". Now you just subtract the camera position to it, then normalize and that gives you the ray direction.

Note that since the virtual screen is always a plane in screen space, you can optimize this by calculating the four corners of the screen and doing a simple bilinear interpolation to obtain any other point. Also, because a standard view (lookat) matrix is always orthogonal, its inverse is equal to its transpose, i.e. M^-1 = M^T.

[color=#ff0000]Remember that math functions typically have *no* notion of what resolution/aspect ratio you are using, and so assume normalized coordinates, normalized distances, normalized vectors, normalized everything, so that they can operate in the most general way possible.

If this is your problem I can give you the camera code of my path tracer (ray tracer) for you to look at. It's in Free Pascal but it should be easily understandable.

##### Share on other sites
No worries; I got it working.
 Vector4 mouseVector = new Vector4(input.Mouse.State.XPos - display.ScreenRectangle.Left, input.Mouse.State.YPos - display.ScreenRectangle.Top, 0F, 1); mouseVector.X = (mouseVector.X - (display.ScreenRectangle.Width / 2)) * 2; mouseVector.Y = (mouseVector.Y - (display.ScreenRectangle.Height / 2)) * 2; mouseVector.X = mouseVector.X / display.ScreenRectangle.Width; mouseVector.Y = mouseVector.Y / display.ScreenRectangle.Height; Vector4 rayBegin2 = new Vector4(mouseVector.X, -mouseVector.Y, 0, mouseVector.W) * scene.GetCamera(mainCameraName).Projection.Inverted(); Vector4 rayEnd2 = new Vector4(mouseVector.X, -mouseVector.Y, 1, mouseVector.W) * scene.GetCamera(mainCameraName).Projection.Inverted(); rayBegin2 *= scene.GetCamera(mainCameraName).View.Inverted(); rayEnd2 *= scene.GetCamera(mainCameraName).View.Inverted(); Vector3 rayBegin = new Vector3(rayBegin2.X / rayBegin2.W, rayBegin2.Y / rayBegin2.W, rayBegin2.Z / rayBegin2.W); Vector3 rayEnd = new Vector3(rayEnd2.X / rayEnd2.W, rayEnd2.Y / rayEnd2.W, rayEnd2.Z / rayEnd2.W); 
Now, to stick the code in a function...