|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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.
__________________
Miscellaneous Software Viper_SB Developershed E-Support Anyone else play chess? Challenge me |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
Quote:
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Game Development > C/C++ 2D SDL Tile Based Game, Gravity problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|