Sign in to follow this  
Greenbird

Java/ Collision Detection

Recommended Posts

Hey guys ive posted this question in a lot of other forums but i have not really gotten a solution yet a friend of mine recommended this forum to me so i thought id give it a try. Anyway at the moment im creating a Puyo Puyo game in Java puyo puyo is basically tetris the only difference being instead of blocks its spheres that are connected to each other side by side. My problem is collision detection im trying to implement grid collision detection. After a set of puyos has fallen if another set tries to fall into that same location on the board it should stop on top of it and not overlap in other words collision. Basically what i did is i listed all the possible locations the puyos could fall at the bottom of my board which is 12 X 6. If a location is not null then there is something there what im not understanding is that once a puyo has fallen and that location is no longer null how do i perform a check if another puyo is trying to fall in that same location and keep it from overlapping the other puyo. If anyone can help id really appreciate it.
//Check to see if the position located at p1 and p2 is holding a set of puyo's.
	  public boolean positionEmpty()
	   {
        p1 = puyolist[0][11];
    	p2 = puyolist[1][11];
    	p3 = puyolist[2][11];
    	p4 = puyolist[3][11];
       	p5 = puyolist[4][11];
    	p6 = puyolist[5][11];
         
    	  if((p1 != null) && (p2 != null)){
  
    	  	if((p3 != null) && (p4 != null)){
    	  		
    	  		if((p5 != null) && (p6 != null)){
    	  		}
    	  	} 
       }
       return false;
	}
[Edited by - Greenbird on November 6, 2009 2:13:27 PM]

Share this post


Link to post
Share on other sites
You want to call a function like this after puyos have been removed, to implement gravity:


private void movePuyosDown() {
final int width = puyolist.length; // Not sure which way around you have your array set up: is it puyolist[col][row] or puyolist[row][col]? You might need to swap these two definitions.
final int height = puyolist[0].length;

// Move vertically up the board
for(int y = height - 2; y >= 0; --y) {
// Move horizontally right across the board
for(int x = 0; x < width; ++x) {
// Look for an empty spot under this puyo
int newY = y;
while(newY + 1 < height && puyolist[x][newY + 1] == null)
++newY;

// Did we actually find an empty spot?
if(newY != y) {
// Yes, so move this puyo down
// Copy the puyo down
puyolist[x][newY] = puyolist[x][y];
// Empty its old spot
puyolist[x][y] = null;
}
}
}
}




I'm not sure what level of ability you're at in game programming, so let me know if this helps, or if you need further clarification.

Share this post


Link to post
Share on other sites
Hey thanks a lot for the reply mattd but yeah im going to need further clarification if its not too much trouble. Ive only implemented collision detection in dark basic this is my first attempt at actual programming language. If you could explain how i could implement this logic you've posted within my game id really appreciate it. Thanks again.

Share this post


Link to post
Share on other sites
Quote:
Original post by Greenbird
Hey thanks a lot for the reply mattd but yeah im going to need further clarification if its not too much trouble. Ive only implemented collision detection in dark basic this is my first attempt at actual programming language. If you could explain how i could implement this logic you've posted within my game id really appreciate it. Thanks again.


What are you talking about? He gave you source code. Programming is a task that requires thinking. At some point you have to figure things out for yourself. At the very least, figure out exactly what part you don't understand.

Share this post


Link to post
Share on other sites
Hi im very sorry... i should have clarified... the reason im asking for further clarification is that the answer posted to my question seems to check if there is a spot below a puyo after it has been removed which does not really answer my question.

What i was asking was when a puyo falls and lands on the board and then when a second puyo falls after the first, how would i have the second puyo know that there is already a puyo on the board and that if it tries to move into the location of the first puyo it will not be able to. So it would essentially stop on top of the second set of puyos "Collision" and this would then continue to happen until there a connected set puyos of the same color or a game over condition.

Unless im a totally bad programmer and the answer posted does exactly what Ive just said then i apologize...but at the moment i feel that my question was written poorly and misunderstood and i apologize for that.

So if anyone can help me figure out how to perform collision detection between my puyos id really appreciate it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this