[LayaAirIDE 2.0]3D网格添加刚体后设置欧拉角出现位置错误
第一个网格在添加刚体后设置position,rotationEuler可设可不设
第二个网格在添加刚体后设置rotationEuler,不设置position
第二个刚体会带有第一个刚体的position,受刚体影响,第二个网格将与第一个网格出现在同一个位置
在添加刚体前设置则不会出现,第二个网格不设置rotationEuler或设置position也不会出现
将下面的代码拷贝到引擎示例(3D-物理系统-基础碰撞器)中运行即可:
class PhysicsWorldBaseCollider{
constructor(){
//初始化引擎
Laya3D.init(0, 0);
//设置画布模式
Laya.stage.scaleMode = Laya.Stage.SCALE_FULL;
Laya.stage.screenMode = Laya.Stage.SCREEN_NONE;
Laya.Stat.show();
this.tmpVector = new Laya.Vector3(0, 0, 0);
this.scene = Laya.stage.addChild(new Laya.Scene3D());
//初始化照相机
let camera = this.scene.addChild(new Laya.Camera(0, 0.1, 100));
camera.transform.translate(new Laya.Vector3(0, 6, 9.5));
camera.transform.rotate(new Laya.Vector3(-15, 0, 0), true, false);
camera.addComponent(CameraMoveScript);
camera.clearFlag = Laya.BaseCamera.CLEARFLAG_SOLIDCOLOR;
//方向光
let directionLight = this.scene.addChild(new Laya.DirectionLight());
directionLight.color = new Laya.Vector3(0.6, 0.6, 0.6);
let mat = directionLight.transform.worldMatrix;
mat.setForward(new Laya.Vector3(-1.0, -1.0, -1.0));
directionLight.transform.worldMatrix = mat;
//材质加载
this.mat1 = new Laya.BlinnPhongMaterial;
//添加漫反射贴图
Laya.Texture2D.load("res/threeDimen/Physics/rocks.jpg", Laya.Handler.create(this, function (tex) {
this.mat1.albedoTexture = tex;
}));
//平面加载
let plane = this.scene.addChild(new Laya.MeshSprite3D(Laya.PrimitiveMesh.createPlane(10, 10, 10, 10)));
plane.transform.position = new Laya.Vector3(0, -2, 0);
let planeMat = new Laya.BlinnPhongMaterial();
Laya.Texture2D.load("res/threeDimen/Physics/grass.png", Laya.Handler.create(null, function (tex) {
planeMat.albedoTexture = tex;
}));
//拉伸UV
planeMat.tilingOffset = new Laya.Vector4(10, 10, 0, 0);
//板的画面的渲染材质
plane.meshRenderer.material = planeMat;
//给板添加物理碰撞器
let planeStaticCollider = plane.addComponent(Laya.PhysicsCollider);
//设置碰撞器
let planeShape = new Laya.BoxColliderShape(10, 0, 10);
planeStaticCollider.colliderShape = planeShape;
planeStaticCollider.friction = 2;
planeStaticCollider.restitution = 0.3;
//设置box模型
let box = this.scene.addChild(new Laya.MeshSprite3D(Laya.PrimitiveMesh.createBox(1, 1, 1)));
//将box的模型赋予材质
box.meshRenderer.material = this.mat1;
//添加刚体组件
let rigidBody = box.addComponent(Laya.Rigidbody3D);
let boxShape = new Laya.BoxColliderShape(1, 1, 1);
rigidBody.colliderShape = boxShape;
rigidBody.gravity = new Laya.Vector3();
//设置旋转角度
this.tmpVector.setValue(Math.random() * 360, Math.random() * 360, Math.random() * 360);
box.transform.rotationEuler = this.tmpVector;
//设置掉落位置
this.tmpVector.setValue(-2, 2, -2);
box.transform.position = this.tmpVector;
let sphere = this.scene.addChild(new Laya.MeshSprite3D(Laya.PrimitiveMesh.createSphere(0.5)));
sphere.meshRenderer.material = this.mat1;
let rigidBody2 = sphere.addComponent(Laya.Rigidbody3D);
let sphereShape = new Laya.SphereColliderShape(0.5);
rigidBody2.colliderShape = sphereShape;
rigidBody2.gravity = new Laya.Vector3();
//设置旋转角度
this.tmpVector.setValue(Math.random() * 360, Math.random() * 360, Math.random() * 360);
sphere.transform.rotationEuler= this.tmpVector;
}
}
//激活启动类
new PhysicsWorldBaseCollider();
没有找到相关结果
已邀请:
要回复问题请先登录
2 个回复
Laya_z
赞同来自:
影子路过
赞同来自: