[LayaAir 2.0]rigidBody.applyForce 对刚体应用力,物体没有移动
1.问题描述
如题,通过给物体施加力rigidBody.applyForce,物体没有发生移动。
2.重现步骤
在https://layaair2.ldc2.layabox.com/demo2/?language=ch&category=3d&group=Physics3D&name=PhysicsWorld_BaseCollider
粘贴以下代码执行:
//问题在 addSphere 方法
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.diffuseColor = 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;
this.mat2 = new Laya.BlinnPhongMaterial;
this.mat3 = new Laya.BlinnPhongMaterial;
this.mat4 = new Laya.BlinnPhongMaterial;
this.mat5 = new Laya.BlinnPhongMaterial;
//添加漫反射贴图
Laya.Texture2D.load("res/threeDimen/Physics/rocks.jpg", Laya.Handler.create(this, function (tex) {
this.mat1.albedoTexture = tex;
}));
Laya.Texture2D.load("res/threeDimen/Physics/plywood.jpg", Laya.Handler.create(this, function (tex) {
this.mat2.albedoTexture = tex;
}));
Laya.Texture2D.load("res/threeDimen/Physics/wood.jpg", Laya.Handler.create(this, function (tex) {
this.mat3.albedoTexture = tex;
}));
Laya.Texture2D.load("res/threeDimen/Physics/steel2.jpg", Laya.Handler.create(this, function(tex) {
this.mat4.albedoTexture = tex;
}));
Laya.Texture2D.load("res/threeDimen/Physics/steel.jpg", Laya.Handler.create(this, function(tex) {
this.mat5.albedoTexture = tex;
}));
//平面加载
let plane = this.scene.addChild(new Laya.MeshSprite3D(Laya.PrimitiveMesh.createPlane(10, 10, 10, 10)));
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;
//创建球
this.addSphere();
}
//添加球体
addSphere(){
let radius = 0.5;
let sphere = this.scene.addChild(new Laya.MeshSprite3D(Laya.PrimitiveMesh.createSphere(radius)));
sphere.meshRenderer.material = this.mat2;
this.tmpVector.setValue(0, 2, 2);
sphere.transform.position = this.tmpVector;
let rigidBody = sphere.addComponent(Laya.Rigidbody3D);
let sphereShape = new Laya.SphereColliderShape(radius);
rigidBody.colliderShape = sphereShape;
rigidBody.mass = 1;
rigidBody.gravity = new Laya.Vector3(0, 0, 0);//取消重力
rigidBody.applyForce(new Laya.Vector3(0, 0, -1000));//应用力
//rigidBody.linearVelocity = new Laya.Vector3(0,0,-1);
Laya.timer.loop(2000, this, function(){console.log(rigidBody.linearVelocity.z)});
}
}
//激活启动类
new PhysicsWorldBaseCollider();
如题,通过给物体施加力rigidBody.applyForce,物体没有发生移动。
2.重现步骤
在https://layaair2.ldc2.layabox.com/demo2/?language=ch&category=3d&group=Physics3D&name=PhysicsWorld_BaseCollider
粘贴以下代码执行:
//问题在 addSphere 方法
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.diffuseColor = 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;
this.mat2 = new Laya.BlinnPhongMaterial;
this.mat3 = new Laya.BlinnPhongMaterial;
this.mat4 = new Laya.BlinnPhongMaterial;
this.mat5 = new Laya.BlinnPhongMaterial;
//添加漫反射贴图
Laya.Texture2D.load("res/threeDimen/Physics/rocks.jpg", Laya.Handler.create(this, function (tex) {
this.mat1.albedoTexture = tex;
}));
Laya.Texture2D.load("res/threeDimen/Physics/plywood.jpg", Laya.Handler.create(this, function (tex) {
this.mat2.albedoTexture = tex;
}));
Laya.Texture2D.load("res/threeDimen/Physics/wood.jpg", Laya.Handler.create(this, function (tex) {
this.mat3.albedoTexture = tex;
}));
Laya.Texture2D.load("res/threeDimen/Physics/steel2.jpg", Laya.Handler.create(this, function(tex) {
this.mat4.albedoTexture = tex;
}));
Laya.Texture2D.load("res/threeDimen/Physics/steel.jpg", Laya.Handler.create(this, function(tex) {
this.mat5.albedoTexture = tex;
}));
//平面加载
let plane = this.scene.addChild(new Laya.MeshSprite3D(Laya.PrimitiveMesh.createPlane(10, 10, 10, 10)));
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;
//创建球
this.addSphere();
}
//添加球体
addSphere(){
let radius = 0.5;
let sphere = this.scene.addChild(new Laya.MeshSprite3D(Laya.PrimitiveMesh.createSphere(radius)));
sphere.meshRenderer.material = this.mat2;
this.tmpVector.setValue(0, 2, 2);
sphere.transform.position = this.tmpVector;
let rigidBody = sphere.addComponent(Laya.Rigidbody3D);
let sphereShape = new Laya.SphereColliderShape(radius);
rigidBody.colliderShape = sphereShape;
rigidBody.mass = 1;
rigidBody.gravity = new Laya.Vector3(0, 0, 0);//取消重力
rigidBody.applyForce(new Laya.Vector3(0, 0, -1000));//应用力
//rigidBody.linearVelocity = new Laya.Vector3(0,0,-1);
Laya.timer.loop(2000, this, function(){console.log(rigidBody.linearVelocity.z)});
}
}
//激活启动类
new PhysicsWorldBaseCollider();
要回复问题请先登录
5 个回复
MarsLiu
赞同来自:
TTtwo
赞同来自:
Rounder
赞同来自:
改成rigidBody.applyForce(new Laya.Vector3(-1000,0,0, ));
夜未央
赞同来自:
1591094979用户
赞同来自:
你们解决了嘛