RTS Selection

Started by
2 comments, last by Wolfdog 20 years, 8 months ago
Im looking for a good way to select objects in a 3d rts. Ive already got the 3d vectors and such were i have a cube placed on the terrain were the mouse it pointed. Now im wondering what might be the best way to get some sort of selection.
Advertisement
Here is the way I did it in my RTS game:
Each tile in the map stores a pointer to the unit currently on that tile. During a selection, I went through a for loop, which stored the pointers in a linked list for group movements. Then, as each unit was added to the list, I also set a flag in the unit indicating that it was currently selected. When this flag was set, the unit would draw a selection box around itself in its update function.

Nicholas Skapura
skapura.2@wright.edu
http://skap.8k.com
Nicholas Skapura[email=skapura.2@wright.edu]skapura.2@wright.edu[/email]http://skap.8k.com
I did this. Ok so you have a unit model, such as a cube. (that''s what I used). Find and easy way to do a collision. For example I made my cube 2x2x2 and centered it at 0,0,0. Then I found a math equation to check for collision. Ok now to render your cube you need a transform, store it with the unit and recalculate it every time it moves.

Now to see if you are clicking on the cube imagine that your mouse shoots out a ray. The first thing it hits is selected. So in screen space is ray has a near z value of 0, a far z of 1. The x and y values are the 2d pixel values. Now your need to transform this to world space, so use your view projection matrix, err or maybe the inverse. It has been a while. Use it on both endpoints of the ray. Now you have it in world space. Now use the unit''s transform to get it into unit space. Now check to see if the ray goes through. I know my description is quite lacking. Here is some code. Things like the variable tdz stand for the variables found in a parametric equation. The variable t, times a differential z. Vector is just a typedef for the standard DirectX vector. Hope you can figure this out. I know it is hard, especially since most help (including mine) is cryptic and imcomplete.

bool Unit::GetCollision(Vector start, Vector end, Vector& result)
{
Vector direction;

D3DXVec3Unproject
(
&start,
&start,
&Graphics::viewport,
&Graphics:rojection,
&Graphics::view,
&transform
);

D3DXVec3Unproject
(
&direction,
&end,
&Graphics::viewport,
&Graphics:rojection,
&Graphics::view,
&transform
);

direction/=D3DXVec3Length(&direction);


Vector normal;


//zs
normal=Vector(0,0,1.0f);
if(D3DXVec3Dot(&direction,&normal) < 0 )
{
float tdz = 1 - start.z;
float t = tdz/direction.z;
Vector w = start+t*direction;
if(w.y<1 && w.y > -1 && w.x < 1 && w.x > -1)
{
result=w;
return true;
}
}

normal=Vector(0,0,-1.0f);
if(D3DXVec3Dot(&direction,&normal) < 0 )
{
float tdz = -1 - start.z;
float t = tdz/direction.z;
Vector w = start+t*direction;
if(w.y<1 && w.y > -1 && w.x < 1 && w.x > -1)
{
result=w;
return true;
}
}


//ys
normal=Vector(0,1.0f,0);
if(D3DXVec3Dot(&direction,&normal) < 0 )
{
float tdy = 1 - start.y;
float t = tdy/direction.y;
Vector w = start+t*direction;
if(w.z<1 && w.z > -1 && w.x < 1 && w.x > -1)
{
result=w;
return true;
}
}

normal=Vector(0,-1.0f,0);
if(D3DXVec3Dot(&direction,&normal) < 0 )
{
float tdy = -1 - start.y;
float t = tdy/direction.y;
Vector w = start+t*direction;
if(w.z<1 && w.z > -1 && w.x < 1 && w.x > -1)
{
result=w;
return true;
}
}

//xs
normal=Vector(1.0f,0,0);
if(D3DXVec3Dot(&direction,&normal) < 0 )
{
float tdx = 1 - start.x;
float t = tdx/direction.x;
Vector w = start+t*direction;
if(w.z<1 && w.z > -1 && w.y < 1 && w.y > -1)
{
result=w;
return true;
}
}

normal=Vector(-1.0f,0,0);
if(D3DXVec3Dot(&direction,&normal) < 0 )
{
float tdx = -1 - start.x;
float t = tdx/direction.x;
Vector w = start+t*direction;
if(w.z<1 && w.z > -1 && w.y < 1 && w.y > -1)
{
result=w;
return true;
}
}

return false;

}
Can i ask for some sample code(file) for that Anonymous poster?

just a simple sample that includes terrain(heightmap) and selecting an object in the terrain
pls,pls,pls...
i badly need it!!!!

my email is ruellm@yahoo.com


This topic is closed to new replies.

Advertisement