# Tankyroo

Member

19

102 Neutral

• Rank
Member
1. ## 2D Collision detection, need some help.

Thanks for the replies everyone. Okay, I've taken some of what each of you guys said, and I think I have a very very basic version of what I want, the only problem is, it's only on the X-axis. Here is what I have so far: [CODE] if(p.p.intersects(c.c) && p.x + p.width <= c.x + 5 && p.x + p.width >= c.x - 5){ p.x -= p.xVelocity * 60 / 1000.0; p.xVelocity = 0.0f; } if(p.p.intersects(c.c) && p.x <= c.x + c.width + 5 && p.x >= c.x + c.width - 5){ p.x -= p.xVelocity * 60 / 1000.0; p.xVelocity = 0.0f; } if(p.p.intersects(c.c) && p.y + p.height >= c.y - 5 && p.y + p.height <= c.y + 15){ p.y -= p.yVelocity * 60 / 1000.0; p.yVelocity = 0.0f; p.onGround = true; } [/CODE] This was in the update function of the main class by the way. Please note that I'm only writing the collision code against one rectangle because this is just to learn the basics of collision detection. What's happening now is the player will stop when he hits either side on the X-axis (just like I want), but if he jumps onto the block colliding on the Y-axis, he continuously bounces up and down very slightly, making my "onGround" boolean go from true to false very rapidly so the player is unable to jump. What can I do to fix this? Also, I haven't done any collision detection on the bottom yet, but I'm assuming it will be similar to how I've done it on the X-axis. P.S. The multiplying the velocity by 60 / 1000.0 is to simulate multiplying it by delta as I've done in the player class. Please tell me if I'm misunderstanding this whole delta thing.
2. ## 2D Collision detection, need some help.

How would I check the collision on the lines? I've never worked with lines like that before. Thanks for the response!
3. ## 2D Collision detection, need some help.

Hey, I'm here to ask for help on a topic I'm sure a lot of people get stuck on, and that is collision detection. I'll describe what I'm having trouble with and all of the specifics below. I'm learning Java, and after making a couple simple games I decided it would be a good learning experience to make a platformer, but I'm having a lot of trouble so far. I'm using Java with the Slick2D library. So far I have a character (rectangle) that you are able to control with a very basic movement system (you can move left, right, and jump.) and now I'm going into what I can't put off any longer, collision detection. I've tried a few different methods that I came up with myself, and nothing is working, I've also tried looking for some tutorials on google, but I can't find what I'm looking for. I'm not trying to do anything fancy, I just want to be able to collide with and jump on another rectangle to create the feel of a solid block. What is the best way of doing this? I can't seem to get the player to stop at the right place, especially while jumping onto the block. Any and all help is appreciated, thanks for reading! Code below: Main class: [CODE] public class Main extends BasicGame{ Player p = new Player(); Cube c = new Cube(); public Main(String title) { super(title); } public static void main(String[] args) throws SlickException{ AppGameContainer app = new AppGameContainer(new Main("Game")); app.setTargetFrameRate(60); app.setDisplayMode(640, 480, false); app.start(); } @Override public void render(GameContainer gc, Graphics g) throws SlickException { p.paint(g); c.paint(g); } @Override public void init(GameContainer gc) throws SlickException { } @Override public void update(GameContainer gc, int delta) throws SlickException { p.update(60); c.update(60); p.keyListener(gc); } } [/CODE] Player class: [CODE] public class Player { Rectangle p; Cube c = new Cube(); int x, y, width, height, speed; double gravity, yVelocity; boolean jump, moveDown, moveLeft, moveRight, onGround, colliding; public Player(){ x = 50; y = 100; width = 64; height = 64; speed = 100; gravity = 500; p = new Rectangle(x,y,width,height); } public void update(long deltaTime){ double delta = deltaTime / 1000.0; if(jump) yVelocity = -400; if(moveLeft){ x -= speed * delta; } if(moveRight){ x += speed * delta; } if(x <= 0) x=0; if(y <= 0) y=0; if(x >= 640 - width) x = 640 - width; if(y >= 480 - width) onGround = true; else onGround = false; if(y > 480 - width) y = 480 - width; if(onGround && !jump) yVelocity = 0; y += yVelocity * delta; yVelocity += gravity * delta; p.setX(x); p.setY(y); } public void keyListener(GameContainer gc){ Input input = gc.getInput(); if(input.isKeyPressed(Input.KEY_W) && onGround || input.isKeyPressed(Input.KEY_SPACE) && onGround){ jump = true; }else{jump = false;} if(input.isKeyDown(Input.KEY_A)){ moveLeft = true; }else{moveLeft = false;} if(input.isKeyDown(Input.KEY_D)){ moveRight = true; }else{moveRight = false;} } public void paint(Graphics g){ g.drawRect(x,y,width,height); g.drawString("X/Y:" + x + "/" + y, 10, 25); g.drawString("onGround: " + onGround, 10, 40); } [/CODE] Cube class: [CODE] public class Cube { int x, y, width, height; double gravity, yVelocity; boolean onGround; Rectangle c; public Cube(){ x = 300; y = 100; width = 64; height = 64; gravity = 500; c = new Rectangle(x, y, width, height); } public void update(long deltaTime){ double delta = deltaTime / 1000.0; if(y >= 480 - width){ y = 480 - width; onGround = true; }else{onGround = false;} if(!onGround){ y += yVelocity * delta; yVelocity += gravity * delta; } c.setX(x); c.setY(y); } public void paint(Graphics g){ g.drawRect(x,y,width,height); } } [/CODE] The code is probably very messy and I'm sure I've done a lot of things the wrong way, so feel free to point those out for my future reference.
4. ## Simple way to rotate image?

Hey guys, I have a question, I'm trying to make a game and I need to rotate my ship image, what is the most simple way of doing this? Or am I better off just using four different images? Thanks.
5. ## [Java] Getting mouse coordinates

Hey guys, I was wondering, what is the best way to get the coordinates of the mouse? When I use Mouse.getX() and Mouse.getY(), the Y coordinate is wrong, it shows that 0 is at the bottom, instead of the top. How do I fix this? Or what is the best way to get the coordinates?
6. ## [Java] Detecting collision between rectangles between classes

[quote name='rip-off' timestamp='1324372662' post='4895635'] You are instantiating new Entities in the render method. These entities are not tested for collision, because they do not exist in the scope of that method. Instead, you are using the Entity "eo", which isn't drawn. Consider instantiating the Entities in the constructor or init() function, and placing them in a container e.g. ArrayList<Entity>. In the render method, loop through all the Entities and draw them. In the update method, loop through all the entities and test for collision. [/quote] Edit: Never mind, I finally got it, thank you!
7. ## [Java] Detecting collision between rectangles between classes

The title probably doesn't make much sense, but I'm having trouble detecting the collision between my player from my Main class and the monster from the Entity class. Problem: The collision isn't where the 'monster' rectangle(s) are drawn, the collision is at 0,0 but the 'monsters' are clearly at different coordinates. Since I've been having so much trouble and I'm not completely sure what's causing it, I'm just going to cope/paste both of my classes here. Main class: [code] package src; import org.lwjgl.input.Keyboard; import org.newdawn.slick.AppGameContainer; import org.newdawn.slick.BasicGame; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.SlickException; import org.newdawn.slick.geom.Rectangle; public class Main extends BasicGame{ int px = 100, py = 100; Entity eo = new Entity("", 0, 0, 0); Rectangle player = new Rectangle(px, py, 16, 16); public Main() { super("Test"); } public static void main(String[] args) throws SlickException{ AppGameContainer app = new AppGameContainer(new Main()); app.start(); } @Override public void render(GameContainer gc, Graphics g) throws SlickException { Entity m1 = new Entity("Monster1", 100, 400, 200); m1.render(gc, g); Entity m2 = new Entity("Monster2", 100, 550, 200); m2.render(gc, g); Entity m3 = new Entity("Monster3", 100, 500, 300); m3.render(gc, g); g.drawString("X:" + player.getX() + "\nY: " + player.getY(), 10, 40); //Draw player g.drawRect(px, py, 16 ,16); } @Override public void init(GameContainer gc) throws SlickException { } @Override public void update(GameContainer gc, int arg) throws SlickException { //Movement if(Keyboard.isKeyDown(Keyboard.KEY_W)) py -= 0.1; else if(Keyboard.isKeyDown(Keyboard.KEY_S)) py ++; else if(Keyboard.isKeyDown(Keyboard.KEY_D)) px ++; else if(Keyboard.isKeyDown(Keyboard.KEY_A)) px -= 0.1; player.setX(px); player.setY(py); // Collision if(player.intersects(eo.r)){ System.out.println("Collision"); }else{System.out.println("No collision");} } } [/code] And my Entity class: [code]package src; import org.newdawn.slick.BasicGame; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.SlickException; import org.newdawn.slick.geom.Rectangle; public class Entity extends BasicGame{ public String name; public int health, x, y; public Rectangle r = new Rectangle(x, y, 32, 32); public Entity(String n, int h, int posx, int posy){ super("Test"); name = n; health = h; x = posx; y = posy; } @Override public void render(GameContainer gc, Graphics g) throws SlickException { g.drawString(name, x-20, y-32); g.drawString("Health: " + health, x-20, y-16); g.drawRect(x, y, 32, 32); g.drawRect(x, y, 16, 16); } @Override public void init(GameContainer gc) throws SlickException { } @Override public void update(GameContainer gc, int i) throws SlickException { } } [/code] This draws the 'collision boxes' to where they need to be, I've made them bigger for testing purposes. But the actually point of collision is at 0,0 (top-left.) The code is messy, and I'm sure there are plenty of things wrong with it, but I've just been trying for the past two hours and can't get it to work, so it's far from what it needs to be. Any and all help is appreciated, thanks for taking the time to read this.
8. ## [java] How to create multiple instances of the same thing?

[quote name='Nice Try' timestamp='1323120752' post='4890837'] [quote name='Tankyroo' timestamp='1323114077' post='4890805'] Sorry for the late reply, I was busy yesterday. I kind of get what you're saying, but I'm still confused. Could you give an example of what the class would look like? [/quote] This response makes it apparent that you don't quite yet have a basic understanding of what object oriented programming is. That said, I'd read up a bit more on class structure in Java, and also hone your understanding of all the data types used in Java. Only reason I'm assuming this is because I had the EXACT same problem when I didn't have a full understanding of OOP and data types, regardless of the language. In my first game ever (written in C++), I was instantiating monsters by just saying something along the lines of: [code]monster = Monster(arguments)[/code] And then just manipulating the object associated with that variable 'monster' throughout my entire game. I never really knew how else to store those monster objects, other than assigning each individual monster to a variable like this. Instantiating a new monster is as easy as just saying [code]Monster(arguments)[/code] if you already have a Monster class, but it ultimately comes down to your own understanding of object oriented programming and knowing data types that will allow you to do different things with this small 1 line of code. What prh99 said, using an array, is a good solution, but requires you to have an understanding of your class object (Monster in your case) and an array type of data structure. Hope this helps. -Adam [/quote] Thanks for the advice, do you have any suggestions as to where I should read-up on the topic? or is it all the same and I should just google an article on it?
9. ## [java] How to create multiple instances of the same thing?

[quote name='prh99' timestamp='1323034349' post='4890496'] [quote name='Tankyroo' timestamp='1322976718' post='4890317'] This is something I've struggled with and ended up bypassing with the two games I have made, but I figure I better learn it sooner than later, so any help is appreciated, I'll try and be more specific below: What I'm trying to do is make multiple instances of the same thing, the most efficient way. (Ex: two of the same enemy on screen at once, or multiple collectibles on screen at once.) How do I do this? My best guess would be making a class that has all of the info, but I don't know how to lay that out, or draw it to the screen. I know I'm not describing it well but I hope someone can help. Thanks! Oh also, once I do draw them and everything, how do I destroy them? [/quote] A class would probably make the most since to represent an enemy etc, what you need is something to contain the reference to each instance you create, an array for example. As for what you store in that class, it depends on what you're representing. If it's an enemy you'd probably want to keep track of it's stats like health and attack power etc also more fundamentally it's location in on the screen and in your game map. You'd also functions to move it etc. To destroy a class in Java you can either use delete e.g delete obj; and set the reference to NULL or destroy references to it by just setting it to NULL and the JVM's garbage collection will get to it at some point. The former is probably better than the latter. Then when you update/redraw the scene you only draw enemies in the array who's reference isn't NULL. [/quote] Sorry for the late reply, I was busy yesterday. I kind of get what you're saying, but I'm still confused. Could you give an example of what the class would look like?
10. ## [java] How to create multiple instances of the same thing?

This is something I've struggled with and ended up bypassing with the two games I have made, but I figure I better learn it sooner than later, so any help is appreciated, I'll try and be more specific below: What I'm trying to do is make multiple instances of the same thing, the most efficient way. (Ex: two of the same enemy on screen at once, or multiple collectibles on screen at once.) How do I do this? My best guess would be making a class that has all of the info, but I don't know how to lay that out, or draw it to the screen. I know I'm not describing it well but I hope someone can help. Thanks! Oh also, once I do draw them and everything, how do I destroy them?
11. ## [java] Exporting jar.

[quote name='Kyan' timestamp='1322901903' post='4890083'] [quote]Once again, sorry for the bump, but I'm really stuck on this.[/quote] Relax, it's fine. [quote]And that fixed my previous error, but now what happens is when I try to open the jar file, the game window opens but closes immediately (Progress!). Any insight as to why?[/quote] First, what is it you are actually doing? Is this a simple "Hello, World!" but with your application requiring the other two as a test? We can help you better if you show us what it is you're trying to do - e..g what does the "main" portion of your application look like (in code)? That said, if you're certain your application isn't bailing immediately for some reason then don't worry about it. Second, are there no errors? In this same vein, are you passing on any exceptions (e.g. just using empty [i]catch(){}[/i] blocks) that might give an indication of a problem? I'm not trying to be obtuse but I think there's some confusion regarding what you've actually done at this point. So, if I read this aright you've: [list][*]Compiled your application successfully.[*]Packaged your application into a .jar[*]Packaged your dependencies (LWJGL and Slick) into the jar as well.[*]Altered the manifest so that your application can find your dependencies correctly.[*]Tried to run the application and it just "starts then stops".[*]??? output ???[/list] One of the key steps missing is what you did with the native libraries (.dll, .so, or .dynlib depending on your platform) as those are required for your application to run correctly. [/quote] That is what I've done, you have read it right. My dlls and everything are both in the jar in a folder called "lib" (just like they were in eclipse) and they are also outside of the jar with the same folder name just in case. This is the "main" portion of my code (If this isn't what you mean, I can post other stuff.): [code]public static void main(String[] args) throws SlickException{ AppGameContainer app = new AppGameContainer(new Platformer()); app.setDisplayMode(800, 600, false); app.start(); }[/code] And yes, when I run the .jar the only thing that happens is the window opens, and then closes immediately, before anything loads, so the glimpse that I see of it is just a while screen, no title or anything either. Also, about output, there are no errors or anything. Edit: It looks like I finally got it to work! Thank you everyone for your help! for people who might have this same problem, here is what I did: In command prompt I changed the directory to where everything is located. I then entered: >java -Djava.library.path=C:\j\lib -jar test.jar (C:\j\lib is where my library files are. lwjgl.jar, slick.jar, and all of the .dlls that go with it.) And that opened my program successfully! But, I didn't want to open command prompt to use my program every time. So in the same directory as my jar and the necessary files, I made a batch file named run.bat. The contents of the .bat are the same as I entered in command prompt. (java -Djava.library.path=C:\j\lib -jar test.jar) Now all I have to do is open run.bat and it will run my game! I hope this helps people who have a similar problem so they won't have to spend nearly five days trying to figure it out.
12. ## [java] Exporting jar.

Once again, sorry for the bump, but I'm really stuck on this.
13. ## [java] Exporting jar.

[quote name='m3rlino' timestamp='1322768005' post='4889552'] [quote name='Tankyroo' timestamp='1322766447' post='4889527'] I'm not sure what the rules say about bumping, but I still can't figure this out. [/quote] Hello. I think there are some problems in the Keyboard class in the library. did you compile it with Eclipse? If so you click with right button of the mouse on the project. Click properties. Then look for java build path... There should be everything you need about how to compile the program. Remember that you can run directly your application from eclipse without having to compile from the command line. Everything it should be automatic and should solve all your problems and lack of times about configurations. It may be that you lack some library or maybe they change the protection of some constants, maybe before they were public and now they are private and they should be accessed with getCONSTANT getter method. Remember about assembly dependencies too in the properties window. There you can specify what you want to export and in which directory. I hope I can help [/quote] I tried what you said, but I couldn't find anything that looked wrong. What I did was add this to my manifest: [code]Class-Path: lib/lwjgl.jar lib/slick.jar[/code] And that fixed my previous error, but now what happens is when I try to open the jar file, the game window opens but closes immediately (Progress!). Any insight as to why? Edit: I didn't only add that Class-Path code to the manifest, I also put the required files outside of the jar in the same folder, but I'm still having that problem where it opens and closes.
14. ## [java] Exporting jar.

I'm not sure what the rules say about bumping, but I still can't figure this out.
15. ## [java] Exporting jar.

Well I finally got the tutorial example to work (not completely sure how) and I'm still running into problems with my game, I tried doing it the same way as the one that just worked, but I get 47 errors, most of them are like this: [code] cannot find symbol symbol : variable Keyboard location: class src.Platformer // (Trying to compile another file to see if that will work, that's why the name isn't slickGame like in the original post.) if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) ^ method does not override or implement a method from a supertype[/code]