how i collide with other rectangle/circle/sprite

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
vitail
Prole
Posts: 26
Joined: Thu Apr 09, 2015 8:37 pm

how i collide with other rectangle/circle/sprite

Post by vitail »

i'm searching and i don't found this, i only founded this and i don't know how works

Code: Select all

function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
  return x1 < x2+w2 and
         x2 < x1+w1 and
         y1 < y2+h2 and
         y2 < y1+h1
end
how i can use that with an if statement and a object who moves all the time(ball of pong),

EDIT: now i know how to use, but sometimes he dont collide, here is the code
it is in the update function

Code: Select all

	--BALL
	if(ball.x + xa + (ball.w / 2) > love.window.getWidth()) then
		xa = -1
	elseif(ball.x + xa - (ball.w / 2) < 0) then
		xa = 1
	end

	if(ball.y + ya + (ball.h / 2) > love.window.getHeight()) then
		ya = -1
	elseif(ball.y - (ball.h / 2) + ya < 0) then
		ya = 1
	end

	if(CheckCollision(ball.x,ball.y,ball.w,ball.h,  paddle1.x,paddle1.y,pWidth,pHeight)) then 
		xa = 1;
	end

	if(CheckCollision(ball.x,ball.y,ball.w,ball.h,  paddle2.x,paddle2.y,pWidth,pHeight)) then 
		xa = -1;
	end

	ball.x = ball.x + xa * dt * ball.spd
	ball.y = ball.y + ya * dt * ball.spd
vitail
Prole
Posts: 26
Joined: Thu Apr 09, 2015 8:37 pm

Re: how i collide with other rectangle/circle/sprite

Post by vitail »

Jasoco wrote:This might help:

http://2dengine.com/doc/gs_intersection.html

Something more easy and efficient?
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: how i collide with other rectangle/circle/sprite

Post by Jasoco »

There's nothing difficult about the code on that page.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: how i collide with other rectangle/circle/sprite

Post by ivan »

Hey thanks for the mention Jasoco,
Can't take full credit for the code as some of it was ported from Anderson's Real-Time Collision detection.
Vitail, I admit that some of the code could be optimized,
but the general idea is:
1.find the nearest point Q from the center of circle C on the area of rectangle R
(if the circle center C is inside the rectangle R, then you can skip step 2)
2.compare the radius of circle C to the distance |C-Q|
If you want to visualize the problem, imagine the circle shrinking to a point while the rect is simultaneously expanding in all directions.
That's it. :)
Leap of Time
Prole
Posts: 23
Joined: Fri Sep 11, 2015 9:20 pm

Re: how i collide with other rectangle/circle/sprite

Post by Leap of Time »

Or you can just make it easy and understandable:

Code: Select all

-- my collision function, you can just copy and paste it in

function collision(x1, y1, width1, height1, x2, y2, width2, height2)
	if x2 + width2 > x1 and x2 < x1 + width1 then
		if y2 + height2 > y1 and y2 < y1 + height1 then
			return true
		end
	end
	return false
end
returns true if the two boxes ( they cant be cicles or anything else, they have to have 4 points in linear lines) intercect with each other.

If you want to use it you can do it like this:

Code: Select all

[[you have one thing that is on x: 32 and y: 32, it is 32x32, then you have another box at x: 48 and y 48, it is 32x32 too. so they DO intercect]]
if collision(32, 32, 32, 32, 48, 48, 32, 32) then --do stuff
it will return true in this case.
vitail
Prole
Posts: 26
Joined: Thu Apr 09, 2015 8:37 pm

Re: how i collide with other rectangle/circle/sprite

Post by vitail »

Leap of Time wrote:Or you can just make it easy and understandable:

Code: Select all

-- my collision function, you can just copy and paste it in

function collision(x1, y1, width1, height1, x2, y2, width2, height2)
	if x2 + width2 > x1 and x2 < x1 + width1 then
		if y2 + height2 > y1 and y2 < y1 + height1 then
			return true
		end
	end
	return false
end
returns true if the two boxes ( they cant be cicles or anything else, they have to have 4 points in linear lines) intercect with each other.

If you want to use it you can do it like this:

Code: Select all

[[you have one thing that is on x: 32 and y: 32, it is 32x32, then you have another box at x: 48 and y 48, it is 32x32 too. so they DO intercect]]
if collision(32, 32, 32, 32, 48, 48, 32, 32) then --do stuff
it will return true in this case.
hey, thanks for the answer, but that code sometimes it doesn't work if i'm moving the paddle in my pong game, or sometimes i dont move the paddle and he dont collide.
Post Reply

Who is online

Users browsing this forum: keharriso and 214 guests