#### Archived

This topic is now archived and is closed to further replies.

# RTS Selection

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

## Recommended Posts

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.

##### Share on other sites
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

##### Share on other sites
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;

}

##### Share on other sites
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...

my email is ruellm@yahoo.com

1. 1
2. 2
3. 3
Rutin
17
4. 4
5. 5
JoeJ
13

• 9
• 14
• 10
• 25
• 9
• ### Forum Statistics

• Total Topics
632645
• Total Posts
3007629
• ### Who's Online (See full list)

There are no registered users currently online

×