The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Game Development
|
C/C++ 2D SDL Tile Based Game, Gravity problem
Discuss C/C++ 2D SDL Tile Based Game, Gravity problem in the Game Development forum on Dev Shed. C/C++ 2D SDL Tile Based Game, Gravity problem Game Development forum covering non language specific programming - game creation, design, modding, theories and math. A place for developers and gamers of all levels to discuss and debate all things involved in game creation and modding.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 8th, 2007, 05:38 PM
|
|
Registered User
|
|
Join Date: Feb 2007
Location: St. Catharines, Ontario, Canada
Posts: 5
Time spent in forums: 3 h 7 m
Reputation Power: 0
|
|
C/C++ 2D SDL Tile Based Game, Gravity problem
Greetings,
I'm currently writing a 2D, Tile-based game, written in c/c++, and SDL. Everything is working pretty well up to this point (Collision detection, Player input, etc).
But one thing that isn't working top-notch, is my gravity. The logic behind the game works like this:
Code:
Main Entry
Game Loop
Player->getInput
Background->Draw
Map->Draw(tiles)
Player->update
Player->draw
Screen->update
End Game Loop
End Main
The player class is where all the magic happens for my gravity, within' the update() method/function. My jumping/gravity works something like this:
Code:
// player_velocity_y will equal -10, for moving up.
// GRAVITY for example will = 1 and eventually make player_velocity_y equal 0
// I simply re-wrote what I have in the actual source file to be more legible
// but everything here is exactly how I have it.
// Player is moving up
if(player_velocity_y < 0) {
player_ypos += player_velocity_y;
player_velocity_y += GRAVITY;
// Player hit top of screen or a tile above him
if(player_ypos < 0 || collision_vertical(tiles)) {
player_ypos -= player_velocity_y;
player_velocity_y = 0;
}
}
// Player is moving down
else {
player_ypos += player_velocity_y;
player_velocity_y += GRAVITY;
player_lockjump = true; // player may not jump in air
// Player hit the ground tiles
if(collision_vertical(tiles)) {
player_ypos -= player_velocity_y; // don't fall through tile
player_velocity_y = 0; // no more vertical speed
player_lockjump = false; // player may jump again
}
}
Now the code that I have written above works fine, with one problem, my sprite looks like its having a seizure, because when it collides with a tile on the ground, it tries to move the sprite back up (so it's not inside a tile), when doing so, player_velocity_y is less then zero, therefor the program thinks the player is jumping again, and its an endless looping of seizures. At least I think that's what it's doing. I have a compiled binary if anybody needs to see what I mean, but this forum will not allow URL links.
Thanks,
Dex
|

February 8th, 2007, 08:47 PM
|
 |
Psycho Canadian
|
|
Join Date: Jan 2001
Location: Canada
|
|
Welcome to Dev Shed, the forum does allow links you just have to have 5 posts first  . It's to prevent spamming.
When you move the char back up when it hits a tile don't give it a velocity, or better yet check if it's going to hit the tile and don't move it down.
|

February 8th, 2007, 09:10 PM
|
|
Registered User
|
|
Join Date: Feb 2007
Location: St. Catharines, Ontario, Canada
Posts: 5
Time spent in forums: 3 h 7 m
Reputation Power: 0
|
|
I was thinking of maybe getting the position of the tile that I'm colliding with, and just setting the player to that position, no velocity, just a simple:
Code:
player_ypos = tilecoordinates*TILESIZE-player_height-1;
The "tilecoordinates" being the tile that I collided with. But I'm just wondering if that would rise problems later if I go that route.
|

February 8th, 2007, 09:22 PM
|
 |
Psycho Canadian
|
|
Join Date: Jan 2001
Location: Canada
|
|
That should work I'd think. But I haven't done much collision detection yet  . There are some others here that are quite good at this and hopefully they'll come and reply. Might have to wait a day or so.
|

February 8th, 2007, 09:25 PM
|
|
Registered User
|
|
Join Date: Feb 2007
Location: St. Catharines, Ontario, Canada
Posts: 5
Time spent in forums: 3 h 7 m
Reputation Power: 0
|
|
Quote: | Originally Posted by Viper_SB That should work I'd think. But I haven't done much collision detection yet  . There are some others here that are quite good at this and hopefully they'll come and reply. Might have to wait a day or so. |
Yeah, I'm kind of new to collision detection. I can only grasp Bounding Box collision right now, which is pretty simple so far =). But I'll hold on and see what others have to say, thanks for your input.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|