Dynamic Shadow In TGB? Yes!
by William Lee Sims · in Torque Game Builder · 08/21/2010 (3:07 am) · 46 replies
When searching around the Hardcore final level, we wanted the power-ups and enemies to be hidden behind pillars. To that end, I found an article on dynamic 2D shadows.
Because it would be too hard to inject myself into the middle of the TGB rendering, I had to modify the steps a little bit. I create an overlay that makes the scene get darker away from our hero's light source. Then, I find all of the pillars currently on the screen and create convex polygons around the main features of the pillars.
It looks pretty nice and it adds a lot of tension and exploration to the game.
My code isn't generic enough for general use, but if you want a headstart, I can send you the class and an outline of its functionality.
Because it would be too hard to inject myself into the middle of the TGB rendering, I had to modify the steps a little bit. I create an overlay that makes the scene get darker away from our hero's light source. Then, I find all of the pillars currently on the screen and create convex polygons around the main features of the pillars.
It looks pretty nice and it adds a lot of tension and exploration to the game.
My code isn't generic enough for general use, but if you want a headstart, I can send you the class and an outline of its functionality.
About the author
#42
Nothing but shadow casters (and NOT the player) should have group 2.
Could you try this modification and let me know what the console says (I apologize in advance if I made some typos... just writing this off the top of my head):
03/23/2012 (9:14 pm)
Oops! I did mean light map. I switch "light" and "shadow" all the time when talking about this!Nothing but shadow casters (and NOT the player) should have group 2.
Could you try this modification and let me know what the console says (I apologize in advance if I made some typos... just writing this off the top of my head):
void t2dLightMap::renderShadow( const Vector<t2dVector>& verts, const t2dVector& lightPos )
{
static bool qFirstTime = true;
if( qFirstTime )
Con::printf( "Verts: %d, LightPos: %f %f", verts.size(), lightPos.mX, lightPos.mY );
S32 startingVert = -999, endingVert = -999;
for( S32 i = 1; i < verts.size() + 1; ++i )
{
S32 vert0 = (i-1) % verts.size();
S32 vert1 = (i) % verts.size();
S32 vert2 = (i+1) % verts.size();
t2dVector normal0( verts[vert0] - verts[vert1] ); normal0.perp();
t2dVector normal1( verts[vert1] - verts[vert2] ); normal1.perp();
t2dVector ray( verts[vert1] - lightPos );
F32 n0 = ray.dot( normal0 );
F32 n1 = ray.dot( normal1 );
if( qFirstTime )
Con::printf( "%d: %f %f", i, n0, n1 );
if( n0 <= 0 && n1 > 0 )
{
startingVert = vert1;
}
if( n0 > 0 && n1 <= 0 )
{
endingVert = vert1;
}
}
if( qFirstTime )
Con::printf( "Start: %d, End: %d.", startingVert, endingVert );
glBegin( GL_TRIANGLE_STRIP );
glColor4f( 0, 0, 0, 1 );
for( S32 i = 0; i < verts.size(); ++i )
{
S32 vert = (startingVert + i) % verts.size();
Con::printf( "Render %d: %d", i, vert );
t2dVector ray( verts[vert] - lightPos ); ray.normalise( 600 );
glVertex2f( verts[vert].mX, verts[vert].mY );
glVertex2f( verts[vert].mX + ray.mX, verts[vert].mY + ray.mY );
if( vert == endingVert ) break;
}
glEnd();
qFirstTime = false;
}
#43
Hm...It no longer crashes right away. But when I move the character around or it starts to approach something that casts a shadow, it crashes. The shadows aren't working. Would the size of the objects or the lightmap have anything to do with it?
03/24/2012 (6:41 am)
Quote:Verts: 24, LightPos: -30.493797 -8.840000
1: 1615.422974 1562.044189
2: 1562.044189 1465.711670
3: 1465.711670 1332.990601
4: 1332.990601 1172.925049
5: 1172.925049 996.424805
6: 996.424805 815.515686
7: 815.515686 642.528809
8: 642.528809 489.250763
9: 489.250763 366.128876
10: 366.128876 281.552734
11: 281.552734 241.286697
12: 241.286697 248.074051
13: 248.074051 301.452881
14: 301.452881 397.785309
15: 397.785309 530.507019
16: 530.507019 690.571594
17: 690.571594 867.072571
18: 867.072571 1047.980957
19: 1047.980957 1220.968384
20: 1220.968384 1374.245605
21: 1374.245605 1497.367920
22: 1497.367920 1581.947021
23: 1581.947021 1622.209106
24: 1622.209106 1615.422974
Start: -999, End: -999.
Render 0: -15
Fatal: (d:\programs\torque 2d\engine\source\core\tvector.h @ 441) Vector<T>::operator[] - out of bounds array access!
keyboard0 input device unacquired.
DirectInput deactivated.
Activating DirectInput...
Hm...It no longer crashes right away. But when I move the character around or it starts to approach something that casts a shadow, it crashes. The shadows aren't working. Would the size of the objects or the lightmap have anything to do with it?
#44
Whatever the size of your largest object is (say 128x128 pixels), double it and add it to your camera area (say 800x600) to get your t2dLightMap size (say 1056x856). Let's see what happens then.
03/24/2012 (11:34 am)
The pairs of numbers in the long list should switch between positive and negative and then back again. Now that you mention the lightmap, I do have to wonder. I made my lightmap significantly larger than the size of the camera. The code in t2dLightMap picks all objects on the screen.Whatever the size of your largest object is (say 128x128 pixels), double it and add it to your camera area (say 800x600) to get your t2dLightMap size (say 1056x856). Let's see what happens then.
#46
Would it be possible for you to zip up your game directory (just the directory with the copied executable and the subdirectories) and email it to me? My email is wls@wsims.com.
03/25/2012 (3:29 pm)
They aren't necessary for this code to work, but if you walk into a shadow caster that doesn't have collision, it will cause this problem.Would it be possible for you to zip up your game directory (just the directory with the copied executable and the subdirectories) and email it to me? My email is wls@wsims.com.
Cyrad
Silent Syntax
The light map is attached to a player object. I performed a test where I just mounted the lightmap onto a object that does nothing and it still gives the same error. Should the player object also be on group 2?
Looks like startingVert is -999 while vert is being set.