• What do you need help with? Version 1
    5,001 replies, posted
So I've finished the collision method but I'm unsure how to match the colour of the pixels I want to check with those that are picked up by the grabber? [cpp] 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; }[/cpp] So after this line System.out.println( returnS ); I need to check whether the pixels sent to the method: [cpp] if ( key == KeyEvent.VK_RIGHT ) { String typeArray[] = { "200 | 200 | 200232 | 232 | 232", "200 | 200 | 200232 | 232 | 232200 | 200 | 200" }; boolean collision = checkForCollision( 'r', typeArray ); if ( collision == false ) { ImageIcon i = new ImageIcon( "D:/PW/Java/right.png" ); still = i.getImage( ); dx = 1; } }[/cpp] match up to what is found in returnS.. how would I do that?
I think you should load the background image only once. This will make your program faster And use a colour structure instead of strings. This will make it easier for you to read and modify values.
[QUOTE=shill le 2nd;23715440]There you go. Equivalent code if you had a signum function defined: [cpp]public int SubtractOneHalfAndReturnSignum(double d) { d -= 0.5; return signum(d); }[/cpp][/QUOTE] Why the sign function?
[QUOTE=Vampired;23783833]I think you should load the background image only once. This will make your program faster And use a colour structure instead of strings. This will make it easier for you to read and modify values.[/QUOTE] The background only loads once and so do images, thanks for the note. I'll look into a colour structure, thanks for the advice! [editline]08:46PM[/editline] -snip- Recoding the framework before I get any further and it gets [I]more [/I]messy.
I'm trying to display a string on the screen in OGL. Currently I make a Bitmap, make a Graphics instance and draw a string on the bitmap with it. Then I lock the bitmap and push the resulting BitmapData into a byte array. Then I make a texture and load the array in. It works but as you can imagine it's kinda slow. So I'm wondering if there's a way to draw to an existing texture without having to make a new one with the modified Bitmap every time. What do you guys use for text? Using C#.
[QUOTE=WTF Nuke;23781367]C++, and I want to expand the array. At the start I declare the minimum amount of tiles (16 by 12) and then when I read the map, it might increase by one column or one row, so how would I expand it to do that?[/QUOTE] Well first off you don't want to use statically-sized arrays. You might want to use vector<vector<int>> my_tiles; You would create it something like this -- [CODE] #include <vector> ... void your_func() { vector<vector<int>> my_tiles; for(int i = 0; i < number_of_rows: i++) { my_tiles.push_back( vector<int>( number_of_columns)); } } [/CODE] You can access the elements as you normally would -- my_tiles[0][0] for row0,col0.. etc.. If you want to add more rows, all you would do is do my_tiles.push_back( vector<int>(number_of_columns); if you want to do more columns, you would do [CODE] for( vector<int>::iterator it = my_tiles.begin(); it != my_tiles.end(); it++) { it->insert(it->end(), number_of_cols_to_add, default_value); } [/CODE] If you're averse to using iterators, you can also do [CODE] for( int i = 0; i < my_tiles.size(); i++) { for( int col = 0; col < number_of_cols_to_add; col++) { my_tiles[i].push_back(default_value); } } [/CODE] The best thing to do would be to wrap the 2D tiles-matrix in its own class. [CODE]class TileArray { TileArray(int rows, int cols) { vector<vector<int>> tiles; // Populate the vector of vector of integers as before } void add_rows(int number_of_rows) { // row adding code as before } void add_cols(int number_of_cols) { // column adding code as before } int get_element(int row, int col) { // do bounds checking return tiles[row][col]; } void set_element(int row, int col, data) { // bounds checking tiles[row][col] = data; } };[/CODE] Then in the future you can just do [CODE] TileArray game_world(16,12); game_world.add_row(5); game_world.add_col(5); [/CODE] and your tile array is now 21 x 17
[QUOTE=ZeekyHBomb;23756667]In SFML you can simply use the Intersects-function.[/QUOTE] I'm able to to get a collision, and store the overlap rectangle in a sf::Rect, but I'm having trouble applying this to a platformer style game (that is, gravity moving down and X axis movement by input). [cpp]if(intersect.Bottom <= block.GetPosition().y) { SetY(intersect.Top - sprite.GetImage()->GetHeight()); } else if (GetFarX() <= intersect.Left) { SetX(intersect.Left - sprite.GetImage()->GetWidth()); } else { SetX(intersect.Left); } [/cpp] Is all I can come up with, but it doesn't work as I want it to. (SetX and SetY and just functions that save me typing sprite.SetPosition().x over and over, but do the same thing, GetFarX returns the position plus the width of the image)
If I'm doing a 2D, side-view based, game (that isn't tile based) would it be easiest to detect collisions using colour codes (which the mapper would have to define somewhere) or simply asking the mapper to draw over his map with certain colours. For example, there would be two images: one would always be the background of the application, where as the other would be drawn behind it and used to detect the collisions. As an example: [LIST=1] [*]There is a map with a flat floor (a line), and is grey. [*]The mapper then makes a new file, copies in the line and fills it say red. [*]The game knows that red means floor (to walk on); so it acts as a barrier. [/LIST] Or would it be easier: [LIST=1] [*]There is a map with a flat floor (a line), and is grey. [*]The mapper has to in some way specify which colour the line is and that it cannot be passed through. [*]The game has to then read this and make it act as a barrier. [/LIST] A tile system is basically impossible for this concept of maps, so I'm going to have to stick with pixel detection for collisions.
[QUOTE='[ApS] Elf;23800543']If I'm doing a 2D, side-view based, game (that isn't tile based) would it be easiest to detect collisions using colour codes (which the mapper would have to define somewhere) or simply asking the mapper to draw over his map with certain colours. For example, there would be two images: one would always be the background of the application, where as the other would be drawn behind it and used to detect the collisions. As an example: [LIST=1] [*]There is a map with a flat floor (a line), and is grey. [*]The mapper then makes a new file, copies in the line and fills it say red. [*]The game knows that red means floor (to walk on); so it acts as a barrier. [/LIST] Or would it be easier: [LIST=1] [*]There is a map with a flat floor (a line), and is grey. [*]The mapper has to in some way specify which colour the line is and that it cannot be passed through. [*]The game has to then read this and make it act as a barrier. [/LIST] A tile system is basically impossible for this concept of maps, so I'm going to have to stick with pixel detection for collisions.[/QUOTE] Well, I think the standard is to color the whole image magenta, draw the map and then filter out magenta in your game. [editline]05:33PM[/editline] Then you'd have to make a second image that is a background. [editline]05:34PM[/editline] Maybe that's what you said:tinfoil:
I meant like this: Mapper makes map like this: [IMG]http://ooft.net/upload/621005e.png[/IMG] Mapper makes collision objects colour (255, 0, 0): [IMG]http://ooft.net/upload/2225219e.png[/IMG]
[QUOTE=FerrisWheel;23796463]I'm able to to get a collision, and store the overlap rectangle in a sf::Rect, but I'm having trouble applying this to a platformer style game (that is, gravity moving down and X axis movement by input). [cpp]if(intersect.Bottom <= block.GetPosition().y) { SetY(intersect.Top - sprite.GetImage()->GetHeight()); } else if (GetFarX() <= intersect.Left) { SetX(intersect.Left - sprite.GetImage()->GetWidth()); } else { SetX(intersect.Left); } [/cpp] Is all I can come up with, but it doesn't work as I want it to. (SetX and SetY and just functions that save me typing sprite.SetPosition().x over and over, but do the same thing, GetFarX returns the position plus the width of the image)[/QUOTE] What does not work? You could try to watch some variables in the debugger or output them to view if the intersection positions are what you expect them to be.
I'm creating an applet in Elcipse using the following: [cpp]public class Frame extends JApplet { public Frame( ) { JFrame frame = new JFrame( "dev" ); frame.add( new GamePanel( ) ); frame.setTitle( "dev" ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setSize( 800, 600 ); frame.setVisible( true ); frame.setLocationRelativeTo( null ); frame.setPreferredSize( frame.getSize( ) ); //frame.setResizable( false ); } public static void main ( String args[] ) { new Frame( ); } }[/cpp] The only problem is when I run the code and the frame is generated in Eclipse, it is not correctly sized. Instead of being 800 x 600 and containing the full 800 x 600 image you have to manually resize the window so it fits. Is there any way to fix this?
[quote=http://download-llnw.oracle.com/javase/7/docs/api/java/awt/Frame.html]The size of the frame includes any area designated for the border. The dimensions of the border area may be obtained using the getInsets method, however, since these dimensions are platform-dependent, a valid insets value cannot be obtained until the frame is made displayable by either calling pack or show. Since the border area is included in the overall size of the frame, the border effectively obscures a portion of the frame, constraining the area available for rendering and/or displaying subcomponents to the rectangle which has an upper-left corner location of (insets.left, insets.top), and has a size of width - (insets.left + insets.right) by height - (insets.top + insets.bottom).[/quote]
Thanks, my google search terms never work :frown:
I'm planning to redesign my DROD Roguelike tile system. Actually there's a 3 dimension array of tiles : Tile[,,] Tiles Tiles[X, Y, Z] -- Z can be 0, 1 or 2. 0 is for the moving entities, enemies, player. 1 is for middle elements. 2 s for floor/wall/pit, etc. However this isn't elegant and doesn't work well, expecially when I want to add a new layer. Adding a new layer requires the game loop to cycle through a lot more tiles. Would a better solution be having a List<Entity> in each Tile, and have a 2 dimensional tile array? Or is there anything else that's even better?
What would be the possible reasons for my game to stutter? I do the same thing for a couple of seconds (panning the camera) and it doesn't move smoothly. It's not rendering anything different, just a simple grid. I have this build in: [cpp] Thread.Sleep( (int)( 16 - UpdateTime * 1000 ) > 0 ? (int)( 16 - UpdateTime * 1000 ) : 0 );[/cpp] and I trace the amount it sleeps, it's 15 but changes to something lower every few frames (I don't think it ever drops to 0) There, I uploaded it if I didn't explain it very well. [url=http://anyhub.net/file/isorts.rar][img]http://anyhub.net/thumb?id=54405[/img][/url]
Tried running your app, but I don't have the required .net framework. Anyway... so from your snippet it looks like you want to update your main loop every 16ms. I assume that updateTime is the amount of time (in seconds as a float(?)...) since the last update? Are you double buffering? Have you tried lowering the time between updates?
UpdateTime is the time spent inside the OnUpdate function so it sleeps the rest of the time remaining to make it update every 16 ms. Hmm, now that I think of it, that doesn't account for the time spent in rendering. That might be the problem.
[QUOTE=Darwin226;23807724][img]http://anyhub.net/thumb?id=54405[/img][/QUOTE] people actually use anyhub?
Thanks for the help inside. Another problem. It's about the same game. So I am trying to make the main character( a mage) shoot magic balls. It's supposed to shoot a ball when you press mouse 1, and shoot it at the mouse's location. It works, but only if I am to the right and below the characters location. It also sometimes is too fast (supposed to be at a fixed 250 pixels per second max). Can you help? Here is the code: [cpp]void castspell::move(float t){ if(mainch.firefirst){ x = App.GetInput().GetMouseX(); y = App.GetInput().GetMouseY(); sx = mainch.you.GetPosition().x + 30; sy = mainch.you.GetPosition().y + 40; spell.SetPosition(sx, sy); x = sx - x; y = sy - y; if (x > y){ y = y / x; x = 1; } else { x = x / y; y = 1; } x *= 250; y *= 250; mainch.firefirst = false; } spell.Move(x * t, y * t); }[/cpp]
Okay, so I'm still working on that pixel collision detection. I came around making this function: [cpp] private int runCollisionCheck( ) { 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 ( i == 0 ) { 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 < imageHeight; j++ ) { playerY = Math.round( y + j ); 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 0; } } // check left. else if ( i == 1 ) { pixels = new int[ 1 * 1 ]; playerX = Math.round( x - 1 ); // inner loop. check every pixel on the right side of the player. for ( int j = 0; j < imageHeight; j++ ) { playerY = Math.round( y + ( imageHeight + j ) ); 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 1; } } // check below. else if ( i == 2 ) { pixels = new int[ 1 * 1 ]; playerY = Math.round( y - 1 ); // inner loop. check every pixel on the right side of the player. for ( int j = 0; j < imageWidth; j++ ) { playerX = Math.round( x + ( imageHeight + j ) ); 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 2; } } // check below. else if ( i == 3 ) { pixels = new int[ 1 * 1 ]; playerY = Math.round( y - 1 ); // inner loop. check every pixel on the right side of the player. for ( int j = 0; j < imageWidth; j++ ) { playerX = Math.round( x + j ); 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 3; } } } return 5; }[/cpp] It's run in the sprites update: [cpp] // change position. public void update( long timePassed ) { // call collision check if velocityX/Y has a value. if ( velocityX > 0 || velocityY > 0 ) { // Check for a collision. aCollision = runCollisionCheck( ); System.out.printf( "RUNNING COLLISION CHECK (%s)\n", aCollision ); if ( aCollision == 5 ) { System.out.printf( "MOVING CHARACTER (%s)\n", timePassed ); // So if velocity is distance / time, then // distance = velocity * time. x += velocityX * timePassed; y += velocityY * timePassed; // sprite's animation frame. animation.update( timePassed ); } if ( aCollision == 0 || aCollision == 1 ) { velocityX = 0; System.out.println( "STOPPING CHARACTER (X)" ); } if ( aCollision == 2 || aCollision == 3 ) { velocityY = 0; System.out.println( "STOPPING CHARACTER (Y)" ); } } }[/cpp] Though I figured the for loops create so much lag that the sprite wont move (yes, it prints MOVING CHARACTER but the character doesn't move at all). I think this is due to the lag it generates, but I'm really not sure. Any ideas? [url]http://anyhub.net/file/main.zip[/url] full source code if needed.
Offtopic. What are the code tags?
[.cpp][./cpp] [.code][./code] without .'s.
Thank you.
[QUOTE='[ApS] Elf;23812367'] Though I figured the for loops create so much lag that the sprite wont move (yes, it prints MOVING CHARACTER but the character doesn't move at all). I think this is due to the lag it generates, but I'm really not sure. Any ideas? [/QUOTE] I betcha it's the debug prints that makes it lag.
[QUOTE=ralle105;23813267]I betcha it's the debug prints that makes it lag.[/QUOTE] I had to make a batch script to delete all of my temp folders. It was funny because maximizing the window would cause it to take about .5s per file. Making it as small as possible would make it run at about .1s per file :v:
[QUOTE=haushippo;23812248]people actually use anyhub?[/QUOTE] Since it's fucking awesome, yes. [editline]06:36PM[/editline] [QUOTE=Darwin226;23807724]What would be the possible reasons for my game to stutter? I do the same thing for a couple of seconds (panning the camera) and it doesn't move smoothly. It's not rendering anything different, just a simple grid. I have this build in: [cpp] Thread.Sleep( (int)( 16 - UpdateTime * 1000 ) > 0 ? (int)( 16 - UpdateTime * 1000 ) : 0 );[/cpp] and I trace the amount it sleeps, it's 15 but changes to something lower every few frames (I don't think it ever drops to 0) There, I uploaded it if I didn't explain it very well. [url=http://anyhub.net/file/isorts.rar][img]http://anyhub.net/thumb?id=54405[/img][/url][/QUOTE] I'm getting no stuttering, unless you mean you can't hold down the keys and have it continually move, but I'm pretty sure you don't. The only weird thing I notice is your grid lines get thicker when the camera is panning.
Meh, it's not that bad all in all. I'll try to fix it up some more latter.
I just compiled SFML for VS2010. When I try to run my program, I get a single warning: [code]Warning 1 warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library C:\Users\Jake\Documents\Visual Studio 2010\Projects\Roguelike\Roguelike\LINK Roguelike [/code] What do I need to do?
[QUOTE=NorthernGate;23815410]Since it's fucking awesome, yes.[/QUOTE] 50% uptime and broken mimetypes are fucking awesome. totally. give up and use dropbox.
Sorry, you need to Log In to post a reply to this thread.