Problem with making objects bounce from wall

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
JHB
Prole
Posts: 30
Joined: Tue Feb 12, 2013 6:15 pm

Problem with making objects bounce from wall

Post by JHB »

Hi,

Okay, I am trying to make somethink like a shooter. And i try to make an explosion were random pixels spawn, and fly in random directions everytime an enemy or the wall gets hit. That works fine. Now i want these random pixel to bounce from the wall so the dont "fly" out of the screen. I use this to make this happen:

Code: Select all

if #ex ~= nil and ex[e] ~=nil and ex[e][p] ~= nil then
	if ex[e][p].start_y < 0 then -- Top
		ex[e][p].speed_y = ex[e][p].speed_y*-1
	elseif ex[e][p].start_y > 240 then -- Bottom
		ex[e][p].speed_y = ex[e][p].speed_y*-1
	elseif ex[e][p].start_x < 0 then -- Left
		ex[e][p].speed_x = ex[e][p].speed_x*-1
	elseif ex[e][p].start_x > 400 then -- Right
		ex[e][p].speed_x = ex[e][p].speed_x*-1
	end
end

-- EDIT:
-- i already tried:
-- ex[e][p].speed_x = -ex[e][p].speed_x
-- but this just has the same affect
And technically this works also. The problem is that it seems that this code affect the way (position) the explosions are spawning if an explosion should start if a bullet hits a wall. I cant even tell in wich way, it seems that the explosion starts outsite of the screen (sometimes). It just dosent looks right.
Without that code, everything is fine, so i assume it's that code. I am looking at this for days now, and cant get why this should affect the spawnposition. mabe thare is an easy solution i just can't see so i am asking here for help.

I attach a .love file where this code is "activated". The files where the magic happend are "colliosion" and "boom"<- there is the code in.
Attachments
game.love
(40.9 KiB) Downloaded 112 times
Post Reply

Who is online

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