Page 1 of 1
Reflection problems..
Posted: Tue Sep 06, 2011 6:59 pm
by GijsB
I made a reflection raycasting-thingy
everything works, exept when you raycast in a horizontal direction...
(change ray direction with 'a' and 'd', make mirrors with the mouse)
my .love and reflection formula in a .png, and 1 image that shows the problem
Re: Reflection problems..
Posted: Tue Sep 06, 2011 8:59 pm
by kraftman
I havn't worked out the solution yet, still reading through the code, but a couple of notes on efficiency:
If you only change the image data when the mouse/keyboard is pressed, then you only need to generate a new image after these events, rather than every frame.
In this case you need to move
Code: Select all
DImage = love.graphics.newImage(ImageD)
DImage:setFilter("nearest","nearest")
to the end of Ray() instead of love.draw()
Also,
Code: Select all
Ray(px,py,A)
local moved = 1
local ux = px
local uy = py
local ua = A
is fairly redundant, since you could just do
Less important, but still worth noting, is that if you define the map pixel function outside of Ray(), it will only be defined once, instead of every time Ray() is called, so its less work for GC.
Which bit do you think is calculating the horizontal mirror?
Re: Reflection problems..
Posted: Tue Sep 06, 2011 9:17 pm
by GijsB
it has to do with the collision detection :
1)get next position of the ray
2)check if a mirror, if so
2.1)calculate reflection =
angle mirror+(angle mirror-ray angle)
the angle of the mirror is detected like this :
every time you draw a mirror, the position of every pixel of the mirror is stored in Mirrors, with the angle like =
Mirrors[x..y] = angle
2.2)update the begin positions, and the moves to zero again.
3) if not keep on going with the ray
Re: Reflection problems..
Posted: Tue Sep 06, 2011 11:17 pm
by Robin
GijsB wrote:every time you draw a mirror, the position of every pixel of the mirror is stored in Mirrors, with the angle like =
Mirrors[x..y] = angle
That's pretty awful. Your code can't distinguish between (23, 5) and (2, 35), for instance. At least do something like:
Re: Reflection problems..
Posted: Thu Sep 08, 2011 5:42 pm
by GijsB
Robin wrote:GijsB wrote:every time you draw a mirror, the position of every pixel of the mirror is stored in Mirrors, with the angle like =
Mirrors[x..y] = angle
That's pretty awful. Your code can't distinguish between (23, 5) and (2, 35), for instance. At least do something like:
thanks, but it didnt do much, the reflection still glitches
Re: Reflection problems..
Posted: Wed Sep 14, 2011 9:38 pm
by stripwax
I must be missing something. This .love does nothing for me, just a white screen. Any ideas?
EDIT: seems to be due to using non-power-of-two image resolutions. I can make the code work on my machine if I change the resolution to use 512x512 instead.
Question: why are you not doing this by just creating a list of mirrors as startx,starty,endx,endy coordinates? Surely by trying to do the reflection on a pixel grid with a 1-pixel-wide mirror line means you're just going to get beams that 'pass through' the mirrors when the mirror pixels have gaps between them (i.e. on diagonals) and do completely weird things. Certainly that seems to happen with your current implementation. See bug illustrated below. The diagonal completely passes through the mirror.
- bug.gif (4.33 KiB) Viewed 4230 times
Question2: Why does the line reflect if the mirror is horizontal, but 'pass through at a different angle' if the mirror is vertical? It looks like you're using degrees for the incident angle (A or ua) and radians for the mirror angle (OA or sa). You can't just add degrees to radians. If you put math.deg(sa) into Mirrors, or, alternatively, just use radians everywhere, it works fine. (above screenshot taken with that fix in place, and also the fix to use power-of-two imagedata)
Re: Reflection problems..
Posted: Thu Sep 15, 2011 1:54 pm
by GijsB
stripwax wrote:I must be missing something. This .love does nothing for me, just a white screen. Any ideas?
EDIT: seems to be due to using non-power-of-two image resolutions. I can make the code work on my machine if I change the resolution to use 512x512 instead.
Question: why are you not doing this by just creating a list of mirrors as startx,starty,endx,endy coordinates? Surely by trying to do the reflection on a pixel grid with a 1-pixel-wide mirror line means you're just going to get beams that 'pass through' the mirrors when the mirror pixels have gaps between them (i.e. on diagonals) and do completely weird things. Certainly that seems to happen with your current implementation. See bug illustrated below. The diagonal completely passes through the mirror.
bug.gif
Question2: Why does the line reflect if the mirror is horizontal, but 'pass through at a different angle' if the mirror is vertical? It looks like you're using degrees for the incident angle (A or ua) and radians for the mirror angle (OA or sa). You can't just add degrees to radians. If you put math.deg(sa) into Mirrors, or, alternatively, just use radians everywhere, it works fine. (above screenshot taken with that fix in place, and also the fix to use power-of-two imagedata)
wow thanks
, and the problem is i dont know how radians work D:
Re: Reflection problems..
Posted: Thu Sep 15, 2011 5:22 pm
by Robin
Radians on Dutch Wikipedia:
Radiaal_(wiskunde).
0 radians = 0°
1 radian = 57.3° (not exactly, but something like that)
½ pi radians = 90°
pi radians = 180°
1½ pi radians = 270°
2 pi radians = 360°
As you will learn later in school, radians are very useful. If you start using them now, you'll have a head start on your classmates!
Re: Reflection problems..
Posted: Thu Sep 15, 2011 6:03 pm
by GijsB
ahaa...
so if i want to turn an image 180° i have to rotate it with math.pi?
Re: Reflection problems..
Posted: Thu Sep 15, 2011 6:46 pm
by Rad3k
GijsB wrote:ahaa...
so if i want to turn an image 180° i have to rotate it with math.pi?
Correct