[SOLVED]multy rec 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

[SOLVED]multy rec collision!

Post by pielago »

Not sure why my rectangle tables wont collide?
I want the red rectangle to collide with the green rectangle!
here is what I mean hope anyone can help me :(
can for loop fix my problem or is there another way to do it base on how I made it???
Attachments
multy tables.love
(3.37 KiB) Downloaded 82 times
Last edited by pielago on Wed May 13, 2015 3:33 am, edited 1 time in total.
User avatar
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

Re: multy rec collision!

Post by redsled »

Hey there! The problem relies within actor.lua and the actor:draw().

This is the original code:

Code: Select all

function actor:draw()
		
	love.graphics.setColor(self.hurt.color)
	love.graphics.rectangle("line", self.hurt.x ,self.hurt.y , self.hurt.w, self.hurt.h)
	
	love.graphics.setColor(self.hit.color)
	love.graphics.rectangle("line", self.hurt.x+20 ,self.hurt.y , self.hit.w, self.hit.h)

end
Sure the self.hit is drawn at the self.hurt's x-position (+20 pixels) and self.hurt's y-position, but in reality:

Code: Select all

self.hit={x=0, y=0, w=30, h=10, color={255,0,0} }
...self.hit.x and self.hit.y are both set to 0. So the "hit box" is drawn at the self.hurt's positions, but when your calculating the collisions, your using actor.hits x-position and y-position; both of which are set to zero.

So, I all you really had to change is actor:update(dt) to this:

Code: Select all

self.hit.x = self.hurt.x+20
self.hit.y = self.hurt.y
if love.keyboard.isDown("a")  then
	self.hurt.x = self.hurt.x - self.fuel*dt
	self.hit.x = self.hit.x - self.fuel*dt
end
if love.keyboard.isDown("d")  then
	self.hurt.x = self.hurt.x + self.fuel*dt
	self.hit.x = self.hit.x + self.fuel*dt
end
Basically, all it does is set the self.hit's position to self.hurts position and adjust when the keys are pressed, just like the self.hurt does. Furthermore, you didn't set self.hit's position to self.hurt's in actor:draw(), all that was happening was that it was getting drawn there.

I'm not the best at answering questions, but if you have any, I'd be happy to answer them. :ultrahappy:
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: multy rec collision!

Post by pielago »

thanks for the answer thank you so much!!!!!
let me try to fix it and see if it works :p
else ill come back XD
btw I am trying to have 4 collisions together under actor but if i can make these 2 work than i am in a good page :p

BTW I dont need to touch the draw function right? leave it a it is? taking the extra pixels???

Code: Select all

love.graphics.setColor(self.hit.color)
   love.graphics.rectangle("line", self.hurt.x+20 ,self.hurt.y , self.hit.w, self.hit.h)
to this

Code: Select all

love.graphics.setColor(self.hit.color)
   love.graphics.rectangle("line", self.hit.x ,self.hit.y , self.hit.w, self.hit.h)
User avatar
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

Re: multy rec collision!

Post by redsled »

pielago wrote: BTW I dont need to touch the draw function right? leave it a it is? taking the extra pixels???

Code: Select all

love.graphics.setColor(self.hit.color)
   love.graphics.rectangle("line", self.hurt.x+20 ,self.hurt.y , self.hit.w, self.hit.h)
to this

Code: Select all

love.graphics.setColor(self.hit.color)
   love.graphics.rectangle("line", self.hit.x ,self.hit.y , self.hit.w, self.hit.h)
Np! Essentially, it would be doing the exact same thing, but I would personally change it to the latter piece of code.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 4 guests