Isometric shadow casting light.

Game development specific discussions.
Boromir
Posts: 451
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Isometric shadow casting light.

Postby Boromir » Mar 01, 2017 22:50

leopardpm wrote:looked at your demo - really nice! and yes, the problem is with the slope function... you can tell because the demo works when the block is in the 0 to 180 degree field from the mouse.... 181 - 359 degrees cannot compute correctly.... I would have to think, but my guess is that if ydiff OR xdiff is negative, then the signs of both have to be changed.... just a quick off the top of my head gut response though... i dont trust it! lol


Looks like you were right. All it took was using the absolute values in the angle calculation.
atan2(ydif, xdif)*(180 / 3.14)
atan2(abs(ydif), abs(xdif))*(180 / 3.14)
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Isometric shadow casting light.

Postby leopardpm » Mar 01, 2017 23:08

nicely done! In glancing at your code, there seems to me to be a way to streamline some things... especially the block of 8 IF...THEN statements - it 'seems' like it is very fast... I wonder how it would compare to dafhi's routine above... could give him a run for his money - but his is more robust in the ability to handle circles (albeit very slowly).

.. nice to know the algo works - that is satisfying! Thank you... are you going to use a version in your shadow routine for your game?
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Isometric shadow casting light.

Postby leopardpm » Mar 01, 2017 23:19

I was just thinking, if we can get the routine fast enough, then it may be that you could have multiple, fully dynamic lighting... as in, Eric carrying a torch, the light emanating from a few wisps in the woods, I would think that could really lend a good emotional effect from the ambiance on the player. Light is a good way to evoke emotions.... an shadowy, erie scene, a bright and cheery scene, etc
Boromir
Posts: 451
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Isometric shadow casting light.

Postby Boromir » Mar 02, 2017 0:38

leopardpm wrote:I was just thinking, if we can get the routine fast enough, then it may be that you could have multiple, fully dynamic lighting... as in, Eric carrying a torch, the light emanating from a few wisps in the woods, I would think that could really lend a good emotional effect from the ambiance on the player. Light is a good way to evoke emotions.... an shadowy, erie scene, a bright and cheery scene, etc

You took the words out of my mouth! That's exactly my thoughts. ;)

I'm working on some cleaning and optimizations but I'm using a 32 bit .bmpx image for the glow and don't know how to share it.
I suppose you can make your own 400x400 32 bit bitmap

right now this
paint img,(chx2+1,chy1+1),rgba(0,0,0,0),rgba(0,0,0,0)
doesn't work very well and sometimes leaks into the main glow creating flicker.

Code: Select all

'2d Quad-Shadow caster
'by Ezekiel Gutierrez and Leopardpm
'
'requires light.bmpx 400x400 for the glow
'
function getslope(byref xdif as integer,byref ydif as integer, _
                  x1 as integer,y1 as integer, _
                  x2 as integer,y2 as integer) as integer
        xdif = x2 - x1
        ydif = y2 - y1
        return atan2(abs(ydif), abs(xdif))*(180 / 3.14)
end function

sub makepoint(xdif as integer,ydif as integer,dist as integer, _
              x1 as integer,y1 as integer, _
              byref x2 as integer,byref y2 as integer)
    xdif=xdif*7000'massively in need of optimization
    ydif=ydif*7000'massively in need of optimization
    x2=x1+(ydif)  -(ydif+xdif)
    y2=y1-(xdif)  -(ydif-xdif)
end sub

screen 18,32
dim as integer x,y,x2,y2,xl,yl, _
               shx1,shy1,shx2,shy2,dist,xdif,ydif
dim as integer slope1,slope2,slope3,slope4
x=100 'box
y=100 '    co-ordinate 1
x2=110'box
y2=110'    co-ordinate 2

'create light glow from 32 bit bitmap
dim as any ptr light
light=imagecreate(400,400)
bload "light.bmpx",light
'====================================

do
screenlock
cls
getmouse xl,yl'put light at mouse

dim as any ptr img
dim as integer chx1,chy1,chx2,chy2,testval
'calculate differences for each point
slope1=getslope(xdif,ydif,x2,y,xl,yl)
slope2=getslope(xdif,ydif,x2,y2,xl,yl)
slope3=getslope(xdif,ydif,x,y,xl,yl)
slope4=getslope(xdif,ydif,x,y2,xl,yl)
'find two edge points for the shadow caster
'===================================================
if slope1>testval then testval=slope1:chx1=x2:chy1=y
if slope2>testval then testval=slope2:chx1=x2:chy1=y2
if slope3>testval then testval=slope3:chx1=x:chy1=y
if slope4>testval then testval=slope4:chx1=x:chy1=y2

if slope1<testval then testval=slope1:chx2=x2:chy2=y
if slope2<testval then testval=slope2:chx2=x2:chy2=y2
if slope3<testval then testval=slope3:chx2=x:chy2=y
if slope4<testval then testval=slope4:chx2=x:chy2=y2
'===================================================
img=imagecreate(641,481,rgba(0,0,0,0))'create temporary image array
put img,(xl-200,yl-200),light,alpha'   put light into it

getslope(xdif,ydif,chx1,chy1,xl,yl)'get slope difference for one edge of the shadow
makepoint(xdif,ydif,30,chx1,chy1,shx1,shy1)'create first point
getslope(xdif,ydif,chx2,chy2,xl,yl)'get slope difference for the other edge
makepoint(xdif,ydif,30,chx2,chy2,shx2,shy2)'create second point

'create shadow block
line img,(shx1,shy1)-(chx1,chy1),rgba(0,0,0,0)
line img,(shx2,shy2)-(chx2,chy2),rgba(0,0,0,0)
line img,(shx1,shy1)-(shx2,shy2),rgba(0,0,0,0)
line img,(chx1,chy1)-(chx2,chy2),rgba(0,0,0,0)
paint img,(chx2+1,chy1+1),rgba(0,0,0,0),rgba(0,0,0,0)

'draw glow
put (0,0),img,alpha
'draw shadow casting box
line (x,y)-(x2,y2),rgb(0,0,0),bf



screenunlock
sleep 1
loop until multikey(1)


leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Isometric shadow casting light.

Postby leopardpm » Mar 02, 2017 2:58

Boromir wrote:I'm working on some cleaning and optimizations but I'm using a 32 bit .bmpx image for the glow and don't know how to share it.
I suppose you can make your own 400x400 32 bit bitmap

can you change it to a PNG and use mediafire? Might not even need to change it to a PNG, i think any filetype is OK with MediaFire

here is an attempt to use my mediafire - try to save it in here:
https://www.mediafire.com/folder/9rqvk1dt8r0vb/Folder_Boromir

What does the light file look like? I Don't know what I am recreating?
Last edited by leopardpm on Mar 02, 2017 3:56, edited 1 time in total.
Boromir
Posts: 451
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Isometric shadow casting light.

Postby Boromir » Mar 02, 2017 3:45

How do I add a file to the folder? There doesn't seem to be an upload button.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Isometric shadow casting light.

Postby leopardpm » Mar 02, 2017 3:51

Boromir wrote:How do I add a file to the folder? There doesn't seem to be an upload button.
drats, can you make a mediafire account and share that folder?
Boromir
Posts: 451
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Isometric shadow casting light.

Postby Boromir » Mar 02, 2017 3:52

leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Isometric shadow casting light.

Postby leopardpm » Mar 02, 2017 4:03

got it! looks great! How fun is this?! good job... glad that you use Paint.net with the BMPX plugin... makes things so much easier to be able to use FB's regular BLOAD and still be able to get transparency.
Boromir
Posts: 451
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Isometric shadow casting light.

Postby Boromir » Mar 02, 2017 4:17

I remember getting really hyped when I found the paint.net plugin. I couldn't believe my own eyes. :)

There seems to be a bad bug in the code though. On my windows 64 bit it crashes on certain angles. On my 32 bit it lags up but remains running. Can you try moving the light slowly around the box and see if it crashes for you.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Isometric shadow casting light.

Postby leopardpm » Mar 02, 2017 4:37

Boromir wrote:I remember getting really hyped when I found the paint.net plugin. I couldn't believe my own eyes. :)

There seems to be a bad bug in the code though. On my windows 64 bit it crashes on certain angles. On my 32 bit it lags up but remains running. Can you try moving the light slowly around the box and see if it crashes for you.

I did notice that there was an issue at certain points, if you move the mouse very slowly around the 0 degree and 180 degree point... it doesn't crash for me, but is has some distorted effects.... it seems to actually 'reverse' the light source... what I mean is that the part that should be should shows the light, and the part that should be shoiwing the light, all goes dark... must be a simple calc issue, maybe even a precision issue in the angle conversion since it only does it at those two, opposite, angles.... I will put in some debugging code and print out the calculated angles on screen as I move around the mouse... we should see the error in them.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Isometric shadow casting light.

Postby leopardpm » Mar 02, 2017 4:40

I was wrong, it is not only at those two angles (0 and 180 degrees) but at all 4 of the 90 degree angles (0, 90, 180, 270)
Boromir
Posts: 451
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Isometric shadow casting light.

Postby Boromir » Mar 02, 2017 4:44

paint img,(chx2+1,chy1+1),rgba(0,0,0,0),rgba(0,0,0,0)

this is what creates the inverted light

But the bug I'm having actually effects the ability of the program to operate. I'm still trying to find the cause. Strange that it doesn't happen to you.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Isometric shadow casting light.

Postby leopardpm » Mar 02, 2017 4:52

drats, I will need to decipher your code better to be able to print out the correct numbers for the angles.... this will take a bit to do

wait! I think it is actually in the paint fill part! You are always choosing the point lower right (+1 x and +1 y) as the point to start painting - this point can also be outside the shadow block as well as on one of the lines delineating it... this might cause a crash? it definitely accounts for what I am seeing!

LOL - you found that already... well, change it and see if you still crash?

Do you crash at certain angles? or anything that is repeatable?
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Isometric shadow casting light.

Postby leopardpm » Mar 02, 2017 5:03

for fixing the paint issue, choose the point that is midway between opposing corners... but I can't figure out exactly which two coordinate variables would be opposing in your code:

Code: Select all

line img,(shx1,shy1)-(chx1,chy1),rgba(0,0,0,0)
line img,(shx2,shy2)-(chx2,chy2),rgba(0,0,0,0)
line img,(shx1,shy1)-(shx2,shy2),rgba(0,0,0,0)
line img,(chx1,chy1)-(chx2,chy2),rgba(0,0,0,0)
paint img,(chx2+1,chy1+1),rgba(0,0,0,0),rgba(0,0,0,0)

does this code draw each adjacent side of the shadow polygon in sequential order?

if so, then this should always choose a point in the box:

paint img, (abs(shx2-chx1),abs(shy1-chy2),rgba(0,0,0,0),rgba(0,0,0,0)

I am thinking your crash is a memory leak - since you are painting to an image buffer, it is overspilling out of the buffer somehow...

Return to “Game Dev”

Who is online

Users browsing this forum: No registered users and 3 guests