Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

collision detection -java

  • 21-03-2015 2:08pm
    #1
    Closed Accounts Posts: 768 ✭✭✭


    Hi all just working on a (crap :) ) game just to wrap my head around game loops etc.

    Have a brick class and am using drawRect() method to draw it on screen.
    How would I go around checking for collisions?

    The brick class doesnt extend Rectangle so dont have access to the intersect() method.

    for what its worth heres the brick class code:
    public class Brick  {
    	
    	private int width;
    	private int height;
    	private boolean isAlive;
    	private Color colour;
    	private int speed;
    	private int xPos;
    	private int yPos;
    	
    
    	//constructor
    	public Brick(int w,int h,boolean isAlive,Color colour,int spd,int xPos,int yPos){
    		this.width = w ;
    		this.height = h ;
    		this.isAlive = isAlive;
    		this.colour = colour;
    		this.speed = spd ;
    		this.xPos = xPos;
    		this.yPos = yPos;
    	}
    }//end class
    

    Any ideas?


Comments

  • Registered Users, Registered Users 2 Posts: 7,264 ✭✭✭witnessmenow


    Basically you could need to check are the x,y coordinates of something within the bounds of the rectangle

    I intentionally left the Y part for you to do!
    public boolean checkForCollision(int x, int y)
    {
        int rightSideX = this.xPos + this.width;
    
        if(x >= this.xPos && x <= rightSideX )
        {
               if(#Do the same for y)
               {
                      return true; 
               }
        }
        
        return false;
    }
    

    to check for collision between two bricks would be something more like this
    public boolean checkForCollision(Brick otherBrick)
    {
        int rightSideX = this.xPos + this.width;
    
        int otherBricksRightSideX = otherBrick.xPos + otherBrick.width;
    
        //Its probably easier to figure out this works by sketching it out
        //Draw the two rectangles out in a few scenarios and go through the logic
        if(otherBricksRightSideX >= this.xPos && otherBrick.xPos <= rightSideX )
        {
    
               if(#Do the same for y)
               {
                      return true; 
               }
        }
        
        return false;
    }
    

    Can I ask why you are using pure Java as opposed to something like Libgdx?


  • Closed Accounts Posts: 768 ✭✭✭SpaceSasqwatch


    Thanks witness for replying.Will implement that code later.Was thinking something along the same lines.

    As for just using pure java,youre not the first person to say that to me :)
    Basically I just want to get a good grip of java before heading onto using game libaries.I plan on using just java up as far as using sprites for a simple 2d platformer again just for the knowledge more than anything before I start work on my Call Of Duty killer game :)


  • Registered Users, Registered Users 2 Posts: 7,264 ✭✭✭witnessmenow


    Sure let me know if its not working I'll take another look.

    Just personally I think something like Libgdx is a good way to learn game development/programming.

    I looks after some stuff for you but you still need to do the majority of coding yourself, like when you are developing with LibGDX its very clearly still Java. The real nice part about is deploying to mobiles etc

    Good luck with it either way :)


  • Moderators, Category Moderators, Computer Games Moderators Posts: 52,410 CMod ✭✭✭✭Retr0gamer


    Haven't been catching up in a while here but checking if boxes overlap every frame has some big disadvantages if your game isn't very slow moving, such as hitboxes passing right through each other. Might be better off implimenting a vector based collision system. The guys that made N+ have a great tutorial on it but you will likely need a bit more help with it as well. It's not an easy thing to impliment but it's a great coding exercise, gives youa vastly more powerful collision detection system and will give you some basics if you go to 3D since you are basically treating your shapes as polygons.

    I did one myself that works with triangles and square shapes although it could do with some optimisation. It wasn't easy to do especially when I was hard coding scrolling as well but it was essential considering how fast my game idea would be. Took about 2 months but I was working at the time and hadn't weekends free.

    Unfortunately after I had the whole collision system working and had the hard work all done I ended up getting very busy with work and didn't go back to the project :/ It would take me a week now to get my head back around the code.


  • Registered Users, Registered Users 2 Posts: 8,493 ✭✭✭RedXIV


    Wise coder always say:
    "Code should be heavily commented, you will inevitably forget what madness possessed you as you wrote it" :D


  • Advertisement
  • Moderators, Category Moderators, Computer Games Moderators Posts: 52,410 CMod ✭✭✭✭Retr0gamer


    RedXIV wrote: »
    Wise coder always say:
    "Code should be heavily commented, you will inevitably forget what madness possessed you as you wrote it" :D

    Oh it's heavily commented alright, it's just the maths is very confusing behind it.


Advertisement