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