Restricting 3D movement XNA c#

Started by
4 comments, last by Zakwayda 13 years, 8 months ago
Guys I was trying to get the basics down for my game and I can't seem to be able to restrict the characters movement on the X and Z axis. I have the following code:



// Prevent ship from flying under the ground
Position.Y = Math.Max(Position.Y, MinimumAltitude);

//Prevent ship from flying past the floor boundaries
Position.X = Math.Max(-650000.0f, 65000.0f);
Position.Z = Math.Max(-650000.0f, 65000.0f);

although this just stops the character from moving off this one spot.

One way I thought I could fix this is using some sort of sin or cos function to restrict its movement although how I would implement it is beyond me so any help would be really appreciated!

Game Development Tutorials - My new site that tries to teach LWJGL 3.0 and OpenGL to anyone willing to learn a little.

Advertisement
Quote:Position.X = Math.Max(-650000.0f, 65000.0f);
Position.Z = Math.Max(-650000.0f, 65000.0f);
I think you want to use a 'clamp' function here. The above two lines simply equate to:
Position.x = 65000.0f;Position.y = 65000.0f;
For a clamp function, see:

'Where can I find the clamp function in .Net?'
Thanks a lot guys! :D

Game Development Tutorials - My new site that tries to teach LWJGL 3.0 and OpenGL to anyone willing to learn a little.

Or you could just write:
Position.X = Math.Min(Math.Max(-650000.0f, Position.X), 65000.0f);
Position.Z = Math.Min(Math.Max(-650000.0f, Position.Z), 65000.0f);
Quote:Original post by Ratslayer
Or you could just write:
Position.X = Math.Min(Math.Max(-650000.0f, Position.X), 65000.0f);
Position.Z = Math.Min(Math.Max(-650000.0f, Position.Z), 65000.0f);
If you're going to be performing this operation more than once (as is the case for the OP), you don't want to be writing that out or copy-and-pasting it repeatedly. Instead, the code should be factored out and placed in its own function so that it can be reused.

In other words, a 'clamp' function, as has already been suggested.

This topic is closed to new replies.

Advertisement