Results 21 to 40 of 59
Thread: 2D Game
- 08-02-2010, 04:38 PM #21
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
- 08-02-2010, 04:52 PM #22
The code you posted only gets one pixel a location 0,0. Remember the array index is 0 based.
int pixel = pixels[ 0 * 0 ];
When I run your code with your image I get: 186 | 186 | 186 for the pixel at 0,0.
- 08-02-2010, 05:01 PM #23
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
Aha! Yes, perfect! Thanks a lot, I'll try and integrate it into the game now!
- 08-02-2010, 05:42 PM #24
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
Java Code:if ( key == KeyEvent.VK_DOWN ) { String downCol = checkForCollission( 'd' ); System.out.println( "DOWNCOL: " + downCol ); if ( downCol == "200 | 200 | 200" ) System.out.println( "It's that colour!" ); else dy = 1; }I'm now making it so that when you press the down key it calls the checkForCollission function. If it returns the color 200 | 200 | 200 then we want to stop the player from moving down, otherwise let's move down. However, even though downCol is 200 | 200 | 200 when printed, the if statement seems to say it isn't and goes straight to the else..Java Code:Console Output: DOWNCOL: 200 | 200 | 200 DOWNCOL: 200 | 200 | 200 DOWNCOL: 200 | 200 | 200 DOWNCOL: 200 | 200 | 200 DOWNCOL: 200 | 200 | 200
Any ideas?
No worries, just did this:
if ( downCol.equals( "200 | 200 | 200" ) )Last edited by Elffus; 08-02-2010 at 05:45 PM.
- 08-02-2010, 05:59 PM #25
That's weird code. What's the meaning of: "200 | 200 | 200"
Why does the method return that string?????
What is the purpose/function of the checkForCollission method? Should it return a boolean if there is a collusion? A collusion being if the next pixel in the direction being moved in is a particular color:BTW Use the equals() method to compare StringsJava Code:boolean checkForCollission(Point, <direction>, <BoundaryColor>) // Returns true if a move from Point in direction is BoundaryColor
- 08-02-2010, 06:13 PM #26
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
I know, I edited it at the bottom, just had to use equals. Right, the meaning of 200 | 200 | 200. Basically the Collision function gets the RGB ( R200 | G200 | B200) values of the 3 pixels infront/below it, etc. It returns them, and if the function then picks them up we can stop the player moving. Here is what I have for when the right key is pressed:
If the player is next to the wall, and they press the right arrow key it wont let them through (the wall has two colors). However, if they hold it down they can get through easily as this is only KeyPressed. Is their anyway to continuously check when it's being held?Java Code:if ( key == KeyEvent.VK_RIGHT ) { String downCol = checkForCollission( 'r' ); int right = downCol.indexOf( "200 | 200 | 200232 | 232 | 232" ); //int skySpace = downCol.indexOf( "137 | 205 | 255" ); System.out.println( downCol ); //System.out.println( "Floor: " + floor + " | Skyspace: " + skySpace ); //if ( downCol.equals( "200 | 200 | 200" ) ) if ( right < 0 ) { ImageIcon i = new ImageIcon( "D:/PW/Java/right.png" ); still = i.getImage( ); dx = 1; } }
- 08-02-2010, 06:44 PM #27
This is getting weirder and weirder.downCol.indexOf( "200 | 200 | 200232 | 232 | 232" );
Pass an array of boundary colors to the collusion testerthe wall has two colors
- 08-02-2010, 08:12 PM #28
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
I constructed it as you advised.. but I am unsure of how to check whether the pixels match what the array wants?Java Code:public boolean checkForCollision( char type, String match[] ) { String returnS = ""; for ( int i = 0; i < 2; i++ ) { // the background image. Image background = new ImageIcon( "D:/PW/Java/background.png" ).getImage( ); // declare width & height. int width = 1, height = 1; // automtatically a right check. int playerX = x + ( still.getWidth( null ) - 1 + i ); int playerY = y + ( still.getHeight( null ) / 2 ); // if it's a down check. if ( type == 'd' ) { playerX = x + ( still.getWidth( null ) / 2 ); playerY = y + ( still.getHeight( null ) - 1 + i ); } // create the pixel grabber. int[] pixels = new int[ 1 * 1 ]; PixelGrabber pg = new PixelGrabber( background, playerX, playerY, width, height, pixels, 0, width ); try { pg.grabPixels( ); } catch ( InterruptedException e ) { System.out.println( "Couldn't grab pixels." ); } int pixel = pixels[ 0 * 0 ]; int red = ( pixel >> 16 ) & 0xff; int green = ( pixel >> 8 ) & 0xff; int blue = ( pixel ) & 0xff; returnS = returnS + ( red + " | " + green + " | " + blue ); System.out.println( returnS ); } return false; }
- 08-02-2010, 08:21 PM #29
Here's a very rough sample of my idea:
Java Code:public boolean checkForCollision(char direction, int x, int y, Color[] boundaryColors ) { // check the color at the point in the direction int pixel = getColorInDirection(x,y,direction); for ( int i = 0; i < boundaryColors.length; i++ ) { if(boundaryColors[i].getRGB() == pixel) return true; } return false; // pixel doesn't touch a boundary }
- 08-02-2010, 08:57 PM #30
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
Okay, thanks! I created this:
and to run this:Java Code:public boolean checkForCollision( char type, Color[] match ) { // declare width & height. int width = 1, height = 1; // automatically a right check. int playerX = x + ( still.getWidth( null ) - 1 ); int playerY = y + ( still.getHeight( null ) / 2 ); // if it's a down check. if ( type == 'd' ) { playerX = x + ( still.getWidth( null ) / 2 ); playerY = y + ( still.getHeight( null ) - 1 ); } // create the pixel grabber. int[] pixels = new int[ 1 * 1 ]; PixelGrabber pg = new PixelGrabber( background, playerX, playerY, width, height, pixels, 0, width ); try { pg.grabPixels( ); } catch ( InterruptedException e ) { System.out.println( "Couldn't grab pixels." ); } int pixel = pixels[ 0 * 0 ]; int red = ( pixel >> 16 ) & 0xff; int green = ( pixel >> 8 ) & 0xff; int blue = ( pixel ) & 0xff; //returnS = returnS + ( red + " | " + green + " | " + blue ); //System.out.println( returnS ); Color currentColor = new Color( red, green, blue ); System.out.println( "|| COLORCURRENT ||" + currentColor ); for ( int j = 0; j < match.length; j++ ) { System.out.println( "|| COLORMATCH ||" + match[j] ); if ( match[j].equals( currentColor ) ) { // pixel hits a boundary. System.out.println( "|| INCREMENTING ||" ); return true; } } // nothing is touched. return false; }
It works perfectly in the sense that if you stop at a door and press the key you wont be able to go through - but if you hold down the key you can (because this is only in the KeyPress function). How do I make it check whenever the character is moving, rather than when the key is pressed?Java Code:if ( key == KeyEvent.VK_DOWN ) { Color[] typeArray = { greyEdge, whiteParts }; boolean collision = checkForCollision( 'd', typeArray ); if ( collision == false ) { dy = 1; } }
- 08-02-2010, 09:36 PM #31
"is moving" means at different locations at different times. To detect rate of movement, you need to save an older location and the time the character was there and then get the time at the new location and compare. If the locations for different times are the same, the character did not move over that time slice.How do I make it check whenever the character is moving
- 08-04-2010, 03:25 PM #32
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
Okay. I recoded the whole base and redid the Sprite class. To check collisions I now have a separate image, and you draw red lines to symbolize a collision. The only problem now is that when you run the applet and you move you can sometimes run through the collisions.
It checks like this:
Which is called in Sprite.java.Java Code:public void update( long timePassed ) { // call collision check if velocityX/Y has a value. if ( velocityX != 0 ) { // Check for a collision. if ( velocityX > 0 ) aCollision = runCollisionCheck( "right" ); else if ( velocityX < 0 ) aCollision = runCollisionCheck( "left" ); //System.out.printf( "RUNNING COLLISION CHECK (%s)\n", aCollision ); if ( aCollision == false ) { // So if velocity is distance / time, then // distance = velocity * time. x += velocityX * timePassed; } else if ( aCollision == true ) { velocityX = 0; //System.out.println( "STOPPING CHARACTER (X)" ); } animation.update( timePassed ); } }
- 08-04-2010, 03:41 PM #33
Sounds like a logic problem.problem now is that when you run the applet and you move you can sometimes run through the collisions.
How do you flag a collision? Does the code check that a collision has occurred and then NOT run thru it? What prevents "running thru" a collision?
- 08-04-2010, 03:48 PM #34
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
The code above. If their is a collision it sets the velocity to 0. Otherwise, it moves the player depending on the time it takes to move.
- 08-04-2010, 03:52 PM #35
So what allows it to "run thru" a collision? The code must not see or it ignores the condition. Time for some more debugging.
- 08-04-2010, 03:59 PM #36
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
So when the velocity for moving across X is higher than 0 (it has to be to move) it makes a check for a collision.Java Code:if ( velocityX > 0 ) aCollision = runCollisionCheck( "right" );
runCollisionCheck:
So if there is a collision pixel one across from the top right of the sprite, or the bottom of the sprite, it returns true.Java Code:private boolean runCollisionCheck( String side ) { int[] pixels; int playerX, playerY; int imageHeight, imageWidth; Image getImage = getImage( ); //for ( int i = 0; i < 4; i++ ) //{ // we need to check each side of the sprite. //playerX = x + ( still.getWidth( null ) - 1 ); //playerY = y + ( still.getHeight( null ) / 2 ); imageHeight = getImage.getHeight( null ); imageWidth = getImage.getWidth( null ); // check right. if ( side == "right" ) { pixels = new int[ 1 * 1 ]; playerX = Math.round( x + ( imageWidth - 1 ) ); // inner loop. check every pixel on the right side of the player. for ( int j = 0; j < 2; j++ ) { if ( j == 0 ) playerY = Math.round( y ); else playerY = Math.round( y + imageHeight - 1); PixelGrabber pg = new PixelGrabber( collisionImg, playerX, playerY, 1, 1, pixels, 0, 1 ); try { pg.grabPixels( ); } catch ( InterruptedException e ) { System.out.println( "Couldn't grab pixels (1)." ); } int pixel = pixels[ 0 * 0 ]; int red = ( pixel >> 16 ) & 0xff; int green = ( pixel >> 8 ) & 0xff; int blue = ( pixel ) & 0xff; //String returnS = ( red + " | " + green + " | " + blue ); //System.out.println( returnS ); Color currentColor = new Color( red, green, blue ); if ( currentColor.equals( Color.RED )) return true; } }
Which stops the character. I'll start the applet and run through the wall now; with the debugs uncommented. Each test I'll run the character through a collision line (red) of 1px.Java Code:else if ( aCollision == true ) { velocityX = 0; //System.out.println( "STOPPING CHARACTER (X)" ); }
First run:
Second run:Java Code:MOVING CHARACTER (X) RUNNING COLLISION CHECK (true) STOPPING CHARACTER (X)
Java Code:RUNNING COLLISION CHECK (false) MOVING CHARACTER (X)
SO as you can see, sometimes it works, other times it doesn't.Java Code:RUNNING COLLISION CHECK (false) MOVING CHARACTER (X)
- 08-04-2010, 04:16 PM #37
What is there about the times it doesn't? What is different? Why doesn't it stop?other times it doesn't.
The only info I see in your debug output is true one time and false another.
What other variables can you print out to show WHY the code works differently?
- 08-04-2010, 05:35 PM #38
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
I don't think there is any other variables. My class which implements Runnable calls
in the run( ) method.Java Code:gameUpdate( timePassed );
That above is game update. Then that callsJava Code:public void gameUpdate( long timePassed ) { if ( gameOver ) isRunning = false; player.update( timePassed ); }
(Yes I have modified it slightly) but it still doesn't always get it. Sometimes it does, other time's it doesn't. Yet the velocity is 0.1, the lowest it can be when you take into account it uses Math.round to get the exact pixel. I don't think there are any other variables, well there aren't any for sure as far as I know.Java Code:public void update( long timePassed ) { doUpdate = false; // call collision check if velocityX/Y has a value. if ( velocityX != 0 ) { aCollision = false; // Check for a collision. if ( velocityX > 0 ) aCollision = runCollisionCheck( "right" ); else if ( velocityX < 0 ) aCollision = runCollisionCheck( "left" ); System.out.printf( "RUNNING COLLISION CHECK (%s)\n", aCollision ); if ( aCollision == false ) { // So if velocity is distance / time, then // distance = velocity * time. System.out.println( "MOVING CHARACTER (X)" ); doUpdate = true; } else if ( aCollision == true ) { velocityX = 0; System.out.println( "STOPPING CHARACTER (X)" ); } } if ( velocityY == 0 ) { aCollision = false; aCollision = runCollisionCheck( "below" ); System.out.printf( "RUNNING COLLISION CHECK (Y0) (%s)\n", aCollision ); if ( aCollision == false ) { velocityY = 0.1f; System.out.println( "MOVING CHARACTER (Y0)" ); doUpdate = true; } animation.update( timePassed ); } // call collision check if velocityX/Y has a value. if ( velocityY != 0 ) { aCollision = false; // Check for a collision. if ( velocityY < 0 ) aCollision = runCollisionCheck( "top" ); else if ( velocityY > 0 ) aCollision = runCollisionCheck( "below" ); System.out.printf( "RUNNING COLLISION CHECK (Y) (%s)\n", aCollision ); if ( aCollision == false ) { // So if velocity is distance / time, then // distance = velocity * time. System.out.println( "MOVING CHARACTER (Y)" ); doUpdate = true; } else if ( aCollision == true ) { velocityY = 0; System.out.println( "STOPPING CHARACTER (Y)" ); } } if ( doUpdate ) { // So if velocity is distance / time, then // distance = velocity * time. x += velocityX * timePassed; y += velocityY * timePassed; animation.update( timePassed ); // elf's code is sex. // don't you think? :D } }
- 08-04-2010, 05:52 PM #39
By variables I mean: x, y, velocityX, velocityY, timePassed, etcI don't think there is any other variables
These variables (and maybe some others) control how your code executes.
You need to look at how these variables change. Are they changing unexpectedly or to values that you don't handle? Your debug output doesn't show them.
- 08-04-2010, 06:11 PM #40
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
elfywelfy - live streaming video powered by Livestream
Come to my livestream and I'll demonstrate, just tell me in the chat when you've connected.
Similar Threads
-
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
game code for any game
By deathnote202 in forum Java GamingReplies: 4Last Post: 06-10-2010, 08:06 AM -
Help! Game.
By MIA6 in forum New To JavaReplies: 4Last Post: 11-08-2009, 12:22 AM -
Game 21
By aRTx in forum Advanced JavaReplies: 3Last Post: 04-04-2009, 12:33 AM -
2D strategy game or 2D war game
By led1433 in forum Java 2DReplies: 5Last Post: 02-10-2009, 06:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks