simple collision

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
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

simple collision

Post by pielago »

Hi guys
I read some collision and to be honest they kind of confusing for people that its starting
Now is it possible "IF" someone could share the most simplest example in collision like
2 rec each 100x100 and once they touch each other for the score to add up?

once AGAIN THANK YOU GUYS GREAT FORUM !!!! :cool:

P.S
wonder if apply to bullets? simple as can be...
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: simple collision

Post by Sheepolution »

It's simple and logic thinking :D

If you have 2 squares, what needs to be true so you're sure they touch each other?
Well, the left side of the square 1, needs to be more to the left than the right side of square 2.
The right side of square 1, needs to be more to the right than the left side of square 2.
The upper side of square 1, needs to be higher than down side of square 2.
And the down side of square 1, needs to be lower than the upper side of square 2.

So you will end up with something like this:

Code: Select all


if left1 < right2 and right1 > left2 and up1 < down2 and down1 > up2 then
 collision = true
end 
The variables left, right, up and left is simply the x and y for left and up and x+width and y+height for right and down.
Although I prefer centering the image, because of the way how it turns around.
Attachments
mPQNZMx.png
mPQNZMx.png (117.13 KiB) Viewed 267 times
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: simple collision

Post by pielago »

thank you it works thank you so much :ultrahappy:
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: simple collision

Post by pielago »

Sorry, the board attachment quota has been reached. <---- for some reason the site won't let me upload my .love but well here it goes

can someone help me with a simple collision i have 2 different rectangles and i need them to NOT overlap ..
i have the rectangle that i control plus 2 more and i don't want them to overlap for some reason my code won't work and i need it asap
here its the code and hope someone could help me soon :(

Code: Select all

function love.load()
require 'blockss'
g=love.graphics
k={}
k.x=100
k.y=100
k.w=50
k.h=50
k.direction = 'right'
k.speed=200	
	
	u={}
	u.x=400
	u.y=100
	u.w=50
	u.h=50
	bbb:load()
end

function love.draw()
	bbb:draw()
	g.setColor(255,255,255,255)
	g.rectangle('line',k.x,k.y,k.w,k.h)
	g.rectangle('line',u.x,u.y,u.w,u.h)	
end

function love.update(dt)
	bbb:update(dt)
	if love.keyboard.isDown('w') then
		k.direction = 'up'
		blocks(k.x, k.y - k.speed * dt)
	elseif love.keyboard.isDown('s') then
		k.direction = 'down'
		blocks(k.x, k.y + k.speed * dt)
	end
	
	if love.keyboard.isDown('a') then
		k.direction = 'left'
		blocks(k.x - k.speed * dt, k.y)
	elseif love.keyboard.isDown('d') then
		k.direction = 'right'
		blocks(k.x + k.speed * dt, k.y)
	end
end

function blocks(xx,yy) 	
	if  CheckCollision(xx, yy, k.w, k.h, u.x, u.y, u.w, u.h)   then
		while CheckCollision(xx, yy, k.w, k.h, u.x, u.y, u.w, u.h)  do
			 if k.direction == 'left' then
					xx = xx - 1
				elseif k.direction == 'right' then
					xx = xx + 1
				elseif k.direction == 'up' then
					yy = yy - 1
				elseif k.direction == 'down' then
					yy = yy + 1
	end			
			end
	else
		k.x = xx
		k.y = yy
	end

end		
external lua

Code: Select all


bbb={}
function bbb:load()
	uu={}
	uu.x=400
	uu.y=300
	uu.w=100
	uu.h=50
end

function bbb:draw()
	g.setColor(255,0,0,255)
	g.rectangle('line',uu.x,uu.y,uu.w,uu.h)	
end

function bbb:update(dt)
	if love.keyboard.isDown('w') then
		k.direction = 'up'
		hits(k.x, k.y - k.speed * dt)
	elseif love.keyboard.isDown('s') then
		k.direction = 'down'
		hits(k.x, k.y + k.speed * dt)
	end
	
	if love.keyboard.isDown('a') then
		k.direction = 'left'
		hits(k.x - k.speed * dt, k.y)
	elseif love.keyboard.isDown('d') then
		k.direction = 'right'
		hits(k.x + k.speed * dt, k.y)
	end
	
end 

function hits(xx,yy) 
	if  CheckCollision(xx, yy, k.w, k.h, uu.x, uu.y, uu.w, uu.h)   then
		while CheckCollision(xx, yy, k.w, k.h, uu.x, uu.y, uu.w, uu.h)  do
			if k.direction == 'left' then
					xx = xx - 1
				elseif k.direction == 'right' then
					xx = xx + 1
				elseif k.direction == 'up' then
					yy = yy - 1
				elseif k.direction == 'down' then
					yy = yy + 1
				end			
			end
	else
		k.x = xx
		k.y = yy
	end

end

function CheckCollision(ax1,ay1,aw,ah, bx1,by1,bw,bh)
  local ax2,ay2,bx2,by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
  return ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1
end

User avatar
CRxTRDude
Prole
Posts: 41
Joined: Sun Dec 15, 2013 3:03 am
Location: Some island in the archipelago

Re: simple collision

Post by CRxTRDude »

Code: Select all

object's x = next x - ((next  x + [object's width/2]) % width of what you wanted to collide with.)
See the Source, it's useful for platformers, but it has stuff for collision as well.

I'm also confused on your code. Where is the player and where is the object to collide with?
~ CRxTRDude || Will be out of touch for a wee longer than expected. Will keep in touch with soon enough. Sorry bout that.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 0 guests