Best way for collision pt. 2

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
DiabloTerrorGF
Posts: 64
Joined: Fri Jul 15, 2005 11:44 pm

Best way for collision pt. 2

Post by DiabloTerrorGF »

Code: Select all

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?
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

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
DiabloTerrorGF
Posts: 64
Joined: Fri Jul 15, 2005 11:44 pm

Post by DiabloTerrorGF »

Code: Select all

int sprite_colide&#40;SDL_Rect *rc, SDL_Rect *rc2&#41; &#123;

	int i,z;
	for&#40; i = rc->x; i < rc->x+rc->w; i++&#41; &#123;
		for&#40;z = rc->y; z < rc->y+rc->h; z++&#41; &#123;
			if&#40;i >= rc2->x && i <= rc2->x+rc2->w && z >= rc2->y && z <= rc2->y+rc2->h&#41; return 1;
		&#125;

	&#125;

	return 0;

&#125;
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...)
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

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
imhotep
Posts: 41
Joined: Tue Dec 13, 2005 9:15 pm

Post by imhotep »

Code: Select all

--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+&#40; &#40;x2-x1&#41;/2 &#41;
y_centre=ypos+&#40; &#40;y2-y1&#41;/2 &#41;

e_x_centre=enemyX+&#40; &#40;e_x2-e_x1&#41;/2 &#41;
e_y_centre=enemyY+&#40; &#40;e_y2-e_y1&#41;/2 &#41;


	if math.abs&#40;e_x_centre-x_centre&#41;>=enemyWidth/2		then collision=0
	elseif math.abs&#40;e_y_centre-y_centre&#41;>=enemyHeight/2	then collision=0 
	
	end
worked for me... :)
Post Reply