Jump to content
  • Advertisement
Sign in to follow this  
JuliusDegutis

2D movement

This topic is 2329 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

Hello, I'm trying to make simple 2d platformer with tile based map. And second day I stuck with movement. Maybe anybody know really good tutorial. What I looking is gravitation and jumping.
I have made player states: Falling, Standing, Walking,Running. With last two I have handled, and don't know how to make first two to work.
Have made some methods:
1. For checking if there is no collision. If many and how is same number, then object don't collide with any other object (tile)

private void gravity(ArrayList collisionBlocks)
{
int many = collisionBlocks.Count;
int how = 0;
foreach (Block x in collisionBlocks)
{
if (!this.collision(x))
how++;
}
if (how == many)
{
mCurrentState = State.Falling;
}
}

2. For stoping gravity if collides with any object. And if collides, then put player on top of collided tile

private void stopGravity(ArrayList collisionBlocks)
{
foreach (Block x in collisionBlocks)
{
if (mCurrentState == State.Falling)
if (this.collision(x))
{
this.worldXY.Y = x.bounds.Top - this.height;
mCurrentState = State.Standing;
break;
}
}
}

3. And last one - update method



public void movement(ArrayList collisionBlocks)
{

Console.WriteLine("State - " + mCurrentState);
gravity(collisionBlocks);
stopGravity(collisionBlocks);
if (mCurrentState == State.Falling)
mGravitation();



if (mCurrentState != State.Falling)
{
if (Keyboard.GetState().IsKeyDown(Keys.A))
{
moveLeft();
mCurrentState = State.Walking;

} if (Keyboard.GetState().IsKeyDown(Keys.D))
{
moveRight();
mCurrentState = State.Walking;

}
if (Keyboard.GetState().IsKeyDown(Keys.LeftShift))
{
acceleration();
mCurrentState = State.Running;
}
if ((Keyboard.GetState().IsKeyUp(Keys.A) && Keyboard.GetState().IsKeyUp(Keys.D)))
mCurrentState = State.Standing;
}
.....
}


Maybe anybody see my mistake? This that code my character jumping all the time. Like move down per pixel, move back, move down and so on. This effect only ends if player is in falling state

Share this post


Link to post
Share on other sites
Advertisement
It might be an issue with this line, in the stopGravity method:

this.worldXY.Y = x.bounds.Top - this.height;

You're probably putting the player back into the air, which kicks you back into the Falling state, and then when you hit the ground again, the same thing happens, making it look like your character is jumping all the time.

PS:

You should build a simple physics system that will properly move all relevant game objects, rather than having each object move itself.

Share this post


Link to post
Share on other sites
Sorry, I'm not really following the code you pasted very well or the exact problem that you described.

The way I look at it, gravity is one of those things that's always in effect. Although the character is not necessarily always falling, there is always this force trying to push him down. Every loop through the code, check to see if gravity would move the character into an object. If it would, then either don't move him at all or make sure that the character is now just on top of the object. If there is no object to collide with, move the character down. Every time the program goes through the loop, always run this code. Then when the player presses a button to jump, attempt to move the character in an upward direction that's greater than the amount he's being pulled downward. The amount you try to move him upwards should decrease every iteration through the loop. Eventually, the amount he's traveling upwards will be 0 and the code you have implementing gravity will pull him back down again. You'll have to account for collisions while jumping accordingly.

I'm not sure how that fits in with what you have for tracking a characters current state but it's the general idea behind what I used for my 2d platform game.

Share this post


Link to post
Share on other sites
A few things to note, as a general method for this:
#1, using a physics engine here would make these work very smoothly. However, I'll assume you want to do this yourself
#2, As mentioned above, you should always have a downward force applied to your character.
#3, you need to check specifically on the player colliding below his feet, not just any collision; otherwise, if you're jumping right, and you hit a wall, you will stand on the wall, since you've collided with it in the X-plane.

In Pseudo code it would do this:


// Speed player moves left or right
#define MOVEMENT_SPEED 10.0f

// initial velocity given to player when he jumps
#define JUMP_VELOCITY 20.0f

void Player::HandleInput()
{
if (LeftIsPressed()) {
this.xVelocity = -MOVEMENT_SPEED;
}
else if (RightIsPressed()) {
this.xVelocity = MOVEMENT_SPEED;
else {
this.xVelocity = 0.0f;
}

// Only jump if we're not already jumping or falling
if (JumpIsPressed() && this.OnGround) {
this.yVelocity = -JUMP_VELOCITY;
}
}

// defines amount to increase downward velocity every frame
#define GRAVITY_FORCE 4.0f

void Player::Update()
{
// Apply downward force to player
this.yVelocity += GRAVITY_FORCE;

// Move the Player
this.xLocation += this.xVelocity;
this.yLocation += this.yVelocity;

// Check we've collide with something above or below us
if (CheckCollisionY()) {
// move us back to previous location and Stop Y Velocity
this.yLocation -= this.yVelocity;
this.yVelocity = 0.0f;
this.OnGround = true;
}
else {
this.OnGround = false;
}

// Check if we've collided with anything on our left or right

if (CheckCollisionX()) {
// move us back to previous location and Stop X Velocity
this.xLocation -= this.xVelocity;
this.xVelocity = 0.0f;
}
}



The above code will always apply a y force. So, if you're player has jumped, you've set the yVelocity to -20.0f. Each update, the velocity will decrease by 4.0f (or increase, as + y is down). Eventually, it will reach 0, then go negative until the player collides with something below him.

Also, not, if the player jumps, and his head hits something, it will set y Velocity to 0 as well. You may or may not want that.

Good Luck!

Share this post


Link to post
Share on other sites
Sorry for going off topic here, but what some of you guys mean by "you should use a physics engine"? I'm planning to start doing my first game, and I didn't know you can use an already build physics engine, how do you implement it? Where do you get it?

Share this post


Link to post
Share on other sites

Sorry for going off topic here, but what some of you guys mean by "you should use a physics engine"? I'm planning to start doing my first game, and I didn't know you can use an already build physics engine, how do you implement it? Where do you get it?

Box2D and Farseer Physics are some free 2D physics engines. Basically, they provide you with methods to add physics objects (with their properties such as mass, friction, etc...) in an empty world, as well as constant forces like gravity, and then they do the physics simulation for you (via some Step() or Update() function). Then you get back the new positions of the objects and use them to render the scene. You still need to learn how to use the physics engine, though, but in general they are pretty friendly.

Share this post


Link to post
Share on other sites

[quote name='ChizFreak' timestamp='1338609022' post='4945482']
Sorry for going off topic here, but what some of you guys mean by "you should use a physics engine"? I'm planning to start doing my first game, and I didn't know you can use an already build physics engine, how do you implement it? Where do you get it?

Box2D and Farseer Physics are some free 2D physics engines. Basically, they provide you with methods to add physics objects (with their properties such as mass, friction, etc...) in an empty world, as well as constant forces like gravity, and then they do the physics simulation for you (via some Step() or Update() function). Then you get back the new positions of the objects and use them to render the scene. You still need to learn how to use the physics engine, though, but in general they are pretty friendly.
[/quote]

Also chipmunk-physics, which i use, is a great 2d physics library. Check out my old blog (linked in my sig) for an example of a game using 2d physics engine.

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!