---
title: "jiglibflash--Flash 3D physics engine"
author: "AS3"
date: "2011-03-09T13:06:32Z"
tags: ["Blog", "Flash", "Google", "SVN", "网络应用"]
categories: ["编程语言"]
slug: "jiglibflash-flash-3d-physics-engine"
draft: false
lastmod: "2011-03-09T13:06:32Z"
id: 948210
url: "https://as3.iteye.com/blog/948210"
---

<http://code.google.com/p/jiglibflash/>

Jiglibflash is a as3 open source 3D physics engine with a MIT license (ported from c++ Jiglib).

Supported 3D engines are: Away 3D, Away 3D lite, Five 3D, Sandy 3D and Papervision 3D.

The source and examples can be found in the trunk at the SVN (see source tab). There's a flash 9 and flash 10 version.

There is no documentation yet, but you can find a short tutorial in the Wiki.

You can reach our blog at: www.jiglibflash.com/blog.








原帖地址http://www.antscript.cn/jiglibflash%e8%b6%85%e7%ae%80%e6%98%93%e5%85%a5%e9%97%a8.html

     JiglibFlash作为Flash 3D中为数不多的一个物理引擎（开源的好像还有一个WOW,另外就是没开源的日本那个超强的3D+物理引擎）也算为这方面填补了空缺，说到效率，JiglibFlash已经能够应付那些要求不是特别高的应用了。
　　
　　
　　官方博客：http://www.jiglibflash.com/blog/
　　google code ：http://code.google.com/p/jiglibflash/
　　
　　

　　使用JiglibFlash其实是很简单，只要了解了它的工作原理，用起来就很容易了，不过想要完全理解它的架构、API、底层原理这些东西，没有一定的数学和物理能力是不行的，不过对于开发者来说，会用就可以了。
　
　　它的工作流程形象的大概描述出来是这样的，对于在3D场景的每一个物体，在物理引擎中建立一个外形与之相同（更多时候是相似）的物理模型（不可见的），物理引擎检测计算之后，将结果在3D场景中的物体上反映出来。
　　
大概流程如下：
　　
1.初始化物理引擎
　　
2.创建3D物体
　　
3.在物理引擎中创建物理模型并与相关3D物体形成映射
　　
4.每帧渲染前进行物理检测
　　

其中有一点需要注意：一旦建立映射以后，对物理模型的操作都将自动应用于与其相对应的3D物体上。

　
　
下面用一段简单的代码来实现一个小球落到一个倾斜的平面上的物理效果　
　　
　　

?View Code ACTIONSCRIPT3package cn.antscript.JiglibFlash_Tutorials 
{
        import flash.events.Event;

        import jiglib.geometry.JPlane;
        import jiglib.geometry.JSphere;
        import jiglib.math.JNumber3D;
        import jiglib.plugin.papervision3d.Papervision3DPhysics;
        import jiglib.plugin.papervision3d.Pv3dMesh;

        import org.papervision3d.lights.PointLight3D;
        import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
        import org.papervision3d.materials.WireframeMaterial;
        import org.papervision3d.objects.primitives.Plane;
        import org.papervision3d.objects.primitives.Sphere;
        import org.papervision3d.view.BasicView;

        /**
         * ...
         * @author AntScript
         * @blog   http://www.antscript.cn
         */
        public class Main extends BasicView
        {
                private var _physicsapervision3DPhysics;

                private var _ball:Sphere;
                private var _pBall:JSphere;

                private var _groundlane;
                private var _pGround:JPlane;

                public function Main() 
                {
                        init();
                }

                private function init():void
                {
                        initPV3D();
                        initPhysics();

                        startRendering();
                }
                //初始化PV3D
                private function initPV3D():void
                {
                        //实例化一个球体，放到场景中
                        _ball = new Sphere(new FlatShadeMaterial(new PointLight3D()),100,20,20);                        
                        scene.addChild(_ball);
                        //实例化一块地面，放到场景中
                        _ground = new Plane(new WireframeMaterial(0x000000), 2000, 2000, 20, 20);
                        scene.addChild(_ground);
                }
                //初始化Jiglib
                private function initPhysics():void
                {
                        //初始化物理引擎
                        _physics = new Papervision3DPhysics(scene, 5);
                        //创建一个物理球体模型，并与_ball形成映射
                        _pBall = new JSphere(new Pv3dMesh(_ball), 100);
                        //设置质量，位置
                        _pBall.mass = 10;
                        _pBall.moveTo(new JNumber3D(0, 300, 0));
                        //加入到物理引擎中
                        _physics.addBody(_pBall);


                        //创建一个物理平面模型，并与_ground形成映射
                        _pGround = new JPlane(new Pv3dMesh(_ground));
                        //设置地面位置及方向等
                        _pGround.moveTo(new JNumber3D(0, -200, 0));                        
                        _pGround.rotationX = 90;
                        _pGround.rotationY = -10;
                        //设置地面不能动
                        _pGround.movable = false;
                        //加入到物理引擎
                        _physics.addBody(_pGround);                        
                }

                override protected function onRenderTick(event:Event = null):void
                {
                        //进行物理检测
                        _physics.step();

                        super.onRenderTick(event);
                }

        }

} 

　　
　　
点击打开新页面看效果
　　
　　
　　这只是一个十分简易的教程，要做出一些应用的话还需更加深入的研究，它自带的和官网上网络上都有很多资料代码，周末马上到了，have fun~









jiglib.cof:
        JConfig:                                配置类

jiglib.collision:
        CollDetectBoxBox:                        立方体与立方体碰撞检测
        CollDetectBoxPlane:                立方体与平面碰撞检测
        CollDetectCapsuleBox:                立方体与胶囊体碰撞检测
        CollDetectCapsuleCapsule:                胶囊体与胶囊体碰撞检测
        CollDetectCapsulePlane:                胶囊体与平面碰撞检测
        CollDetectFunctor:                        碰撞检测基类
        CollDetectInfo:                        碰撞检测数据类
        CollDetectSphereBox:                球体与立方体碰撞检测
        CollDetectSphereCapsule:                球体与胶囊体碰撞检测
        CollDetectSpherePlane:                球体与平面碰撞检测
        CollDetectSphereSphere:                球体与球体碰撞检测
        CollisionInfo:                        碰撞结果信息
        CollisionSystem:                        碰撞管理类
        CollPointInfo:                        碰撞点信息

jiglib.geometry:
        JAABox:                                边界盒
        JBox:                                刚体-立方体
        JCapsule:                                刚体-胶囊体
        JPlane:                                刚体-平面
        JRay:                                射线
        JSegment:                        线段
        JSphere:                                刚体-球体

jiglib.math:
        JMatrix3D:                        4*3矩阵
        JNumber3D:                        3D数学类

jiglib.physics:
        BodyPair:                                刚体对
        CachedImpulse:                        缓存类
        HingeJoint:                        铰链关节
        MaterialProperties:                        材质属性
        PhysicsController:                        物理控制器(关节基类)
        PhysicsState:                        物理状态
        PhysicsSystem:                        物体引擎主类
        RigidBody:                        所有刚体基类