Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Realtime 2D Shadow problem

Author
Message
janbo
16
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 14th Feb 2014 21:25 Edited at: 3rd Oct 2017 21:45
Hello, I made a 2D shadow casting algorithm after i found the needed triangle drawing algorithm.

The shadows behave a bit strange and I can't find the problem, because all functions works great if I use them separately, but if i put them together.. this happens:


Working code:




Thank you in advance.

Maybe you could help me render the shadows into an image and blend them with other lights too.

</div>

Attachments

Login to view attachments
GIDustin
16
Years of Service
User Offline
Joined: 30th May 2008
Location:
Posted: 14th Feb 2014 22:38
I was curious how you were doing this, so I copied the code and started manipulating it to work in DBP. I noticed you call drawLine(), but drawLine() doesn't exist. Line() does though...

Is drawLine an AppGameKit function? And what is Line() used for?

GIDustin
16
Years of Service
User Offline
Joined: 30th May 2008
Location:
Posted: 14th Feb 2014 23:18 Edited at: 14th Feb 2014 23:37
Switch this code:


for this code:


It looks like the +/- signs in the first two blocks are backwards. In addition, you have checks for greater than or less than, but none of your conditions account for "equal to", so I added those

Edit: It also looks much better when you triple the LightRange variable. For me anyway, the shadow only goes so far before stopping, which can look weird at times. Put your mouse a little bit above the top left corner of a box, then slowly move right. At the half-way point the shape of the shadow shifts pretty dramatically.

janbo
16
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 15th Feb 2014 14:27 Edited at: 3rd Oct 2017 21:45
Hey thanks for the fast answer, i'm aware about the "equal to" problem I just didn't implemented it jet.
And even your code wouldn't look real because then we calculate only diagonal corners but never orthogonal corners so let me fix that later

I made the Line() function for playing with memblocks the code is present, but i commented it.
And the drawline() is from AppGameKit v2.. so i'm sorry.

[Edit]
Wow i did't believe that your code would work because i was sure i tried this before... now it works great thank you very much
Changed my Code but have no time for the horizontal shadow's jet.
[/Edit]

</div>

Attachments

Login to view attachments
GIDustin
16
Years of Service
User Offline
Joined: 30th May 2008
Location:
Posted: 15th Feb 2014 17:41
I am really big into 2D games just because I am no artist and 2d stuff is easier to piece together than 3d models. I am interested in any types of algorithms like these, so when you fix the "orthogonal" corners thing, I would be interested in the code once again.

Converting to DBPro was not that hard. I was surprised by how little I had to change.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 15th Feb 2014 19:01 Edited at: 15th Feb 2014 19:04
Quote: "And the drawline() is from AppGameKit v2"

It's in V1 too, but the parameters don't seem to match up with yours.
DrawLine( x, y, x2, y2, red, green, blue )

In some cases you specified two colors, so it must've changed. But CreateRenderImage() must be V2 only so I can't run the code.

I noticed you're not too big on using spaces in your code


Here's the code with GIDustin's changes, converted to DBP. I don't know what you're getting in AppGameKit, but DBP I only get 1 fps! And setting the light range to anything higher makes it unusable. I'm gonna see about speeding up the drawing routines.


janbo
16
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 15th Feb 2014 22:11 Edited at: 3rd Oct 2017 21:45
@GIDustin
Thank you again for the fast fix, i coded it with dbpro in mind because I have similar light alogrithm in DBP.. I think it is far easier to convert it for more polygons/corners per object/sprite.

And I know the problem too good ..I have a Multilayer with cubes and Spheres because of the lack of artistic skill's and story.

@Phaelax
Yeah no spaces.. I think, I'm just too lazy for so much spaces, but consequently at least.

wow 1 Fps..

Try this i got 47 times more frames
Not fast enough but i think you get a frame boost with the advanced2d plugin or/and render to image.
I got 100-200 frames in AGKv2.

Now I'll try the render to image command again and beautify it later.
I don't have any experience with render to image and blending, so i appreciate every kind of help.

</div>

Attachments

Login to view attachments
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 15th Feb 2014 22:46 Edited at: 15th Feb 2014 22:47
I get 44-48 fps with that. I boosted it up to about 50 by getting rid of two sqrt calls.

Changed this:
Dist# = sqrt(DistX*DistX + DistY*DistY)
if Dist# < LightRange


To this:
Dist# = DistX*DistX + DistY*DistY
if Dist# < LightRange*LightRange



And since you have both these variables calculating the same thing:
Dist1# = sqrt(Dist1X*Dist1X+Dist1Y*Dist1Y)
Dist2# = sqrt(Dist1X*Dist1X+Dist1Y*Dist1Y)

Just replace Dist2# with this: Dist2# = Dist1#


Seems a little buggy though, I noticed some boxes will disappear under the light rather than cast a shadow when it should.

janbo
16
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 15th Feb 2014 23:34 Edited at: 3rd Oct 2017 21:45
Of course you are right multiplications are faster than sqrt.
But i need Dist1# and Dist2# to normalize the vector from the light source to the corners..it's the direction multiplied with the shadow length.
Somehow I can't tell you why it looks the same if you set Dist1# equal Dist2# rather than calculate both.
It should be Dist2# = sqrt(Dist2X*Dist2X+Dist2Y*Dist2Y))

This is the result of the AGKv2 line command rendered to an image:
610 Fps .. I can work with that.


[Edit]
And a second light using additive blending and render to image.
There are still some bugs


I hope we get a DrawTriangle() command in AGKv2.
[/Edit]

</div>

Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-11-25 02:14:46
Your offset time is: 2024-11-25 02:14:46