Question

To start out I want to say I am making a 2D side scrolling video game. The game is coded in C# and the framework is with XNA. I was wondering what is an easy way to make a basic physics engine might be, and where to put it. My experience with XNA isn't to extensive but I know enough to make a game without physics, so naturally the now I wish to add physics (a jump feature that then pulls the player down). My goal is to end up with similar physics to Terraria or Super Mario. Any ideas on where to start? I know I will need at least a vector to pull the player down and another to push the player up, but thats as much as I know....

Was it helpful?

Solution

Unless you need the player to push things around and by physics, you mean just jumping, then implementing jumping as in traditional sidescrollers is simple.

You might want to do something like this:

float currentYspeed;
float GRAVITY = 5;
bool playerFlying;

// In Update():
if (isUpArrowKeyDown() && !playerFlying) { playerFlying = true currentYspeed = 50; }
playerY += currentYspeed;
currentYspeed -= GRAVITY;
if (playerCollidesWithFloor())
{
  currentYspeed = 0; // We came back to ground.
  playerFlying = false;
}
else if (playerCollidesWithSomethingElse())
{
  currentYSpeed = 0; // We hit obstacle in-air.
}

Of course, you might want to refactor and beautify this code, make the position and velocity changes dependent on seconds elapsed etc.

If you don't know how to implement the collision functions, consider reading up on keywords "2D collision detection" or "sidescroller/platformer tutorial".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top