| View previous topic :: View next topic |
| Author |
Message |
DiabloTerrorGF
Joined: 15 Jul 2005 Posts: 64
|
Posted: Mon Apr 17, 2006 4:25 am Post subject: Best way for collision pt. 2 |
|
|
| Quote: | | Code: |
function bulletCollide(Obj1X, Obj1Y, Obj1MX, Obj1MY, Obj2X, Obj2Y, Obj2MX, Obj2MY)
Obj1MidX=Obj1X+(Obj1MX/2)
Obj1MidY=Obj1Y+(Obj1MY/2)
Obj2MaxX=Obj2X+Obj2MX
Obj2MaxY=Obj2Y+Obj2MY
if Obj1MidX<Obj2MaxX+1 and Obj1MidX>Obj2X-1 and Obj1MidY<Obj2MaxY+1 and Obj1MidY>Obj2Y-1 then
return true
end
return false
end
|
This code tells you if the center of the first object is anywhere inside the bounds of the second object(return true).
Obj1X is the starting X, Obj1XM is the is the width of the rectangle/square and so on for the others. |
Ok, thats my old code, but does anyone have any better code for distuingishing hit detection between two rectangles? |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Mon Apr 17, 2006 3:06 pm Post subject: |
|
|
At the very least you are going to need 4 comparisons. The only way to go faster is to have the left and right sides of the box precomputed, but that will surely cost you somewhere else. eg in the movement code.
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
DiabloTerrorGF
Joined: 15 Jul 2005 Posts: 64
|
Posted: Mon Apr 17, 2006 3:44 pm Post subject: |
|
|
| Code: | int sprite_colide(SDL_Rect *rc, SDL_Rect *rc2) {
int i,z;
for( i = rc->x; i < rc->x+rc->w; i++) {
for(z = rc->y; z < rc->y+rc->h; z++) {
if(i >= rc2->x && i <= rc2->x+rc2->w && z >= rc2->y && z <= rc2->y+rc2->h) return 1;
}
}
return 0;
} |
This is taken from that Road Kill game for PSP. It's in C. I can't really understand it though. I know what everything does but I can never read that other people make really.
rc->w is the width of the image in pixels, rc->x would be the x location of the first pixel and so on.
Would that code be suffucient if converted to LUA? I can convert it myself, but it may take awhile, but if someone can read that code and make a LUA version of it for me, I'd be most greatful as I hate working with LUA for loops(Having no brackets and such give me a headache...) |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Mon Apr 17, 2006 3:48 pm Post subject: |
|
|
Stick with your own code. That check is very silly - it checks if any point in the source rectangle is inside the destination rectangle which isn't necessary when really you only need to check the corners. It's much slower than yours.
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
imhotep
Joined: 13 Dec 2005 Posts: 41
|
Posted: Wed Apr 26, 2006 1:15 am Post subject: |
|
|
| Code: | --attack area
-- x1---x2
-- | | player
-- | X | X=centre
-- | | --> collision when centre is hit by centre of enemy
-- y1---y2
collision=1
x1=xpos
x2=xpos+playerWidth
y1=ypos
y2=ypos+playerHeight
e_x1=enemyX
e_x2=enemyX+enemyWidth
e_y1=enemyY
e_y2=enemyY+enemyHeight
x_centre=xpos+( (x2-x1)/2 )
y_centre=ypos+( (y2-y1)/2 )
e_x_centre=enemyX+( (e_x2-e_x1)/2 )
e_y_centre=enemyY+( (e_y2-e_y1)/2 )
if math.abs(e_x_centre-x_centre)>=enemyWidth/2 then collision=0
elseif math.abs(e_y_centre-y_centre)>=enemyHeight/2 then collision=0
end |
worked for me... :) |
|
| Back to top |
|
 |
|