private void CreateWheel() { WheelDriverMeta wheel = this.GetDriverMeta <WheelDriverMeta>(); BSphereShape sphereShape = MainObject.AddComponent <BSphereShape>(); sphereShape.Radius = wheel.radius * 0.01f; }
/// <summary> /// Creates node_0 of a manipulator for QuickSwap mode. Node_0 is used to attach the manipulator to the robot. /// </summary> public void CreateManipulatorJoint(GameObject robot) { //Ignore physics/collisions between the manipulator and the robot. Currently not working. foreach (BRigidBody rb in robot.GetComponentsInChildren <BRigidBody>()) { MainObject.GetComponent <BRigidBody>().GetCollisionObject().SetIgnoreCollisionCheck(rb.GetCollisionObject(), true); } if (joint != null || GetSkeletalJoint() == null) { BHingedConstraintEx hc = MainObject.AddComponent <BHingedConstraintEx>(); hc.thisRigidBody = MainObject.GetComponent <BRigidBody>(); hc.otherRigidBody = robot.GetComponentInChildren <BRigidBody>(); hc.axisInA = new Vector3(0, 1, 0); hc.axisInB = new Vector3(0, 1, 0); hc.setLimit = true; hc.localConstraintPoint = new Vector3(0, 0, 0); hc.lowLimitAngleRadians = 0; hc.highLimitAngleRadians = 0; hc.constraintType = BTypedConstraint.ConstraintType.constrainToAnotherBody; } }
private T ConfigJoint <T>(Vector3 position, Vector3 axis, AxisType axisType) where T : BTypedConstraint { GameObject parent = ((RigidNode)GetParent()).MainObject; T joint = MainObject.AddComponent <T>(); joint.otherRigidBody = parent.GetComponent <BRigidBody>(); joint.localConstraintPoint = position; joint.debugDrawSize = 0.1f; return(joint); }
private void CreateWheel() { BRigidBody rigidBody = MainObject.GetComponent <BRigidBody>(); rigidBody.friction = WHEEL_FRICTION; WheelDriverMeta wheel = this.GetDriverMeta <WheelDriverMeta>(); BSphereShape sphereShape = MainObject.AddComponent <BSphereShape>(); sphereShape.Radius = wheel.radius * 0.01f; // Old cylinder method... collision not as smoth. ////BCylinderShapeZ cylinderShape = MainObject.AddComponent<BCylinderShapeZ>(); //BCylinderShapeX cylinderShape = MainObject.AddComponent<BCylinderShapeX>(); //cylinderShape.HalfExtent = new Vector3(wheel.radius * 1.5f, wheel.width, wheel.width) * 0.01f; //cylinderShape.GetCollisionShape().Margin = wheel.radius * 0.005f; // hopefully change later? // ... and continuous collision detection rigidBody.GetCollisionObject().CcdMotionThreshold = CCD_MOTION_THRESHOLD; rigidBody.GetCollisionObject().CcdSweptSphereRadius = CCD_SWEPT_SPHERE_RADIUS; }
/// <summary> /// Creates node_0 of a manipulator for QuickSwap mode. Node_0 is used to attach the manipulator to the robot. /// </summary> public void CreateManipulatorJoint() { //Ignore physics/collisions between the manipulator and the robot. Currently not working. foreach (BRigidBody rb in GameObject.Find("Robot").GetComponentsInChildren <BRigidBody>()) { MainObject.GetComponent <BRigidBody>().GetCollisionObject().SetIgnoreCollisionCheck(rb.GetCollisionObject(), true); } if (joint != null || GetSkeletalJoint() == null) { RotationalJoint_Base rNode = new RotationalJoint_Base(); B6DOFConstraint hc = MainObject.AddComponent <B6DOFConstraint>(); hc.thisRigidBody = MainObject.GetComponent <BRigidBody>(); hc.otherRigidBody = GameObject.Find("Robot").GetComponentInChildren <BRigidBody>(); hc.localConstraintPoint = ComOffset; //Put this after everything else hc.constraintType = BTypedConstraint.ConstraintType.constrainToAnotherBody; } }
/* * public RigidNode(RigidNode_Base parent) : base(parent){ * * } */ public bool CreateMesh(string filePath, bool isMixAndMatch = false, float wheelMass = 1.0f) { //Debug.Log(filePath); BXDAMesh mesh = new BXDAMesh(); mesh.ReadFromFile(filePath); //if (!mesh.GUID.Equals(GUID)) //{ // Debug.Log("Returning false"); // return false; //} List <GameObject> meshObjects = new List <GameObject>(); Auxiliary.ReadMeshSet(mesh.meshes, delegate(int id, BXDAMesh.BXDASubMesh sub, Mesh meshu) { GameObject meshObject = new GameObject(MainObject.name + "_mesh"); meshObjects.Add(meshObject); meshObject.AddComponent <MeshFilter>().mesh = meshu; meshObject.AddComponent <MeshRenderer>(); Material[] materials = new Material[meshu.subMeshCount]; for (int i = 0; i < materials.Length; i++) { materials[i] = sub.surfaces[i].AsMaterial(true); } meshObject.GetComponent <MeshRenderer>().materials = materials; meshObject.transform.position = root.position; meshObject.transform.rotation = root.rotation; }, true); Vector3 com = mesh.physics.centerOfMass.AsV3(); ComOffset = com; Mesh[] colliders = new Mesh[mesh.colliders.Count]; Auxiliary.ReadMeshSet(mesh.colliders, delegate(int id, BXDAMesh.BXDASubMesh sub, Mesh meshu) { colliders[id] = meshu; }, true); MainObject.transform.position = root.position + ComOffset; MainObject.transform.rotation = root.rotation; foreach (GameObject meshObject in meshObjects) { meshObject.transform.parent = MainObject.transform; } if (!this.HasDriverMeta <WheelDriverMeta>() || this.GetDriverMeta <WheelDriverMeta>().type == WheelType.NOT_A_WHEEL) { BMultiShape hullShape = MainObject.AddComponent <BMultiShape>(); foreach (Mesh collider in colliders) { //Mesh m = AuxFunctions.GenerateCollisionMesh(collider, Vector3.zero, 0f/*CollisionMargin*/); //ConvexHullShape hull = new ConvexHullShape(Array.ConvertAll(m.vertices, x => x.ToBullet()), m.vertices.Length); //hull.Margin = CollisionMargin; //hullShape.AddHullShape(hull, BulletSharp.Math.Matrix.Translation(-ComOffset.ToBullet())); MainObject.AddComponent <Wireframe>().Init(collider, ComOffset); ConvexHullShape hull = new ConvexHullShape(Array.ConvertAll(collider.vertices, x => x.ToBullet()), collider.vertices.Length); hull.Margin = CollisionMargin; hullShape.AddShape(hull, BulletSharp.Math.Matrix.Translation(-ComOffset.ToBullet())); } MainObject.AddComponent <MeshRenderer>(); PhysicalProperties = mesh.physics; //Debug.Log(PhysicalProperties.centerOfMass); BRigidBody rigidBody = MainObject.AddComponent <BRigidBody>(); string jsonFile = Directory.GetParent(filePath).FullName + Path.DirectorySeparatorChar + "skeleton.json"; bool useJsonWeight = false; if (File.Exists(jsonFile)) { useJsonWeight = true; } float weight = mesh.physics.mass; if (useJsonWeight) { try { weight = (float)GetSkeletalJoint().weight; } catch (Exception e) { weight = mesh.physics.mass; } } rigidBody.mass = weight; rigidBody.friction = 0.25f; rigidBody.linearSleepingThreshold = LinearSleepingThreshold; rigidBody.angularSleepingThreshold = AngularSleepingThreshold; rigidBody.RemoveOnCollisionCallbackEventHandler(); foreach (BRigidBody rb in MainObject.transform.parent.GetComponentsInChildren <BRigidBody>()) { rigidBody.GetCollisionObject().SetIgnoreCollisionCheck(rb.GetCollisionObject(), true); } MainState mainState = StateMachine.SceneGlobal.FindState <MainState>(); if (mainState != null) { MainObject.AddComponent <BMultiCallbacks>().AddCallback(mainState.CollisionTracker); } } if (this.HasDriverMeta <WheelDriverMeta>() && this.GetDriverMeta <WheelDriverMeta>().type != WheelType.NOT_A_WHEEL && GetParent() == null) { BRigidBody rigidBody = MainObject.GetComponent <BRigidBody>(); if (isMixAndMatch) { rigidBody.mass += wheelMass; } rigidBody.GetCollisionObject().CollisionShape.CalculateLocalInertia(rigidBody.mass); } #region Free mesh foreach (var list in new List <BXDAMesh.BXDASubMesh>[] { mesh.meshes, mesh.colliders }) { foreach (BXDAMesh.BXDASubMesh sub in list) { sub.verts = null; sub.norms = null; foreach (BXDAMesh.BXDASurface surf in sub.surfaces) { surf.indicies = null; } } for (int i = 0; i < list.Count; i++) { list[i] = null; } } mesh = null; GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); #endregion return(true); }
public void CreateJoint(RobotBase robotBase, float wheelFriction = 1f, float lateralFriction = 1f) { if (joint != null || GetSkeletalJoint() == null) { return; } switch (GetSkeletalJoint().GetJointType()) { case SkeletalJointType.ROTATIONAL: if (this.HasDriverMeta <WheelDriverMeta>() && this.GetDriverMeta <WheelDriverMeta>().type != WheelType.NOT_A_WHEEL) { RigidNode parent = (RigidNode)GetParent(); if (parent.MainObject.GetComponent <BRaycastRobot>() == null) { parent.MainObject.AddComponent <BRaycastRobot>().RootNode = RootNode; } WheelType wheelType = this.GetDriverMeta <WheelDriverMeta>().type; BRaycastWheel wheel = MainObject.AddComponent <BRaycastWheel>(); wheel.CreateWheel(this); if (robotBase is MaMRobot) { wheel.Friction = wheelFriction; wheel.SlidingFriction = lateralFriction; } MainObject.transform.parent = parent.MainObject.transform; } else { RotationalJoint_Base rNode = (RotationalJoint_Base)GetSkeletalJoint(); BHingedConstraintEx hc = (BHingedConstraintEx)(joint = ConfigJoint <BHingedConstraintEx>(rNode.basePoint.AsV3() - ComOffset, rNode.axis.AsV3(), AxisType.X)); Vector3 rAxis = rNode.axis.AsV3().normalized; hc.axisInA = rAxis; hc.axisInB = rAxis; if (hc.setLimit = rNode.hasAngularLimit) { hc.lowLimitAngleRadians = rNode.currentAngularPosition - rNode.angularLimitHigh; hc.highLimitAngleRadians = rNode.currentAngularPosition - rNode.angularLimitLow; } hc.constraintType = BTypedConstraint.ConstraintType.constrainToAnotherBody; } break; case SkeletalJointType.CYLINDRICAL: CylindricalJoint_Base cNode = (CylindricalJoint_Base)GetSkeletalJoint(); Vector3 cAxis = cNode.axis.AsV3().normalized; B6DOFConstraint bc = (B6DOFConstraint)(joint = ConfigJoint <B6DOFConstraint>(cNode.basePoint.AsV3() - ComOffset, cAxis, AxisType.X)); bc.localConstraintAxisX = cAxis; bc.localConstraintAxisY = new Vector3(cAxis.y, cAxis.z, cAxis.x); bc.linearLimitLower = new Vector3((cNode.linearLimitStart - cNode.currentLinearPosition) * 0.01f, 0f, 0f); bc.linearLimitUpper = new Vector3((cNode.linearLimitEnd - cNode.currentLinearPosition) * 0.01f, 0f, 0f); // TODO: Implement angular cylinder limits bc.constraintType = BTypedConstraint.ConstraintType.constrainToAnotherBody; break; case SkeletalJointType.LINEAR: LinearJoint_Base lNode = (LinearJoint_Base)GetSkeletalJoint(); Vector3 lAxis = lNode.axis.AsV3().normalized; BSliderConstraint sc = (BSliderConstraint)(joint = ConfigJoint <BSliderConstraint>(lNode.basePoint.AsV3() - ComOffset, lAxis, AxisType.X)); sc.localConstraintAxisX = lAxis; sc.localConstraintAxisY = new Vector3(lAxis.y, lAxis.z, lAxis.x); sc.lowerLinearLimit = (lNode.linearLimitLow - lNode.currentLinearPosition) * 0.01f; sc.upperLinearLimit = (lNode.linearLimitHigh - lNode.currentLinearPosition) * 0.01f; sc.lowerAngularLimitRadians = 0f; sc.upperAngularLimitRadians = 0f; sc.constraintType = BTypedConstraint.ConstraintType.constrainToAnotherBody; bool b = this.HasDriverMeta <ElevatorDriverMeta>(); if (GetSkeletalJoint().cDriver != null) { if (GetSkeletalJoint().cDriver.GetDriveType().IsElevator()) { MainObject.GetComponent <BRigidBody>().mass *= 2f; } } break; } }
public bool CreateMesh(string filePath) { Debug.Log(filePath); BXDAMesh mesh = new BXDAMesh(); mesh.ReadFromFile(filePath); //if (!mesh.GUID.Equals(GUID)) //{ // Debug.Log("Returning false"); // return false; //} List <GameObject> meshObjects = new List <GameObject>(); AuxFunctions.ReadMeshSet(mesh.meshes, delegate(int id, BXDAMesh.BXDASubMesh sub, Mesh meshu) { GameObject meshObject = new GameObject(MainObject.name + "_mesh"); meshObjects.Add(meshObject); meshObject.AddComponent <MeshFilter>().mesh = meshu; meshObject.AddComponent <MeshRenderer>(); Material[] materials = new Material[meshu.subMeshCount]; for (int i = 0; i < materials.Length; i++) { materials[i] = sub.surfaces[i].AsMaterial(true); } meshObject.GetComponent <MeshRenderer>().materials = materials; meshObject.transform.position = root.position; meshObject.transform.rotation = root.rotation; Debug.Log("Mesh Objects count " + meshObjects.Count); }, true); Vector3 com = mesh.physics.centerOfMass.AsV3(); com.x *= -1; ComOffset = com; Mesh[] colliders = new Mesh[mesh.colliders.Count]; AuxFunctions.ReadMeshSet(mesh.colliders, delegate(int id, BXDAMesh.BXDASubMesh sub, Mesh meshu) { colliders[id] = meshu; }, true); MainObject.transform.position = root.position + ComOffset; MainObject.transform.rotation = root.rotation; foreach (GameObject meshObject in meshObjects) { meshObject.transform.parent = MainObject.transform; } if (!this.HasDriverMeta <WheelDriverMeta>() || this.GetDriverMeta <WheelDriverMeta>().type == WheelType.NOT_A_WHEEL) { BMultiHullShape hullShape = MainObject.AddComponent <BMultiHullShape>(); foreach (Mesh collider in colliders) { ConvexHullShape hull = new ConvexHullShape(Array.ConvertAll(collider.vertices, x => x.ToBullet()), collider.vertices.Length); hull.Margin = 0f; hullShape.AddHullShape(hull, BulletSharp.Math.Matrix.Translation(-ComOffset.ToBullet())); } MainObject.AddComponent <MeshRenderer>(); PhysicalProperties = mesh.physics; Debug.Log(PhysicalProperties.centerOfMass); BRigidBody rigidBody = MainObject.AddComponent <BRigidBody>(); rigidBody.mass = mesh.physics.mass; rigidBody.friction = 0.25f; rigidBody.RemoveOnCollisionCallbackEventHandler(); ((RigidBody)rigidBody.GetCollisionObject()).ActivationState = ActivationState.DisableDeactivation; foreach (BRigidBody rb in MainObject.transform.parent.GetComponentsInChildren <BRigidBody>()) { rigidBody.GetCollisionObject().SetIgnoreCollisionCheck(rb.GetCollisionObject(), true); } MainObject.AddComponent <BMultiCallbacks>().AddCallback((StateMachine.Instance.CurrentState as MainState).CollisionTracker); } if (this.HasDriverMeta <WheelDriverMeta>() && this.GetDriverMeta <WheelDriverMeta>().type != WheelType.NOT_A_WHEEL && GetParent() == null) { BRigidBody rigidBody = MainObject.GetComponent <BRigidBody>(); if (MixAndMatchMode.isMixAndMatchMode) { rigidBody.mass += PlayerPrefs.GetFloat("wheelMass", 1f); } rigidBody.GetCollisionObject().CollisionShape.CalculateLocalInertia(rigidBody.mass); } #region Free mesh foreach (var list in new List <BXDAMesh.BXDASubMesh>[] { mesh.meshes, mesh.colliders }) { foreach (BXDAMesh.BXDASubMesh sub in list) { sub.verts = null; sub.norms = null; foreach (BXDAMesh.BXDASurface surf in sub.surfaces) { surf.indicies = null; } } for (int i = 0; i < list.Count; i++) { list[i] = null; } } mesh = null; GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); #endregion return(true); }
public void CreateJoint(int numWheels) { if (joint != null || GetSkeletalJoint() == null) { return; } switch (GetSkeletalJoint().GetJointType()) { case SkeletalJointType.ROTATIONAL: if (this.HasDriverMeta <WheelDriverMeta>() && this.GetDriverMeta <WheelDriverMeta>().type != WheelType.NOT_A_WHEEL) { RigidNode parent = (RigidNode)GetParent(); if (parent.MainObject.GetComponent <BRaycastRobot>() == null) { BRaycastRobot robot = parent.MainObject.AddComponent <BRaycastRobot>(); robot.NumWheels = numWheels; if (MixAndMatchMode.isMixAndMatchMode) { robot.Friction = PlayerPrefs.GetFloat("wheelFriction", 1); } } WheelType wheelType = this.GetDriverMeta <WheelDriverMeta>().type; MainObject.AddComponent <BRaycastWheel>().CreateWheel(this); MainObject.transform.parent = parent.MainObject.transform; } else { RotationalJoint_Base rNode = (RotationalJoint_Base)GetSkeletalJoint(); BHingedConstraintEx hc = (BHingedConstraintEx)(joint = ConfigJoint <BHingedConstraintEx>(rNode.basePoint.AsV3() - ComOffset, rNode.axis.AsV3(), AxisType.X)); Vector3 rAxis = rNode.axis.AsV3().normalized; hc.axisInA = rAxis; hc.axisInB = rAxis; if (hc.setLimit = rNode.hasAngularLimit) { hc.lowLimitAngleRadians = rNode.currentAngularPosition - rNode.angularLimitHigh; hc.highLimitAngleRadians = rNode.currentAngularPosition - rNode.angularLimitLow; } hc.constraintType = BTypedConstraint.ConstraintType.constrainToAnotherBody; } break; case SkeletalJointType.CYLINDRICAL: CylindricalJoint_Base cNode = (CylindricalJoint_Base)GetSkeletalJoint(); B6DOFConstraint bc = (B6DOFConstraint)(joint = ConfigJoint <B6DOFConstraint>(cNode.basePoint.AsV3() - ComOffset, cNode.axis.AsV3(), AxisType.X)); bc.linearLimitLower = new Vector3(cNode.linearLimitStart * 0.01f, 0f, 0f); bc.linearLimitUpper = new Vector3(cNode.linearLimitEnd * 0.01f, 0f, 0f); bc.constraintType = BTypedConstraint.ConstraintType.constrainToAnotherBody; break; case SkeletalJointType.LINEAR: LinearJoint_Base lNode = (LinearJoint_Base)GetSkeletalJoint(); Vector3 lAxis = lNode.axis.AsV3().normalized; // TODO: Figure out how to make a vertical slider? BSliderConstraint sc = (BSliderConstraint)(joint = ConfigJoint <BSliderConstraint>(lNode.basePoint.AsV3() - ComOffset, lNode.axis.AsV3(), AxisType.X)); if (lAxis.x < 0) { lAxis.x *= -1f; } if (lAxis.y < 0) { lAxis.y *= -1f; } if (lAxis.z < 0) { lAxis.z *= -1f; } sc.localConstraintAxisX = lAxis; sc.localConstraintAxisY = new Vector3(lAxis.y, lAxis.z, lAxis.x); sc.lowerLinearLimit = lNode.linearLimitLow * 0.01f; sc.upperLinearLimit = lNode.linearLimitHigh * 0.01f; sc.lowerAngularLimitRadians = 0f; sc.upperAngularLimitRadians = 0f; sc.constraintType = BTypedConstraint.ConstraintType.constrainToAnotherBody; bool b = this.HasDriverMeta <ElevatorDriverMeta>(); if (GetSkeletalJoint().cDriver != null) { if (GetSkeletalJoint().cDriver.GetDriveType().IsElevator()) { MainObject.GetComponent <BRigidBody>().mass *= 2f; } } break; } }
public bool CreateMesh(string filePath) { BXDAMesh mesh = new BXDAMesh(); mesh.ReadFromFile(filePath); if (!mesh.GUID.Equals(GUID)) return false; List<GameObject> meshObjects = new List<GameObject>(); AuxFunctions.ReadMeshSet(mesh.meshes, delegate (int id, BXDAMesh.BXDASubMesh sub, Mesh meshu) { GameObject meshObject = new GameObject(MainObject.name + "_mesh"); meshObjects.Add(meshObject); meshObject.AddComponent<MeshFilter>().mesh = meshu; meshObject.AddComponent<MeshRenderer>(); Material[] materials = new Material[meshu.subMeshCount]; for (int i = 0; i < materials.Length; i++) materials[i] = sub.surfaces[i].AsMaterial(true); meshObject.GetComponent<MeshRenderer>().materials = materials; meshObject.transform.position = root.position; meshObject.transform.rotation = root.rotation; ComOffset = meshObject.transform.GetComponent<MeshFilter>().mesh.bounds.center; }); Mesh[] colliders = new Mesh[mesh.colliders.Count]; AuxFunctions.ReadMeshSet(mesh.colliders, delegate (int id, BXDAMesh.BXDASubMesh sub, Mesh meshu) { colliders[id] = meshu; }); MainObject.transform.position = root.position + ComOffset; MainObject.transform.rotation = root.rotation; foreach (GameObject meshObject in meshObjects) meshObject.transform.parent = MainObject.transform; if (this.HasDriverMeta<WheelDriverMeta>()) { CreateWheel(); } else { BMultiHullShape hullShape = MainObject.AddComponent<BMultiHullShape>(); foreach (Mesh collider in colliders) { ConvexHullShape hull = new ConvexHullShape(Array.ConvertAll(collider.vertices, x => x.ToBullet()), collider.vertices.Length); hull.Margin = 0f; hullShape.AddHullShape(hull, BulletSharp.Math.Matrix.Translation(-ComOffset.ToBullet())); } } physicalProperties = mesh.physics; BRigidBody rigidBody = MainObject.AddComponent<BRigidBody>(); rigidBody.mass = mesh.physics.mass; rigidBody.friction = 1f; if (this.HasDriverMeta<WheelDriverMeta>()) UpdateWheelRigidBody(); foreach (BRigidBody rb in MainObject.transform.parent.GetComponentsInChildren<BRigidBody>()) { rigidBody.GetCollisionObject().SetIgnoreCollisionCheck(rb.GetCollisionObject(), true); } if (this.HasDriverMeta<WheelDriverMeta>()) UpdateWheelMass(); // 'tis a wheel, so needs more mass for joints to work correctly. #region Free mesh foreach (var list in new List<BXDAMesh.BXDASubMesh>[] { mesh.meshes, mesh.colliders }) { foreach (BXDAMesh.BXDASubMesh sub in list) { sub.verts = null; sub.norms = null; foreach (BXDAMesh.BXDASurface surf in sub.surfaces) { surf.indicies = null; } } for (int i = 0; i < list.Count; i++) { list[i] = null; } } mesh = null; GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); #endregion return true; }