Jump to content
  • Advertisement
Sign in to follow this  
emforce

Restricting 3D movement XNA c#

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!