游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2558|回复: 6

世界场景中的碰撞检测(一个函数就搞定)

[复制链接]

4

主题

9

帖子

9

积分

新手上路

Rank: 1

积分
9
发表于 2005-4-8 22:17:00 | 显示全部楼层 |阅读模式
(世界场景中的碰撞检测---direct3d版)
一个函数就搞定了!!!
出自<Premier Press - Programming Role-Playing Games With Directx 8.0>

Colliding with the World  
I don’t know about your games, but in mine, most of my characters are not superheroes,
so they can’t all go around walking through walls! For that reason, a 3-D
engine needs to know when to block the path of a character that is about to collide
with an object such as a wall.
Casting a Ray
In order to check whether a polygon blocks the path from one point to another,
you “cast” an imaginary ray between the two points and check to see whether it
intersects with a plane. Remember planes? I spoke of them in the section, “Planes
and Clipping.” A polygon is nothing more than a finitely sized plane. By constructing
a plane to represent the polygon, you can use algebra to determine the intersection
(refer to Figure 12.7).
(注:figure 12.7 我以附件形式上传)
  In the earlier section “Checking for Visibility with the Plane,” you learned about
dot products and calculating distance from points to planes. Using the same calculations,
you can determine if, and where, a point intersects a plane as that point
moves in 3-D space. The point, as it moves, creates a line that represents the path
an object takes during some sort of movement.
Figure 12.8 illustrates these points. In the figure, you see a polygon and a line. The
line represents the ray cast from a starting point to an ending point. The ray intersects
with the polygon about halfway down the ray.abcabcabc

Remember that a plane is infinite in size, so a ray will always intersect with the
plane as long at it isn’t parallel with the plane. For that reason, you must be able to
tell whether the intersection is within the polygon’s edges, and that’s a little more
difficult to do.
Here comes D3DX to the rescue! A single function performs an intersection test,
ensures that the point of the ray-to-plane intersection lies within the polygon, gives
the exact coordinates of the intersection, and, as a bonus, reveals the distance from
the ray’s starting point to the intersection point. Talk about one useful function! So,
which function is it? It’s the D3DXIntersect function, which has the following prototype:
HRESULT D3DXIntersect(
LPD3DXBASEMESH pMesh, // Mesh to check intersection
CONST D3DXVECTOR3 *pRayPos, // Ray origin
CONST D3DXVECTOR3 *pRayDir, // Direction to cast ray
BOOL *pHit, // Flag if intersection occurs
DWORD *pFaceIndex, // Which face ray intersects
FLOAT *pU,
FLOAT *pV,
FLOAT *pDist); // Distance to intersection
Right off the bat, you can see that D3DXIntersect works with meshes (ID3DXBaseMesh),
which is a boon because mesh objects are just what you’re working with at this
point. Next, you can see that you need to specify the starting origin of the ray
(in pRayPos). For pRayPos (and pRayDir), you can use the D3DXVECTOR3 macro inline.
pRayDir represents a direction vector, much like a normal vector. For example, to
cast a ray upward, you use a value of -1.0 for the Y value. The only other argument
you need to deal with is pDist. pDist points to a FLOAT type variable that is filled with
the distance from the ray origin to the intersection point.

一个函数就搞定,简单吧!


sf_200548221718.bmp

140

主题

1228

帖子

1233

积分

金牌会员

Rank: 6Rank: 6

积分
1233
QQ
发表于 2005-4-9 01:49:00 | 显示全部楼层

Re:世界场景中的碰撞检测(一个函数就搞定)

用的时候你就知道这个函数根本搞不定!算的东西太多了!
最大的问题是,既然要介绍这个函数的用法,干吗直接粘过来呢!

15

主题

107

帖子

115

积分

注册会员

Rank: 2

积分
115
发表于 2005-4-9 09:29:00 | 显示全部楼层

Re:世界场景中的碰撞检测(一个函数就搞定)

如果碰撞用这个函数,肯定是没有实时性的,计算量太大了!

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
发表于 2005-4-9 23:23:00 | 显示全部楼层

Re:世界场景中的碰撞检测(一个函数就搞定)

用总要用的吧
剔除大部分以后再用

13

主题

978

帖子

978

积分

高级会员

Rank: 4

积分
978
发表于 2005-4-10 00:04:00 | 显示全部楼层

Re:世界场景中的碰撞检测(一个函数就搞定)

似乎是dx8中已经有的射线与三角相交的判定函数…………

14

主题

131

帖子

136

积分

注册会员

Rank: 2

积分
136
发表于 2005-4-10 01:08:00 | 显示全部楼层

Re:世界场景中的碰撞检测(一个函数就搞定)

楼主似乎比较天真,事实上有很多种检测是一个游戏都必须具备的,比如 BOX VS BOX,BOX VS SPHERE,RAY VS BOX 等等,等等.......
你说的只是其中一小部分!

63

主题

871

帖子

891

积分

高级会员

Rank: 4

积分
891
QQ
发表于 2005-4-10 17:08:00 | 显示全部楼层

Re:世界场景中的碰撞检测(一个函数就搞定)

我把BOX VS BOX,BOX VS SPHERE,RAY VS BOX 等等,等等.......

做到了物理库里了
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-25 04:41

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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