# making camera go up stairs fps type camera

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

## Recommended Posts

I am finding it hard to figure out how to make the camera walk up stairs , how is done exactly , i have a feeling its done with picking but could be messy or getting the hieight y value of the steps vertex and setting the camera position to that...can anyone shed some light on this, as its got me baffelled... the stairs is a seperate mesh from the corridor btw..

##### Share on other sites
Depends on what routines you have available in your engine. You can certainly do picking - shoot a ray "down" from your camera position to get an elevation. If your application has a collision engine included, you can use a bounding sphere for the camera and let collisions between that sphere and the "terrain" determine the camera location.

##### Share on other sites
but lets says i place a pick ray like you suggest , if i am heading towards the stairs , its going to pick the triangle thats facing the camera and not the actual steps, plus what if the player is looking upwards and still wants to move up the steps , what happens then as the pick is going to pick the steps further up etc..

see the blue triangle is the picked triangle, if i was to get that the height of that it would make no sense :/
http://img19.imageshack.us/img19/8323/picksteps.jpg

##### Share on other sites
Its only going to pick whats in front if you shoot the ray from the camera forward in the look direct, you want to pick whats below the player then shoot the ray down.
Physics systems like bullet or nvidias physx have character controllers that auto step up a given height, making things like this a lot easier, thats what I would suggest using, unless you are trying to do it yourself that is.

##### Share on other sites
Quote:
 Original post by NumberXaeroIts only going to pick whats in front if you shoot the ray from the camera forward in the look direct, you want to pick whats below the player then shoot the ray down.Physics systems like bullet or nvidias physx have character controllers that auto step up a given height, making things like this a lot easier, thats what I would suggest using, unless you are trying to do it yourself that is.

if try placing a ray at the camera positon with a direction of 0,1,0 , then its going to pick the underside of the steps right?

##### Share on other sites
Just to try and clear up some confusion...

What was suggested is that you shoot a ray from your camera's position downward and find the ground.

This isn't done through picking (the process where you find whats under the mouse on the screen), this is done through what's called a ray cast.

Basically you test a ray for collisions against your world geometry.

It doesn't have anything to do with what is or is not on screen or under the mouse.

It has more to do with what is under the player's feet and how far it is to the ground from the player's current position.

Also if wondering, often times even though the stairs look like steps, the collision object for them in games is often a ramp. That makes the camera go smoothly up it and simplifies some other things (esp navigation mesh pathfinding type things).

If you go the route of actually having stairs in the collision geometry, you will probably want to smooth the camera movement to make it not look jerky as you go up and down steps. One way to do this is to interpolate to the camera's target position over time instead of teleporting it there each frame.

HTH!

##### Share on other sites
if i do this i get nothing
D3DXVECTOR3 RayPos(gCamera->mPosW.x,gCamera->mPosW.y,gCamera->mPosW.z + 1.0f);
D3DXVECTOR3 RayDir(0.0f,1.0f,0.0f);

D3DXMatrixInverse(&i,NULL,&(bl2->BuildingWorld * gCamera->view()));
D3DXVec3TransformCoord(&TransPos,&RayPos,&i);
D3DXVec3TransformNormal(&TransLook,&RayDir,&i);
D3DXVec3Normalize(&TransLook,&TransLook);
D3DXIntersect(bl2->Mesh,&TransPos,&TransLook,&Hit,&FaceIndex,NULL,NULL,&Dist,NULL,NULL);
if(Hit)
{
//MessageBox(NULL,"Hit","",0);
sprintf(buffer,"%s %d %f","Hit",FaceIndex,Dist);
SetWindowText(FindWindow("D3DWndClassName",NULL),buffer);
//pm->RenderPickedTri(bl2->Mesh,bl2->BuildingWorld,FaceIndex);
}

removed

##### Share on other sites
well isnt it better to pick the steps from underneath?
then the Dist float would tell me how much i need to add to camera.y etc

##### Share on other sites
It's because TransPos is the starting position and TransLook is how far out you test (as in it's next position to test too). I am referring to the layout you used in this function...
D3DXIntersect(bl2->Mesh,&TransPos,&TransLook,&Hit,&FaceIndex,NULL,NULL,&Dist,NULL,NULL);

Edited : its absolute, not relative positioning, oops

##### Share on other sites
so how comes its not intersecting with the steps?, everthing looks right to me

##### Share on other sites
Sorry, screwed up in my first response and removed it. TransPos should be set to your camera position and TransLook should be set to a value directly below this position. Say if your camera is at 25,50,25 you would subtract say -9.8 (gravity constant in m/s) the TransLook would be 25,(50-9.8),25 (25,40.2,25).

This doesn't include any offset you will need to keep the camera off of the floor and/or stairs.

##### Share on other sites
It just seems the ray dosent move with the camera at all , and is picking random triangles when tested with the floor, i dont think this method will work :(

[Edited by - Anddos on October 19, 2010 9:24:54 PM]

##### Share on other sites
Quote:
 D3DXMatrixInverse(&i,NULL,&(bl2->BuildingWorld * gCamera->view()));

In my first post, I did misspeak (as mentioned above) and you should shoot a ray, not do a pick.

If your camera position is in world coordinates, and BuildingWorld is the world matrix for the mesh you're trying to shoot, you shouldn't be using the camera view matrix.

Try just D3DXMatrixInverse(&i,NULL,&bl2->BuildingWorld).

You want to move the camera position from the world to the object space of the mesh. The view matrix has nothing to do with that.

The camera position you shoot from should be the desired position (above the step), with the direction D3DXVECTOR3( 0, -1, 0 ), pointing down, not up (as you have it).

##### Share on other sites
that was it buckeye, thats great now i can progres and thanks for clearing up the no need for view with no picking rays :)

##### Share on other sites
Quote:
 but it dosent seem to move with the camera

Just shooting a ray isn't going to move the camera for you.

Assuming you want the camera to be a fixed distance above the ground, get the distance from the ray position down to the mesh where you want the camera to be next. If that's less than the height you want the camera to have, move the camera up (and forward to it's new position).

##### Share on other sites
Quote:
Original post by Buckeye
Quote:
 but it dosent seem to move with the camera

Just shooting a ray isn't going to move the camera for you.

Assuming you want the camera to be a fixed distance above the ground, get the distance from the ray position down to the mesh where you want the camera to be next. If that's less than the height you want the camera to have, move the camera up and forward.

i forgot to add scaling to the rendering triangle

pm->RenderPickedTri(bl->Mesh,bl->BuildingScale*bl->BuildingWorld,FaceIndex);

##### Share on other sites
Good for you! [smile]

##### Share on other sites
pritty sure the ray direction should point up , if its down the ray would point down and completely miss the steps , if its up its going to hit underneath the steps etc like this, then the Dist will change as it gets deeper in to the stairs etc, i need to just work with the Dist varible with the Camera.y etc..
i guess the ray would need to point down for going down the stairs, so 2 rays needed it think

like this

http://img169.imageshack.us/img169/225/underneathstepstri.jpg

[Edited by - Anddos on October 19, 2010 11:02:41 PM]

##### Share on other sites
Quote:
 if its down the ray would point down and completely miss the steps

There's obviously some information or assumption that you've made that you haven't stated. Isn't the camera above the steps?

It's been assumed that your camera position is somewhere around the player's eye position and "down" means toward the player's feet. From your original post about getting " the hieight y value of the step" (you mention y as height)it was assumed that ( 0, -1, 0 ) would be down. It was also assumed that the mesh you want to shoot is below the eye position as a player's eyes are normally above the terrain. You would shoot from the eye position down to the mesh to determine the distance between the eye position and whatever's below that position.

By the way, if you have a non-uniform scaling matrix, you can use that to transform a coordinate (the ray position), but you have to use an inverse transpose to transform a direction. Directions don't scale non-uniformly directly.

##### Share on other sites
i am getting a terriable ripping effect if i add an else in this,il make a video soon to show what i mean

static float ground = 112.0f;	gCamera->mPosW.y = ground;	D3DXIntersect(bl->Mesh,&TransPos,&TransLook,&Hit,&FaceIndex,NULL,NULL,&Dist,NULL,NULL);	if(Hit)	{				if(Dist < 1.0f)		{						ground += 0.5f;                }		else		{       			if(Dist > 0.5) //this is the problem 			{				ground -= 0.5f;			}		}					}

##### Share on other sites
Maybe you can explain what it is you're trying to do.

I thought you were trying to position the camera a certain distance above the local ground. If the distance from the eye position to the ground is less than desired, increase the elevation of the eye. If the distance is greater than desired, lower the eye.

##### Share on other sites
this is what i am trying todo , make the camera go up and down the hills
it works but for some reason there is a small shacking effect on the ground texture , ive tried making a video but it dosent show up on the video which is odd , this only happens if that last else is there that lowers the camera

heres a pic anyway

http://img821.imageshack.us/img821/1981/groundcol.jpg