Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Crude Shadow Algorerhythm  (Read 8275 times)

0 Members and 3 Guests are viewing this topic.

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Crude Shadow Algorerhythm
« on: April 23, 2010, 01:17:04 am »
Alright after a bit of thinking about how I could create dynamic shadows I wrote a crude algorerhythm that sort of approaches the problem using SFML. The general idea is that the light source casts out rays and the collision locations of these rays help shape the general outline of the light

Its very simplistic and has quite a few problems that probably will prevent it from working properly let alone being suitable for real time shadows but its simply a jumping board.

What are some suggestions,improvement, or outright replacements you can come up with?  And if there are any outright mistakes feel free to point that out as well I didn't check this in the compiler.

Some Current Problems
1. For and while loops: can eliminate them but replacing them with if statements can cause problems with the light not being generated properly.
2. The light ray movement hinders the ability of the light to change quickly
3. Low resolution: Can be solved by increasing amount of rays cast but this may slow things down.
4. Testing rays for collisions against all objects: Slow but I'm not sure what other options there may be.
5. Light Polygon might not form properly if concave.

Code: [Select]
//Point light:

//place pixel light
light.SetPosition(200,200);

//place light circle
lightcircle.SetPosition(200,200)

//Optional Step: Check for collision of light circle can be substituted with continuous scanning

for(itcirchk=0;itcirchk<=objectlist.size();itcirchk++)
{
  if (CircleTestCollision(lightcircle,object[itcirchk].Mass)==1)
  {//If collision, start raycasting every x degrees to form shape of light polygon
  xlightray=.1;
  ylightray=-.9;

    do
    {

      while(distancebetweenobjects(light.Mass,bullet)<=100)//while the ray is in the light's range
      {
 lightray.move(xlightray,ylightray);

       for (itraycolchk=0;itraycolchk<=objectlist.size();itraycolchk++)
   {//every step check ray for collision against all objects
     if (PixelPerfectCollisionCheck(lightray,objectlist[itraycolchk])==1);
     { //If collision record ray location and add to polygon generation vector.
          recordpoint.x=lightray.GetPosition().x;
          recordpoint.y=lightray.GetPosition().y;
          pointvector.pushback(recordpoint);
          pointreached=1;
     break;//move on to next ray
     }
        }

 if pointreached==1;
 break;//break out of loop to cast next ray
     
   else if (distancebetweenobjects(light.Mass,lightray)>=100)
   {//if ray has traveled light's range without collision add another point to polygen vector
   recordpoint.x=lightray.GetPosition().x;
        recordpoint.y=lightray.GetPosition().y;
        pointvector.pushback(recordpoint);
   }

 }

//change direction ray will travel in clockwise fashion

 if ( xlightray>=0 && xlightray<=1 && ylightray>=-1 && ylightray <=0)
 {//first quadrant
 xlightray=xlightray+.1;
 ylightray=ylightray+.1;
 }

 else if (xlightray >=0 xlightray<=1 && ylightray>=0 && ylightray>=1 )
 {//second quad
 xlightray=xlightray-.1;
 ylightray=ylightray+.1;
 }

 else if (xlightray>=-1 && xlightray<=0  && ylightray<=1 && ylightray>=0)
 {//third quad
      xlightray=xlightray-.1;
 ylightray=ylightray-.1;
 }

 else (xlightray>=-1 && xlightray<=0 && ylightray<=0 && ylightray>=-1)
 {//fourth quad
 xlightray=xlightray+.1;
 ylightray=ylightray-.1;
 }

}while (xlightray!=0 && ylightray!=-1)//run loop till rays have been cast in full circle


  }

}

//Draw light polygon based on points in vector
for (itpolydrw=0;itpolydrw<=pointvector.size();itpolydrw++)
LightPolygon.AddPoint(pointvector[itpolydrw].x,pointvector[itpolydrw].y)

//Color polygon
LightPolygon.SetColor(sf::Color(255, 255, 255, 200));

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Crude Shadow Algorerhythm
« Reply #1 on: April 23, 2010, 04:31:26 am »
The problem with dynamic shadows for a 2D game is that the best 2D graphics often simulate 3D or isometric view.

What the shadows are supposed to be like depends entirely on your game.

For most people copying and transforming a sprite creates a good enough shadow.

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Crude Shadow Algorerhythm
« Reply #2 on: April 23, 2010, 06:44:20 am »
Quote from: "Ashenwraith"
The problem with dynamic shadows for a 2D game is that the best 2D graphics often simulate 3D or isometric view.

What the shadows are supposed to be like depends entirely on your game.

For most people copying and transforming a sprite creates a good enough shadow.


There are some amazing dynamic shadows you can find simply by browsing through youtube






They all seem to utilize openGl or Directx though. I would borrow a page out of OrangeTangy's book at Gamedev but his stuff is in java and I'm a little iffy on how to integrate Opengl into my existing code. I haven't been able to find any source in C++.


Still I don't see why a similar result wouldn't be possible without opengl if we had more sophisticated blending and collision functions.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Crude Shadow Algorerhythm
« Reply #3 on: April 23, 2010, 08:15:37 am »
The code to do top down 2D lighting is all over the internet in all languages for years.

But it only is useful if you are going to make a top-down style game.

Just search gamedev and other sites.

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Crude Shadow Algorerhythm
« Reply #4 on: April 23, 2010, 12:50:40 pm »
I recommend you installing Game Maker and open the source of this engine: http://gmc.yoyogames.com/index.php?showtopic=247336

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Crude Shadow Algorerhythm
« Reply #5 on: April 26, 2010, 02:39:27 am »
Quote from: "Ashenwraith"
The code to do top down 2D lighting is all over the internet in all languages for years.

But it only is useful if you are going to make a top-down style game.

Just search gamedev and other sites.



Can you point me to some specific examples? The closest one I can find is OrangeTangy's article.

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Crude Shadow Algorerhythm
« Reply #6 on: April 26, 2010, 02:51:08 am »
Quote from: "panithadrum"
I recommend you installing Game Maker and open the source of this engine: http://gmc.yoyogames.com/index.php?showtopic=247336


interesting but it looks like I'd have to buy game maker plus I'm not sure if I can simply take the source and integrate it or if I'd have to turn my entire program into a gamemaker project.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Crude Shadow Algorerhythm
« Reply #7 on: April 26, 2010, 03:42:31 am »
Quote from: "Jarwulf"
Can you point me to some specific examples? The closest one I can find is OrangeTangy's article.


Orangy tang was the pioneer and his stuff is replicated in many different ways--even FLASH. He explains it so you can redo it in any language.

The gamemaker version is a direct3D extension. You can get it for free, but I don't know how much use it will be since I don't know what your exact problem is.

http://www.incasoftware.de/~kamm/projects/index.php/2007/09/08/soft-shadows-2d/

http://www.pyropixel.de/bmax_shadows.html  //includes isometric version

http://www.ogre3d.org/forums/viewtopic.php?f=10&t=56294

http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=103245&page=2

http://blogs.msdn.com/manders/archive/2007/03/14/shadows-in-2d.aspx

http://www.catalinzima.com/?page_id=313

http://www.idevgames.com/forum/showthread.php?t=17799

http://social.msdn.microsoft.com/Forums/en/smallbasic/thread/df0c67fd-decb-4295-b836-37dd9db52378

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Crude Shadow Algorerhythm
« Reply #8 on: April 26, 2010, 08:58:10 am »
Quote from: "Jarwulf"
Quote from: "panithadrum"
I recommend you installing Game Maker and open the source of this engine: http://gmc.yoyogames.com/index.php?showtopic=247336


interesting but it looks like I'd have to buy game maker plus I'm not sure if I can simply take the source and integrate it or if I'd have to turn my entire program into a gamemaker project.


Not actually at all. You can open and see the code with the not-licensed version, but can't run it. The code is fairly simple. It uses surfaces a.k.a. render images, and triangles.

Maybe I will translate that project to a SFML project!

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Crude Shadow Algorerhythm
« Reply #9 on: April 27, 2010, 03:20:16 am »
blech..


Well I had hoped I could find something directly in C++/OpenGL but I guess that was a pipe dream.

 Even though I was willing to attempt to translate the other solutions I'd have to get knee deep in 3D/OpenGL itself (something I had tried to avoid) and learn not only about shadows but all the API basics and more just to make things look right since there apparently are no suitable traditional 2d solutions to this problem.

Try as I might there are no good resources I could find online or in print to learn modern OpenGL and what was available for older versions was disorganized,inconsistent, and often nonfunctional.

Then of course I found out that my card cannot be updated to OpenGL 3 after a long fruitless search which complicates things considerably.

a failed attempt to use an emulator which apparently only works for cards which which can support 3.0 anyway was the last straw.
 

Since 3d appears to be the only option anyway might as well take a gander at what Ogre can do or maybe the Gamemaker example.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Crude Shadow Algorerhythm
« Reply #10 on: April 27, 2010, 05:37:13 am »
You can do it in sfml. You just have to draw vetices/shapes and update them.

You can also use sprites directly to create the shadow.

Yes learning OpenGL is a pain, especially since they let Direct X kick their ass for so long. Microsoft C# is even replacing C++/Java for beginners at schools.

You have to come at it like a professional programmer and find the best libs to work with and build your program one brick at a time. The PC game market is basically dead unless its flash or a major mmo. Sooner or later you have to learn to rebuild your code to fit whatever market you are deploying on.

The world would be so much easier if you could just buy a video card and plug it into your TV.

 

anything