SunQuest
           Game Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesGame Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old February 8th, 2007, 05:38 PM
theKreation theKreation is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Location: St. Catharines, Ontario, Canada
Posts: 5 theKreation User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 7 m
Reputation Power: 0
Question 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

Reply With Quote
  #2  
Old February 8th, 2007, 08:47 PM
Viper_SB's Avatar
Viper_SB Viper_SB is offline
Psycho Canadian
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jan 2001
Location: Canada
Posts: 4,784 Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Weeks 18 h 11 m 57 sec
Reputation Power: 437
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

Reply With Quote
  #3  
Old February 8th, 2007, 09:10 PM
theKreation theKreation is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Location: St. Catharines, Ontario, Canada
Posts: 5 theKreation User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old February 8th, 2007, 09:22 PM
Viper_SB's Avatar
Viper_SB Viper_SB is offline
Psycho Canadian
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jan 2001
Location: Canada
Posts: 4,784 Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Viper_SB User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Weeks 18 h 11 m 57 sec
Reputation Power: 437
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.

Reply With Quote
  #5  
Old February 8th, 2007, 09:25 PM
theKreation theKreation is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Location: St. Catharines, Ontario, Canada
Posts: 5 theKreation User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesGame Development > C/C++ 2D SDL Tile Based Game, Gravity problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway