游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3740|回复: 1

问问几行windows游戏编程大师里的源码

[复制链接]

11

主题

17

帖子

17

积分

新手上路

Rank: 1

积分
17
发表于 2008-9-16 08:10:00 | 显示全部楼层 |阅读模式
这是第12章人工智能里的赛车寻路的代码:
test_waypoint = (Find_Nearest_Waypoint(car.varsF[0],car.varsF[1])+1)%NUM_WAYPOINTS;

// test if this is a new waypoint and not the last
if (test_waypoint != curr_waypoint)
   curr_waypoint = test_waypoint;

// compute vector toward waypoint
float wx = (path[curr_waypoint].x - car.varsF[0]);
float wy = (path[curr_waypoint].y - car.varsF[1]);

// normalize wx, wy
float length = Fast_Distance_2D(wx+0.5,wy+0.5);
wx/=length;
wy/=length;

// compute angle in radians
float car_angle = ((-90+car.curr_frame*22.5)*PI)/180;

// compute velocity vector of car (use look up for cos, sin in real life
float xv = cos(car_angle);
float yv = sin(car_angle);

// draw vectors
if (vector_display_on==1)
   {
   DDraw_Lock_Back_Surface();
   Draw_Line(car.varsF[0],car.varsF[1], path[curr_waypoint].x, path[curr_waypoint].y,
              250, back_buffer, back_lpitch);

   Draw_Line(car.varsF[0],car.varsF[1], car.varsF[0]+16*xv,car.varsF[1]+16*yv,
              246, back_buffer, back_lpitch);
   DDraw_Unlock_Back_Surface();
   } // end if

// now turn car into waypoint direction, get sign of cross product
// between each vector
float sign_cross = (xv*wy - yv*wx);


if (ai_on)
{
// test the sign to determine which way to turn plus a little slosh
if (sign_cross > 0.3)
   {
   if (++car.curr_frame > 15)
      car.curr_frame = 0;   

   sprintf(buffer,"AI Turning RIGHT", sign_cross);
   Draw_Text_GDI(buffer,320, 460,RGB(255,255,255),lpddsback);

   } // end if


我对这一句相当不明白:
float sign_cross = (xv*wy - yv*wx);
并且sign_cross > 0.3)判定是向右,请问这是为啥呢?

2

主题

429

帖子

435

积分

中级会员

Rank: 3Rank: 3

积分
435
发表于 2008-9-16 19:48:00 | 显示全部楼层

Re:问问几行windows游戏编程大师里的源码

当前朝向与目标朝向叉乘。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-5 11:00

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表