Reflection problems..

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Reflection problems..

Post 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
Attachments
Boo.png
Boo.png (16.07 KiB) Viewed 4338 times
Reflection!.png
Reflection!.png (10 KiB) Viewed 4397 times
Reflection.love
(931 Bytes) Downloaded 206 times
Last edited by GijsB on Wed Sep 07, 2011 5:33 am, edited 1 time in total.
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: Reflection problems..

Post 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

Code: Select all

Ray(ux, uy, ua)
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?
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: Reflection problems..

Post 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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Reflection problems..

Post 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:

Code: Select all

Mirrors[x..':'..y] = angle
Help us help you: attach a .love.
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: Reflection problems..

Post 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:

Code: Select all

Mirrors[x..':'..y] = angle
thanks, but it didnt do much, the reflection still glitches :(
stripwax
Prole
Posts: 39
Joined: Sun Jul 31, 2011 1:16 pm

Re: Reflection problems..

Post 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
bug.gif (4.33 KiB) Viewed 4224 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)
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: Reflection problems..

Post 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 :D, and the problem is i dont know how radians work D:
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Reflection problems..

Post 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!
Help us help you: attach a .love.
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: Reflection problems..

Post by GijsB »

ahaa...

so if i want to turn an image 180° i have to rotate it with math.pi?
Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: Reflection problems..

Post by Rad3k »

GijsB wrote:ahaa...

so if i want to turn an image 180° i have to rotate it with math.pi?
Correct :)
Post Reply

Who is online

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