# 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.

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?