P2.JS之触碰材质(ContactMaterial)备忘

关键代码:
 1 boxShape.material = new p2.Material();
 2 platformShape.material = new p2.Material();
 3 
 4 var material = new p2.ContactMaterial(boxShape.material, platformShape.material, {
 5     surfaceVelocity: -50,
 6     restitution: 0.5,
 7     friction: 0.3,
 8     stiffness: 1e7
 9 });
10 world.addContactMaterial(material);
 
像很普遍的弹跳效果就需要用到这个组件了。
 
技术分享
 
这段代码的效果其实就是方块自由落体到挡板上,有轻微反弹效果,并且有向右的速度。
 
根据我试验的结果,不管是boxShape还是platformShape作为bodyA,当surfaceVelocity小于0时,方块向右移动,大于0时,自然向左。
 
restitution为反弹力属性,默认值1。friction是摩擦力,默认值0.3。stiffness是刚度属性,默认值1e7。
 
这个ContactMaterial类,还有其它的属性,不过没试出效果来,就没写到例子上去了,分别是:
 
relaxation,Relaxation of the resulting ContactEquation that this ContactMaterial generate

frictionStiffness,Stiffness of the resulting FrictionEquation that this ContactMaterial generate

frictionRelaxation,Relaxation of the resulting FrictionEquation that this ContactMaterial generate

contactSkinSize,Offset to be set on ContactEquations. A positive value will make the bodies penetrate more into each other. Can be useful in scenes where contacts need to be more persistent, for example when stacking. Aka "cure for nervous contacts".
 
实在也不知道怎么翻译,就直接贴上来当作备忘吧。
 
 
p2.js v0.6.0 的压缩包,链接:http://pan.baidu.com/s/1mgsW7xA 密码:bj85
p2.js 的Github地址:https://github.com/schteppe/p2.js

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。