12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 4081|回复: 12
打印 上一主题 下一主题

[Anark] Collision Detection

[复制链接]
Asen    

867

主题

0

听众

1万

积分

外协人员

Rank: 7Rank: 7Rank: 7

纳金币
17488
精华
1
跳转到指定楼层
楼主
发表于 2011-9-29 09:00:15 |只看该作者 |倒序浏览

Summary
This sample project shows how to use the Model.intersect() method exposed to script to write your own collision detection.

   
        
            Category
            Tutorials
        
        
            Author
            Gavin Kistner
        
        
            Difficulty
            Intermediate
        
        
            Time to Complete
            10 Minutes
        
   





Downloads
Click Here to download the associated files.
This sample project shows the use of the intersect() method, made available on all Models in Anark Client during at runtime. First, click on picture below to view the sample project, and play with it a bit to see what it does.


click to view the sample project in action
As you drag the end points of the segment, the model turns orange if the segment is hitting it. Further, a yellow arrow shows exactly where the segment hits the model, and is rotated to show the direction of the 'normal' of the face where that collision occurs.
The intersect() method used for this test lets you check to see if an "ideal" line segment intersects the model that invoked the method. To be clear about this: Anark Client does not support model-on-model collision detection. (At least not in version 3.0 ;)
The 'line' that connects the two end points of the segment in this presentation is actually a long, thin cylinder. This is only a visual representation of the segment being used for collision detection. With careful movements, you can set it up so that an edge of the line is hitting the model, but the infinitely-thin segment being used for collision detection does not. Only the line connecting the centers of the two ends is used for collision.


The cylinder itself is not being used for collision detection.
Download the AMW file for the sample project, and open it up. The "Watch for Intersection" behavior (inside the "Content Layer") performs the collision detection for this presentation, and is what we want to look at. Double-click this behavior to view its code.
Initialization
At the top of the behavior, even before it gets its onAttach event, we create a few objects. These are created here instead of during onUpdate in order to save memory. By creating them once and then re-using them each time, we save the (small) amount of memory that would be wasted if we created three new vectors every frame.
onAttach
One more custom property is saved during onAttach, just to make things a little bit faster (and easier to type) during the onUpdate call. (A topic for another tutorial is the speed gain achieved by reducing DOM path navigation.)

onUpdate
Here be dragons! Just kidding, don't be scared. This is where the magic happens, but it's really not that many lines.
There are three steps in collision detection:

   
    Calculate the segment which will be used to check for collisions.
   
   
    Check to see if the segment intersects the model.
   
   
    If it does intersect, do something useful with the information.
   

As noted above, the intersect() method tells you whether or not a segment intersects the model. You describe this segment to the method using three values:

   
    The start location of the segment.

    This is a Vector object, and must be specified in global space.

   
   
   
    The direction that the segment points.

    This is also a Vector object, in this case used to indicate a direction, not location. Think of it this way: if you start at 0,0,0 (the global origin) and move towards the point in space indicated by this vector, that's the direction the segment will go.

   
   
   
    The length of the segment.

    The magnitude of the direction vector is unimportant. This (single number) value is used to tell the intersect() method how far from the start point to look for a hit before it gives up.
   

There are three other parameters which you must pass to the method, in addition to those mentioned above:

   
    Whether or not to use backfaces of the model.

    If the materials on your model are all double-sided, this (boolean) parameter is irrelevant. This only matters when your model is single-sided. If this value is false, AND the segment hits a face of your model that is single-sided, AND the direction the segment is travelling comes from the back side of the face...then the hit will be ignored.

    Put another way, leaving this as false will result in slightly better performance, but if your start point is in the inside of a single-side model, you won't get a hit as the segment comes out through a face.

   
   
   
    A 'hitSpot' vector.

    The values in this vector are ignored. IF you get a hit, then this vector will be modified to represent the exact spot (in global space) where the hit occurred.

   
   
   
    A 'hitNormal' vector.

    Again, the values of this vector are ignored. IF you get a hit, then this vector will be modified to represent the direction of the normal on the face where the hit occurred.
   

Armed with this knowledge, the lines of code in the onUpdate section should seem pretty simple. The first two lines create the global direction vector for the segment. The next line calculates how far it is between the start and end points. Then the call to the intersect() method is made.

When a hit occurs (when the call to the method returns a value greater than or equal to zero), the last two vectors passed into the method (hitSpot and hitNormal) are modified to hold the information about the hit. These are then used to move the yellow arrow to the exact spot of the collision, and to rotate it to point out from the face.
Common "Gotchas"
A few things to watch out for when using this method:

   
    Make sure that your start point and ray direction vectors are in global space, not the local transformation coordinates of the model.
   
   
    The vector indicating ray direction indicates the direction from 0,0,0 (or the offset from the start point), not a location. If you know the end point of your ray, subtract the start point from it to find the global direction offset.
   
   
    The value returned by the method is the distance from the start point to the hit spot squared. It also may be exactly zero, if your start point is in the plane of the face being hit.
   
   
    Even if you don't care about the normal of the face where a hit occurs, or even the exact spot of the hit, you must pass vectors in for these parameters. (For better memory usage, re-use the same 'junk' vectors each time.)
   
   
    Like the start point, the hit location and face normal are in global space.
   

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-2-5 23:25:08 |只看该作者
再次路过……
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-3-12 23:24:08 |只看该作者
很经典,很实用,学习了!
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-4-15 23:25:44 |只看该作者
呵呵,很好,方便罗。
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

5#
发表于 2012-4-24 08:10:28 |只看该作者
加精、加亮滴铁子,尤其要多丁页丁页
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2012-5-11 23:24:14 |只看该作者
呵呵,真得不错哦!!
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-6-18 23:25:22 |只看该作者
其实楼主所说的这些,俺支很少用!
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2012-7-13 23:19:22 |只看该作者
非常感谢,管理员设置了需要对新回复进行审核,您的帖子通过审核后将被显示出来,现在将转入主题
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

9#
发表于 2012-12-2 23:20:37 |只看该作者
“再次路过……”我造一个-----特别路过
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

10#
发表于 2013-2-9 23:25:37 |只看该作者
我就看看,我不说话
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-22 04:11 , Processed in 0.091624 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部