Basic Collision Detection help

Libraries, utilities, bootloaders...

Basic Collision Detection help

Postby zerosimms » Fri Jul 25, 2014 8:07 pm

Hey all.

I'm after a bit of help...
I'm using the gb.display.fillRect and drawRect methods and need some advice on how to create the simplest collision detection. I actually want my objects to pass through each other but detect the fact they have collided. Any pointers would be appreciated

Cheers
User avatar
zerosimms
 
Posts: 33
Joined: Thu May 01, 2014 5:34 pm

Re: Basic Collision Detection help

Postby adekto » Fri Jul 25, 2014 8:27 pm

box collision would be a way to go
so easiest way is to write a function like this but u can use other ways as well

Code: Select all
bool pointCheck(int pointX, int pointY,int boxX, int BoxY, int boxW, int boxH){
//check if point is inside box
if(pointX > boxX && pointX < boxX+boxW &&
pointY > boxY && pointY < boxY+boxH)
{
return true;
}
return false;
}


this sued work by just calling it, dont forget it will return a boolean.
u can edit this code slightly to do box to box check but i let u figure that out yourself ;)

hope this helps
User avatar
adekto
 
Posts: 448
Joined: Tue Feb 25, 2014 9:47 pm
Location: belgium

Re: Basic Collision Detection help

Postby zerosimms » Fri Jul 25, 2014 8:33 pm

Fantastic!
Thank you for that, makes sense I guess... Now lets see if I can put it into practice :)
User avatar
zerosimms
 
Posts: 33
Joined: Thu May 01, 2014 5:34 pm

Re: Basic Collision Detection help

Postby msevilgenius » Fri Jul 25, 2014 9:52 pm

Just a heads up, there are a couple of functions in the library for collisions: gb.collidePointRect and gb.collideRectRect, the last two functions in Gamebuino.cpp
Though they don't appear to be documented on the wiki for some reason.
User avatar
msevilgenius
 
Posts: 12
Joined: Tue Mar 25, 2014 11:10 am

Re: Basic Collision Detection help

Postby Tiash » Fri Jul 25, 2014 11:17 pm

That is another method, for test collisions between rectangles:
Code: Select all
int isRectCollision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
//rect1: start point (x1,y1) with height=h1 and width=w1
//rect2: start point (x2,y2) with height=h2 and width=w2
  return !( x2     >  x1+w1  ||
            x2+w2  <  x1     ||
            y2     >  y1+h1  ||
            y2+h2  <  y1     );
}
User avatar
Tiash
 
Posts: 18
Joined: Sat Jul 12, 2014 7:28 am
Location: Italy

Re: Basic Collision Detection help

Postby rodot » Sat Jul 26, 2014 7:51 am

Very nice method, I'm gonna update the library with yours as min was buggy and slow.
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: Basic Collision Detection help

Postby zerosimms » Sat Jul 26, 2014 9:19 am

Thank you guy's. @msevilgenius that's really handy to know, this is the issue I have with the Arduino IDE and Processing, neither of them give you clues to methods that exist :(
User avatar
zerosimms
 
Posts: 33
Joined: Thu May 01, 2014 5:34 pm

Re: Basic Collision Detection help

Postby rodot » Sat Jul 26, 2014 9:31 am

I didn't put the collision detection in the documentation because I was not sure if I would eventually include them or not. Also my rectangle-rectangle collision was not good.
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: Basic Collision Detection help

Postby adekto » Sat Jul 26, 2014 2:38 pm

zerosimms wrote:Thank you guy's. @msevilgenius that's really handy to know, this is the issue I have with the Arduino IDE and Processing, neither of them give you clues to methods that exist :(

yep totally agree, we need a better editor idk if netbeans can solve it, i use sublime text usually
User avatar
adekto
 
Posts: 448
Joined: Tue Feb 25, 2014 9:47 pm
Location: belgium

Re: Basic Collision Detection help

Postby inversesandwich » Sat Jul 26, 2014 11:43 pm

adekto wrote:
zerosimms wrote:Thank you guy's. @msevilgenius that's really handy to know, this is the issue I have with the Arduino IDE and Processing, neither of them give you clues to methods that exist :(

yep totally agree, we need a better editor idk if netbeans can solve it, i use sublime text usually


I love sublime text and the stino plugin! MariaMole (http://dalpix.com/mariamole) is a great IDE though, and I've only just realised that they've released a Ubuntu version of it. Yay!
User avatar
inversesandwich
 
Posts: 32
Joined: Sat Apr 19, 2014 10:34 pm
Location: United Kingdom

Next

Return to Software Development

Who is online

Users browsing this forum: No registered users and 32 guests

cron