Skip to main content
 首页 » 资源教程 » UE4教程

UE4的Collision与Physics

2016年10月08日 21:45:109780

Collision和Physics都是各种Mesh的属性,和Unity3D很像,Collision和Physics是两个独立的Component,Collision仅仅掌管碰撞发生的检测和回调,而Physics仅仅模拟碰撞发生之后位置速度等等的变化,可见Collision似乎用处更大一点。

比如Collision可以设置各种perset,里边的标签都是不同的碰撞处理逻辑,但是本质上都是一些channel。就是说我到底会和谁发生碰撞(这里要注意,Collision发生碰撞不一定会有物理表现,有物理表现完全是PhysicsSimulate的功能,Collision仅仅检测是否碰碰撞和回调一些碰撞处理函数) 
比如: 
BlockAll会和所有物体发生碰撞(不开启PhysicsSimulate,还是能穿过,开启之后会和所有物体碰撞) 
而Vehicle会和WorldStatic、WorldDynamic发生碰撞(毕竟是汽车,不能穿模啊) 
Spectator只和WorldStatic发生碰撞(因为是观战者,不能干扰比赛,但也不能陷到地底下去啊) 
Trigger只和WorldDynamic发生“碰撞”(注意了Trigger可不是真的碰撞,就像前边说的,Collision碰撞可能触发了Overlap,但是不会有物理表现,两物体会互相穿过,不发生碰撞之后的反弹,就算你开了PhysicsSimulate,也只是会对Trigger的mesh提供重力模拟,它碰到谁都是穿过的)

其实看这些预设的模式都不是很好懂,直接看persets里边customs的所有设置

CollisionEnable管理检测决定可以碰撞之后,是仅仅发生overlap回调、raycast(射线拾取)、sweep(扫描建模)还是有物理反馈,有下边三个选项 
1.NoCollision:就算重叠,并且通过channel检测可以碰撞,也不发生overlap,不raycast,不物理反馈 
2.NoPhysicsCollision:发生重叠,通过channel检测可以碰撞之后,进行overlap,raycast,但是不物理反馈,也就是会有一些函数被调用,但是一定会互相穿过 
3.CollisionEnable:发生重叠,channel决定可以碰撞,进行overlap(未确定,因为Block了之后可能不会overlap),raycast,并且进行物理反馈(要求碰撞双方至少有一方进行PhysicsSimulate,不然还是会穿模,毕竟如果两物体都不进行物理模拟,那么双方都不会回弹,但是都是刚体,物理上说不通,只能穿模),只有这个选项才会使得函数回调,并且有物理表现 
可见Collision的主要作用是两个: 
1.作为一个物理空间(可见或不可见),检测有没有东西进到里边,或者有没有射线与我相交 
2.作为一个真正的刚体(可见或不可见)

有了碰撞的反馈选项,还得有碰撞是否发生的检测,就是CollisionEnable后边的那一堆选项了 
ObjectType:里边有一堆选项,比如WorldDynamic,WorldStatic,Pawn等等(顺便一提WorldDynamic是指动态的物体,但不一定是PhysicSimulate的,比如一个电梯,它的运动不是实时计算的,比本可以说是个动画。而分类PhysicsBody是模拟物理实时计算的),但是估计都仅仅是分类,相当于Cocos2d里边的CategoryBitMask,进行物体分类方便管理怎么碰撞 
之后的打勾勾意义很简单,就是本物体会忽视哪些类型碰撞体,会仅仅和哪些类型发生overlap,又和哪些类型发生Block,这里的设定又是和CollisionEnabled里边的限制是与关系的,比如CollisionEnable里不允许发生物理反馈,而这里表示要发生Block,但是最后不会发生Block的。 
Block对于Trace而言和物理反馈是不一样的,比如你multiline射线拾取,第一个物体是Block,那么后边的都会拾取不到,第一个是Overlap,后边的能拾取到,直到被Block。 
(不过也有例外,比如打勾勾里边有一个Camera选项,就是说明摄像机是否能穿过这个物体,如果是Block,就算CollisionEnable是NoPhysicsCollision也会发生Block使得摄像机被挤出去。毕竟摄像机和其他Object不能一概而论) 
最后还得在强调一次,Collision里边进行Block是没有物理表现的,他只是检测并且要求PhysicsSimulate实现物理表现,所以就算Collision里边满足所有的Block条件,没有开PhysicsSimulate,也是不会有物理表现的

可见Collision是碰撞发生的条件检测和函数回调,是PhysicsSimulate的前提条件。 
附上官方解释https://docs.unrealengine.com/latest/INT/Engine/Physics/Collision/index.html?utm_source=editor&utm_medium=docs&utm_campaign=doc_anchors 
Collision Presets These are a collection of presets for the Collision Responses below. There is an INI file you can edit to change or add profiles. To edit anything below this point, you need to set this to “Custom…” 
Collision Enabled The 3 settings in here are as follows: 
No Collision - No collision is performed against this object, neither trace nor physics. 
No Physics Collision - This body is only used for raycasts, sweeps, and overlaps. 
Collision Enabled - This body is used for physics simulations and collision queries. 
Object Type This defines what the object should be considered when it moves. It has more to do with the interactions you see below than it does with defining an object as static or dynamic.

Collision Responses This sets all the collision responses below to the value checked. So if you check “Block” here, all of the rows below will also check “Block.” 
Trace Responses These define how an object should behave when interacting with a trace (usually just done with a ray cast). An object can choose to Block, Overlap, or simply Ignore a trace from the given source. There is no difference in the default trace responses except for their names. In code or Blueprint, you can use either for tracing a ray cast from one thing to another. But, for organization of traces, they should be used as their name implies. The default responses for Trace Responses are: 
Visibility - Trace from one location to another, if an object that blocks this type of trace is in the way, the trace is blocked. 
Camera - Exactly the same as Visibility, but should be used for setting when tracing from the camera. 
Object Responses An object can choose to Block, Overlap, or simply Ignore an object it is interacting with. The default responses for Object Responses are: 
WorldStatic - Volumes and world geometry should be WorldStatic. 
WorldDynamic - Moving Actors outside of Pawns, PhysicsBodies, Vehicles, and Destructible Actors. Like an elevator. 
Pawn - Characters. 
PhysicsBody - Any physics object that is or can be simulated in the world. 
Vehicle - A good response to have so you can have Pawns jump into them. 
Destructible - Destructible Actors.

PhysicsSimulate就不用太介绍了,Collision可以发生之后,PhysicsSimulate就计算碰撞之后的物理表现,实现一个真实的物理世界

补充一点,Collision使用的碰撞体问题: 
Collision使用的碰撞体全部是继承自Mesh的,而Mesh的碰撞体是在MeshEditor里边自行编辑的。Editor里边有一个选项CollisionComplexity,有三种选择,第一种是Simple,第二种是Complex,第三种是Default。

Simple:Mesh的碰撞体全部由简单立体图形组成,比如Sphere,Cube等等,可以手工添加和删除,与Mesh本体的匹配度不是很高,但是只有Simple支持PhysicsSimulate,这个是重点。 
Complex:每一个Ploy都会计算碰撞,计算量相当大,于是不支持PhysicsSimulate,实用Complex碰撞之后,如果再使用物理模拟的话,会穿过底面掉到无穷远去。

另外必须注意一点,就是我们人物的运动受控于两个大组件,一个是物理引擎,还有一个是MovementComponent。MovementComponent一定要注意,它很容易被忽视,比如CharacterMovementComponent,它的内部也实现了一个重力移动逻辑,和物理引擎是完全独立的!当你杀死一个人之后,把他的CapsuleComponent设置为Overlap,你就会发现他的CapsuleComponent陷到地下去了,你关了SimulatePhysics,关了GravityEnable都不行,这就是因为他的CharacterMovementComponent内部还是有一个重力逻辑,会使他落下,加上你把他的碰撞关掉了,所以他就陷到地底去了。

评论列表暂无评论
发表评论