tags:

views:

140

answers:

3

I'm building a 3D game but i only plan on using a 2D perspective thus not taking the z axis into the equasion, i want to be able to limit the movement of one of my models so it doesn't move out of the unmoving field of view,

when i was designing 2D it was simple just use clamp, but i cant seem to figurebout how to do this in 3d

any help would be much appreciated

Regards

A: 

So you want in pseudocode:

if (object.position+movementVec is in view of camera)
   object.position+=movementVec;
codymanix
what was wrong with my answer? The OP wanted that his character won't move outside camera, my answer said that Clamp won'T work here since you have to take the camera into account.
codymanix
+1  A: 

If you simply want to do the same thing as a 2D clamp would - Vector3.Clamp set the Y (usually 'UP') component of the two bounding vectors you pass to be 0.

I'm slightly confused on the question however, it seems that maybe what you are after is a form of collision detection with the view frustum This article may help with that if your model can fit into a bounding sphere relativity nicely.

You will need to for test collision vs all the planes which define the view space. If its a perspective camera your using, you'll need to get the Frustum Planes, otherwise if its an orthographic camera they are the planes that make the bounding box of the view space (a cuboid which is orientated the same way as the camera).

Courtney de Lautour
+1  A: 

Just use Vector3.Clamp(Vector3 value1, Vector3 min, Vector3 max) and use the constructor on Vector3 that takes a Vector2 and an int (for z value).

RCIX