Thread: platform game logic, problem

  1. #1
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717

    platform game logic, problem *Asked a new question! Please help!*

    ok, I'm starting to feel stupid for all these questions I ask...

    Anyways, I have a problem with my thinking, the problem being platform game logic stuff, like collision between the player and the platform.
    Now, I have a rectangle collision type, called hgeRect. hgeRect has a function that checks intersection between two hgeRect. But the problem is that I can't check if the intersection is from top or bottom, or from the sides.
    So, when the player is in air, and gets pulled down by the gravity, and lands on the platform, it stops, that isn't that hard, but the hard part is moving the player, 'cause no matter what method I use, I can't get it to do different things, depending on what side you hit the platform from, like, ofc the same action shouldn't be done, if the platfrom is hit from the bottom, or the top.

    Sorry for my bad typing, or other flaws, I'm extremely tired :S
    Last edited by Akkernight; 02-23-2009 at 10:12 AM.
    Currently research OpenGL

  2. #2
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    What the.. I thinl You a refreshment.
    Code:
    check if collided
      check whether playerRect.y < platformRect.y
      player is above the platform
    Just GET it OFF out my mind!!

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    It is usually best to check what is not inside of the collision rect than what is inside of it.

    I cannot really help you much without a better description of what is happening. Yours was a bit vague which was probably because you were tired when you posted.

  4. #4
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    ok, I have an object, which collides with another object. Now, I want to be able to do different functions depending on where the objects collide, like, if it's the left side of one object and so on
    I think audinue gave me the answer tho, Imma try that, thanks
    Currently research OpenGL

  5. #5
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Didn't wanna make a new topic for this, 'cause it feels like I'm spamming these boards :S
    Anyways, I have this code:

    Code:
    bool character::platCollide(platform& p){
    
    	if(rect.Intersect(&p.rect)){
    		//check if left side
    		//I add it with 16 to make it the mid of the platform
    		if( (p.x + 16) > x && (p.y + 1) < y){
    			onLeft = true;
    
    		} else onLeft = false;
    		//check if right side
    		if( (p.x + 16) < x && (p.y + 1) < y){
    			onRight = true;
    
    		}else onRight = false;
    		//check if top
    		if( (p.y + 16) > y && (p.y + 1) > y){
    			onTop = true;
    
    		} else onTop = false;
    		//check if bottom
    		if( (p.y + 16) < y && (p.y + 31) < y){
    			onBottom = true;
    
    		}else onBottom = false;
    
    		return true;
    	}
    	return false;
    }
    Code:
    void character::pullDown(){
    	if(!onTop)
    		y += gravityPull;
    }
    it works fine, but then when the player goes off the platforms again, like into the air, the gravity doesn't affect the player. Now this seems kinda obvious, 'cause I have no code telling it to do so, but it doesn't work to just do

    Code:
    if(!rect.Intersect(&p.rect)){
    		onTop = false;
    		onLeft = false;
    		onRight = false;
    		onBottom = false;
    }
    If I try to do this, the player just passes through the platforms, like no collision at all :S
    Any ideas?
    thanks in advance!
    Last edited by Akkernight; 02-23-2009 at 10:11 AM.
    Currently research OpenGL

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    Your logic for finding out the side seems a bit off, and where is 16 coming from? This is an imperfect method of attack however. To get it right all the time you will have to take into account the player's position before and after the fact. I've never written something like this, but I'm sure you could find code somewhere.

  7. #7
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    well, the + 16 finds the mid of the platform, since they're always 32x32 and the player x and y coordinates are fixed into being the mid of the character
    So what it does for example if we take the left check, it checks if the player is left side of the platform, and then it also checks if the player is on the platform that way the player can move left and right, then he's on the platform, even tho he's on the left side of the platform, or right side :P
    Currently research OpenGL

  8. #8
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    ok, I fixed this!
    Here's the code if anyone will ever need it xP

    Code:
    void character::hitScreenBounds(){
    	//Hit right side
    	if( (x + 16) >= 1024 ) onLeft = true;
    	else onLeft = false;
    	//Hit left side
    	if( (x - 16) <= 0 ) onRight = true;
    	else onRight = false;
    	//Hit bottom
    	if( (y + 32) >= 768 ) onTop = true;
    	else onTop = false;
    	//Hit top
    	if( (y - 32) <= 0 ) onBottom = true;
    	else onBottom = false;
    
    }
    That did it
    Currently research OpenGL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Josephus problem variant logic troubles
    By misterMatt in forum C++ Programming
    Replies: 0
    Last Post: 04-29-2009, 02:38 PM
  2. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  3. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  4. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM