Question

Normally to set a boundary at a certain area, I would implement something like:

if(player.X > 400){

   player.X = 400;

}

This acts as though there is a vertical line at X=400, blocking movement beyond that. I'm wondering how I would implement at boundary for an area that is at a slant or diagonal rather than vertical/horizontal.

Était-ce utile?

La solution

Hope I answer your question instead of making assumptions about what you are trying to achieve and suggesting a physics engine.

Anyways I would suggest using a line equation and the MathHelper.Clamp function

y = mX + b; // Line equation
x = (y - b) / m

So now you can get the x boundary for the y value of your Player

MathHelper.Clamp(Player.X, min, (Player.Y - b) / m) 

Or the x boundary from the y value of you Player

MathHelper.Clamp(Player.Y, min, (m * Player.X) + b)

Autres conseils

You'll need to implement collision of your player against arbitrary lines. It is impossible to help more without knowing more about your player and/or other choices. For example, why not use an existing physics engine?

I'd say that depends on what exactly you want to do and the type of your game. For dynamic games I suggest using a physics engine of your choice (either 2D or 3D) and add some static shapes that define your boundaries.

For tile based games it is easier to block the players movement before he makes the move. E.g if a player is about to enter a tile out of bounds, simply prohibit the movement.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top