[]没有人尝试使用3d物理引擎么

还处于摸索阶段,想问下大家没人在做3d游戏么,物理引擎这块怎么解决的?
我找到了一个叫做“cannon.js”的3d物理引擎项目:
http://www.cannonjs.org/ 
感觉它的结构还算清晰,api也比较详细简洁,目前我是尝试了通过创建cannon的物理场景和刚体几何体对象,由laya.timer驱动cannon解算物理变化,然后不断把cannon世界里的物体变化实时赋值给laya场景里的物体,但是感觉效率有点低啊……
 
比如:
package
{
import laya.d3.core.BaseCamera;
import laya.d3.core.Camera;
import laya.d3.core.MeshSprite3D;
import laya.d3.core.light.DirectionLight;
import laya.d3.core.material.StandardMaterial;
import laya.d3.core.scene.Scene;
import laya.d3.math.RandX;
import laya.d3.math.Vector3;
import laya.d3.resource.Texture2D;
import laya.d3.resource.TextureCube;
import laya.d3.resource.models.BoxMesh;
import laya.d3.resource.models.QuadMesh;
import laya.d3.resource.models.SkyBox;
import laya.display.Stage;
import laya.utils.Browser;
import laya.utils.Stat;
import laya.utils.Timer;

import threeDimen.common.CameraMoveScript;
import threeDimen.common.CameraStat;
import threeDimen.common.SpriteMoveScript;
import threeDimen.common.SpriteStat;

public class LayaAir3D
{

private var box:MeshSprite3D;

private var sphereBody:Object;

private var world:Object;

private var CANNON:Object;

public function LayaAir3D()
{

//初始化引擎
Laya3D.init(0, 0, true);

//适配模式
Laya.stage.scaleMode=Stage.SCALE_FULL;
Laya.stage.screenMode=Stage.SCREEN_VERTICAL;

//开启统计信息
Stat.show();

//添加3D场景
var scene:Scene=Laya.stage.addChild(new Scene()) as Scene;

//添加照相机
var camera:Camera=(scene.addChild(new Camera(0, 0.1, 100))) as Camera;
camera.transform.translate(new Vector3(0, 3, 6));
camera.transform.rotate(new Vector3(-15, 0, 0), true, false);
camera.clearColor=null;
camera.addComponent(CameraMoveScript);
CameraStat.show(camera, 200, 0);

//添加方向光
var directionLight:DirectionLight=scene.addChild(new DirectionLight()) as DirectionLight;
directionLight.ambientColor=new Vector3(0.6, 0.6, 0.6);
directionLight.specularColor=new Vector3(0.6, 0.6, 0.6);
directionLight.diffuseColor=new Vector3(1.6, 1.6, 1.6);
directionLight.direction=new Vector3(1, -1, 0);

//添加自定义模型
box=scene.addChild(new MeshSprite3D(new BoxMesh(1, 1, 1))) as MeshSprite3D;
box.transform.rotate(new Vector3(0, 0, 0), false, false);
var material:StandardMaterial=new StandardMaterial();
material.diffuseTexture=Texture2D.load("res/layabox.png");
box.meshRender.material=material;

box.addComponent(SpriteMoveScript);
SpriteStat.show(box, 200, 50);


//添加场景地板
var floor:MeshSprite3D=scene.addChild(new MeshSprite3D(new QuadMesh(100, 100))) as MeshSprite3D;
var floorMat:StandardMaterial=new StandardMaterial();
floorMat.diffuseTexture=Texture2D.load("res/bw.jpg");
floor.meshRender.material=floorMat;

//添加天空盒
camera.clearFlag=BaseCamera.CLEARFLAG_SKY;
var sky:SkyBox=new SkyBox();
sky.textureCube=TextureCube.load("res/skybox/skybox.ltc");
camera.sky=sky;

initCannon();

}

private function initCannon():void
{
CANNON=Browser.window.CANNON;
world=new CANNON.World();
world.gravity.set(0, 0, -9.8);

// 设置碰撞检测类型
world.broadphase=new CANNON.NaiveBroadphase();

// 创建几何体(几何体和物理实体),添加到加农世界
// 球体,质量5,半径1
var mass:Object=50, radius:Object=1;
var sphereShape:Object=new CANNON.Sphere(radius);
sphereBody=new CANNON.Body({mass: mass, shape: sphereShape});
sphereBody.position.set(0, 0, 100);
world.add(sphereBody);

// 创建世界平面,添加到加农世界
var groundShape:Object=new CANNON.Plane();
var groundBody:Object=new CANNON.Body({mass: 0, shape: groundShape});

world.add(groundBody);

// 运算器步进时长
var timeStep:Object=1.0 / 60.0;


box.timer.loop(1000/60,this,cannonUpdate);

}

private function cannonUpdate():void{
// 循环步进驱动解算
world.step(1.0 / 60.0);
if(box.timer.currFrame %500 == 0){
sphereBody.velocity = new CANNON.Vec3(0,0,20) as Object;
}
box.transform.translate(new Vector3(0, sphereBody.position.z - box.transform.position.y, 0));

}

}
}
已邀请:

cuixueying

赞同来自:

物理引擎如果想使用的话,最好使用多线程处理,貌似有个叫做web worker的东西,你可以研究下...

要回复问题请先

商务合作
商务合作