public GameObject CreateUnityCollisionObjectProxy(CollisionObject body) { if (body is GhostObject) { Debug.Log("ghost obj"); } GameObject go = new GameObject(body.ToString()); MeshFilter mf = go.AddComponent <MeshFilter>(); Mesh m = mf.mesh; MeshFactory2.CreateShape(body.CollisionShape, m); MeshRenderer mr = go.AddComponent <MeshRenderer>(); mr.sharedMaterial = mat; if (body.UserObject != null && body.UserObject.Equals("Ground")) { mr.sharedMaterial = groundMat; } BulletRigidBodyProxy rbp = go.AddComponent <BulletRigidBodyProxy>(); rbp.target = body; return(go); }
public GameObject CreateUnityCollisionObjectProxy(CollisionObject body, string name) { if (body is GhostObject) { Debug.Log("ghost obj"); } GameObject go = new GameObject(name); MeshFilter mf = go.AddComponent <MeshFilter>(); Mesh m = mf.mesh; MeshFactory2.CreateShape(body.CollisionShape, m); MeshRenderer mr = go.AddComponent <MeshRenderer>(); mr.sharedMaterial = mat; BulletRigidBodyProxy rbp = go.AddComponent <BulletRigidBodyProxy>(); rbp.target = body; return(go); }
public void PostOnInitializePhysics() { for (int i = 0; i < demo.World.CollisionObjectArray.Count; i++) { CollisionObject co = demo.World.CollisionObjectArray[i]; CollisionShape cs = co.CollisionShape; GameObject go; if (cs.ShapeType == BroadphaseNativeType.SoftBodyShape) { BulletSharp.SoftBody.SoftBody sb = (BulletSharp.SoftBody.SoftBody)co; if (sb.Faces.Count == 0) { //rope go = CreateUnitySoftBodyRope(sb); } else { go = CreateUnitySoftBodyCloth(sb); } } else { //rigid body if (cs.ShapeType == BroadphaseNativeType.CompoundShape) { BulletSharp.Math.Matrix transform = co.WorldTransform; go = new GameObject("Compund Shape"); BulletRigidBodyProxy rbp = go.AddComponent <BulletRigidBodyProxy>(); rbp.target = co as RigidBody; foreach (BulletSharp.CompoundShapeChild child in (cs as CompoundShape).ChildList) { BulletSharp.Math.Matrix childTransform = child.Transform; GameObject ggo = new GameObject(child.ToString()); MeshFilter mf = ggo.AddComponent <MeshFilter>(); Mesh m = mf.mesh; MeshFactory2.CreateShape(child.ChildShape, m); MeshRenderer mr = ggo.AddComponent <MeshRenderer>(); mr.sharedMaterial = mat; ggo.transform.SetParent(go.transform); Matrix4x4 mt = childTransform.ToUnity(); ggo.transform.localPosition = BSExtensionMethods2.ExtractTranslationFromMatrix(ref mt); ggo.transform.localRotation = BSExtensionMethods2.ExtractRotationFromMatrix(ref mt); ggo.transform.localScale = BSExtensionMethods2.ExtractScaleFromMatrix(ref mt); /* * BulletRigidBodyProxy rbp = ggo.AddComponent<BulletRigidBodyProxy>(); * rbp.target = body; * return go; */ //InitRigidBodyInstance(colObj, child.ChildShape, ref childTransform); } } else if (cs.ShapeType == BroadphaseNativeType.CapsuleShape) { GameObject ggo = GameObject.CreatePrimitive(PrimitiveType.Capsule); Destroy(ggo.GetComponent <Collider>()); go = new GameObject(); ggo.transform.parent = go.transform; ggo.transform.localPosition = Vector3.zero; ggo.transform.localRotation = Quaternion.identity; BulletRigidBodyProxy rbp = go.AddComponent <BulletRigidBodyProxy>(); rbp.target = co as RigidBody; } else { Debug.Log("Creating " + cs.ShapeType + " for " + co.ToString()); go = CreateUnityCollisionObjectProxy(co as CollisionObject); } } createdObjs.Add(go); Debug.Log("Created Unity Shape for " + co); } }
// Creates a Unity game object from the given Bullet CollisionObject. protected void AddUnityObject(CollisionObject co, Material mat) { CollisionShape cs = co.CollisionShape; GameObject go; if (cs.ShapeType == BroadphaseNativeType.SoftBodyShape) { BulletSharp.SoftBody.SoftBody sb = (BulletSharp.SoftBody.SoftBody)co; if (sb.Faces.Count == 0) { //rope go = CreateUnitySoftBodyRope(sb); } else { go = CreateUnitySoftBodyCloth(sb); } } else { //rigid body if (cs.ShapeType == BroadphaseNativeType.CompoundShape) { //BulletSharp.Math.Matrix transform = co.WorldTransform; go = new GameObject("Compund Shape"); BulletRigidBodyProxy rbp = go.AddComponent <BulletRigidBodyProxy>(); rbp.target = co as RigidBody; foreach (BulletSharp.CompoundShapeChild child in (cs as CompoundShape).ChildList) { BulletSharp.Math.Matrix childTransform = child.Transform; GameObject ggo = new GameObject(child.ToString()); MeshFilter mf = ggo.AddComponent <MeshFilter>(); Mesh m = mf.mesh; MeshFactory2.CreateShape(child.ChildShape, m); MeshRenderer mr = ggo.AddComponent <MeshRenderer>(); mr.sharedMaterial = mat; ggo.transform.SetParent(go.transform); Matrix4x4 mt = childTransform.ToUnity(); ggo.transform.localPosition = BSExtensionMethods2.ExtractTranslationFromMatrix(ref mt); ggo.transform.localRotation = BSExtensionMethods2.ExtractRotationFromMatrix(ref mt); ggo.transform.localScale = BSExtensionMethods2.ExtractScaleFromMatrix(ref mt); /* * BulletRigidBodyProxy rbp = ggo.AddComponent<BulletRigidBodyProxy>(); * rbp.target = body; * return go; */ //InitRigidBodyInstance(colObj, child.ChildShape, ref childTransform); } } else if (cs.ShapeType == BroadphaseNativeType.CapsuleShape) { CapsuleShape css = (CapsuleShape)cs; GameObject ggo = GameObject.CreatePrimitive(PrimitiveType.Capsule); Destroy(ggo.GetComponent <Collider>()); go = new GameObject(); ggo.transform.parent = go.transform; ggo.transform.localPosition = UnityEngine.Vector3.zero; ggo.transform.localRotation = UnityEngine.Quaternion.identity; ggo.transform.localScale = new UnityEngine.Vector3(css.Radius * 2f, css.HalfHeight * 2f, css.Radius * 2f); BulletRigidBodyProxy rbp = go.AddComponent <BulletRigidBodyProxy>(); rbp.target = co; } else { //Debug.Log("Creating " + cs.ShapeType + " for " + co.ToString()); go = CreateUnityCollisionObjectProxy(co as CollisionObject, mat); } } m_createdObjs.Add(go); }