فهرست منبع

cloning physics objects

If a mesh has a physics body it will now be cloned together with the
mesh.
Default behavior is to clone it, the developer can choose.
Raanan Weber 10 سال پیش
والد
کامیت
356749f0ed
2فایلهای تغییر یافته به همراه18 افزوده شده و 6 حذف شده
  1. 10 2
      src/Mesh/babylon.mesh.ts
  2. 8 4
      src/Physics/babylon.physicsImpostor.ts

+ 10 - 2
src/Mesh/babylon.mesh.ts

@@ -1009,8 +1009,16 @@
         }
 
         // Clone
-        public clone(name: string, newParent?: Node, doNotCloneChildren?: boolean): Mesh {
-            return new Mesh(name, this.getScene(), newParent, this, doNotCloneChildren);
+        public clone(name: string, newParent?: Node, doNotCloneChildren?: boolean, clonePhysicsImpostor: boolean = true): Mesh {
+            var mesh = new Mesh(name, this.getScene(), newParent, this, doNotCloneChildren);
+            //physics clone
+            if(clonePhysicsImpostor && this.getScene().getPhysicsEngine()) {
+                var impostor = this.getScene().getPhysicsEngine().getImpostorForPhysicsObject(this);
+                if(impostor) {
+                    mesh.physicsImpostor = impostor.clone(mesh);
+                }
+            }
+            return mesh;
         }
 
         // Dispose

+ 8 - 4
src/Physics/babylon.physicsImpostor.ts

@@ -49,13 +49,13 @@ module BABYLON {
             otherImpostor: PhysicsImpostor
         }>;
 
-        constructor(public object: IPhysicsEnabledObject, public type: number, private _options: PhysicsImpostorParameters = { mass: 0 }, scene?: Scene) {
+        constructor(public object: IPhysicsEnabledObject, public type: number, private _options: PhysicsImpostorParameters = { mass: 0 }, private _scene?: Scene) {
             //legacy support for old syntax.
-            if (!scene && object.getScene) {
-                scene = object.getScene()
+            if (!this._scene && object.getScene) {
+                this._scene = object.getScene()
             }
 
-            this._physicsEngine = scene.getPhysicsEngine();
+            this._physicsEngine = this._scene.getPhysicsEngine();
             if (!this._physicsEngine) {
                 Tools.Error("Physics not enabled. Please use scene.enablePhysics(...) before creating impostors.")
             } else {
@@ -377,6 +377,10 @@ module BABYLON {
         public wakeUp() {
             this._physicsEngine.getPhysicsPlugin().wakeUpBody(this);
         }
+        
+        public clone(newObject:IPhysicsEnabledObject) {
+            return new PhysicsImpostor(newObject, this.type, this._options, this._scene);
+        }
 
         public dispose(/*disposeChildren: boolean = true*/) {
             this.physicsBody = null;