📄 demo13_7_16b.cpp
字号:
} // end for index
// unload bitmap image
Unload_Bitmap_File(&bitmap8bit);
// define points of shape
VERTEX2DF shape_vertices[10] =
{ 328-SHAPE_CENTER_X,60-SHAPE_CENTER_Y,
574-SHAPE_CENTER_X,162-SHAPE_CENTER_Y,
493-SHAPE_CENTER_X,278-SHAPE_CENTER_Y,
605-SHAPE_CENTER_X,384-SHAPE_CENTER_Y,
484-SHAPE_CENTER_X,433-SHAPE_CENTER_Y,
306-SHAPE_CENTER_X,349-SHAPE_CENTER_Y,
150-SHAPE_CENTER_X,413-SHAPE_CENTER_Y,
28-SHAPE_CENTER_X,326-SHAPE_CENTER_Y,
152-SHAPE_CENTER_X,281-SHAPE_CENTER_Y,
73-SHAPE_CENTER_X,138-SHAPE_CENTER_Y };
// initialize shape
shape.state = 1; // turn it on
shape.num_verts = 10;
shape.x0 = SHAPE_CENTER_X;
shape.y0 = SHAPE_CENTER_Y;
shape.xv = 0;
shape.yv = 0;
shape.color = RGB16Bit(0,255,0); // green
shape.vlist = new VERTEX2DF [shape.num_verts];
for (index = 0; index < shape.num_verts; index++)
shape.vlist[index] = shape_vertices[index];
// hide the mouse
if (!WINDOWED_APP)
ShowCursor(FALSE);
// initialize directinput
DInput_Init();
// acquire the keyboard only
DInput_Init_Keyboard();
// build the 360 degree look ups
Build_Sin_Cos_Tables();
// initilize DirectSound
DSound_Init();
// load background sounds
ball_ids[0] = DSound_Load_WAV("PBALL.WAV");
// clone sounds
for (index=1; index<8; index++)
ball_ids[index] = DSound_Replicate_Sound(ball_ids[0]);
// return success
return(1);
} // end Game_Init
///////////////////////////////////////////////////////////
int Game_Shutdown(void *parms)
{
// this function is where you shutdown your game and
// release all resources that you allocated
// shut everything down
// kill all the bobs
for (int index=0; index<NUM_BALLS; index++)
Destroy_BOB(&balls[index]);
// shutdown directdraw last
DDraw_Shutdown();
// now directsound
DSound_Stop_All_Sounds();
DSound_Shutdown();
// shut down directinput
DInput_Shutdown();
// return success
return(1);
} // end Game_Shutdown
//////////////////////////////////////////////////////////
void Ball_Sound(void)
{
// this functions hunts for an open handle to play a collision sound
// start a hit sound
for (int sound_index=0; sound_index < 8; sound_index++)
{
// test if this sound is playing
if (DSound_Status_Sound(ball_ids[sound_index])==0)
{
DSound_Play(ball_ids[sound_index]);
break;
} // end if
} // end for
} // end Ball_Sound
///////////////////////////////////////////////////////////
void Compute_Collisions(void)
{
// this function computes if any ball has hit one of the edges of the polygon
// if so the ball is bounced
float length,s,t,s1x,s1y,s2x,s2y,p0x,p0y,p1x,p1y,p2x,p2y,p3x,p3y,xi,yi,npx,npy,Nx,Ny,Fx,Fy;
for (int index = 0; index < NUM_BALLS; index++)
{
// first move ball
balls[index].varsF[INDEX_X]+=balls[index].varsF[INDEX_XV];
balls[index].varsF[INDEX_Y]+=balls[index].varsF[INDEX_YV];
// now project velocity vector forward and test for intersection with all lines of polygon shape
// build up vector in direction of trajectory
p0x=balls[index].varsF[INDEX_X];
p0y=balls[index].varsF[INDEX_Y];
#if 1 // this is the velocity vector used as segment 1
p1x=balls[index].varsF[INDEX_X]+balls[index].varsF[INDEX_XV];
p1y=balls[index].varsF[INDEX_Y]+balls[index].varsF[INDEX_YV];
s1x=p1x-p0x;
s1y=p1y-p0y;
// normalize and scale to 1.25*radius
length = sqrt(s1x*s1x+s1y*s1y);
s1x = 1.25*BALL_RADIUS*s1x/length;
s1y = 1.25*BALL_RADIUS*s1y/length;
p1x = p0x + s1x;
p1y = p0y + s1y;
#endif
// for each line try and intersect
for (int line=0; line < shape.num_verts; line++)
{
// now build up vector based on line
p2x=shape.vlist[line].x+shape.x0;
p2y=shape.vlist[line].y+shape.y0;
p3x=shape.vlist[(line+1)%(shape.num_verts)].x+shape.x0;
p3y=shape.vlist[(line+1)%(shape.num_verts)].y+shape.y0;
s2x=p3x-p2x;
s2y=p3y-p2y;
#if 0 // this is the perp vector used as segment 1
// normalize s2x, s2y to create a perpendicular collision vector from the ball center
length = sqrt(s2x*s2x+s2y*s2y);
s1x = BALL_RADIUS*s2y/length;
s1y = -BALL_RADIUS*s2x/length;
p1x = p0x+s1x;
p1y = p0y+s1y;
#endif
// compute s and t, the parameters
s = (-s1y*(p0x-p2x) + s1x*(p0y-p2y))/(-s2x*s1y + s1x*s2y);
t = (s2x*(p0y-p2y) - s2y*(p0x-p2x))/(-s2x*s1y + s1x*s2y);
// test for valid range (0..1)
if (s >= 0 && s <=1 && t >= 0 && t <=1)
{
// find collision point based on s
xi = p0x+s*s1x;
yi = p0y+s*s1y;
// now we know point of intersection, reflect ball at current location
// N = (-I . N')*N'
// F = 2*N + I
npx = -s2y;
npy = s2x;
// normalize p
length = sqrt(npx*npx+npy*npy);
npx/=length;
npy/=length;
// compute N = (-I . N')*N'
Nx = -(balls[index].varsF[INDEX_XV]*npx + balls[index].varsF[INDEX_YV]*npy)*npx;
Ny = -(balls[index].varsF[INDEX_XV]*npx + balls[index].varsF[INDEX_YV]*npy)*npy;
// compute F = 2*N + I
Fx = 2*Nx + balls[index].varsF[INDEX_XV];
Fy = 2*Ny + balls[index].varsF[INDEX_YV];
// update velocity with results
balls[index].varsF[INDEX_XV] = Fx;
balls[index].varsF[INDEX_YV] = Fy;
balls[index].varsF[INDEX_X]+=balls[index].varsF[INDEX_XV];
balls[index].varsF[INDEX_Y]+=balls[index].varsF[INDEX_YV];
// make sound
Ball_Sound();
// break out of for line
//break;
} // end if
} // end for line
} // end for ball index
} // end Collision_Collisions
//////////////////////////////////////////////////////////
int Game_Main(void *parms)
{
// this is the workhorse of your game it will be called
// continuously in real-time this is like main() in C
// all the calls for you game go here!
int index; // looping var
static rotate = 0;
// start the timing clock
Start_Clock();
// lock back buffer and copy background into it
DDraw_Lock_Back_Surface();
// draw background
Draw_Bitmap16(&background_bmp, back_buffer, back_lpitch,0);
// draw shape
Draw_Polygon2D16(&shape, back_buffer, back_lpitch);
// have a little fun
if (++rotate > 10)
{
Rotate_Polygon2D(&shape,1);
rotate=0;
}
// unlock back surface
DDraw_Unlock_Back_Surface();
// read keyboard
DInput_Read_Keyboard();
// move the balls and compute collisions
Compute_Collisions();
// draw the balls
for (index=0; index < NUM_BALLS; index++)
{
balls[index].x = balls[index].varsF[INDEX_X]+0.5-BALL_RADIUS;
balls[index].y = balls[index].varsF[INDEX_Y]+0.5-BALL_RADIUS;
Draw_BOB16(&balls[index], lpddsback);
} // end for
// draw the velocity vectors
DDraw_Lock_Back_Surface();
for (index=0; index < NUM_BALLS; index++)
{
Draw_Clip_Line16(balls[index].varsF[INDEX_X]+0.5,
balls[index].varsF[INDEX_Y]+0.5,
balls[index].varsF[INDEX_X]+2*balls[index].varsF[INDEX_XV]+0.5,
balls[index].varsF[INDEX_Y]+2*balls[index].varsF[INDEX_YV]+0.5,
RGB16Bit(255,255,255), back_buffer, back_lpitch);
} // end for
DDraw_Unlock_Back_Surface();
// draw the title
Draw_Text_GDI("(16-Bit Version) Object to Contour Collision DEMO, Press <ESC> to Exit.",10, 10,RGB(255,255,255), lpddsback);
// flip the surfaces
DDraw_Flip();
// run collision algorithm here
Compute_Collisions();
// sync to 30 fps = 1/30sec = 33 ms
Wait_Clock(33);
// check of user is trying to exit
if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
{
PostMessage(main_window_handle, WM_DESTROY,0,0);
// stop all sounds
DSound_Stop_All_Sounds();
} // end if
// return success
return(1);
} // end Game_Main
//////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -