public float penetration; // the penetration of body A into body B #endregion Fields #region Constructors /// <summary> /// Constructor to set member properties /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="normal"></param> /// <param name="p"></param> public MyCollision(MyRigidBody a, MyRigidBody b, Vector3 normal, float p) { bodyA = a; bodyB = b; collisionNormal = normal; penetration = p; }
/// <summary> /// Check to see if a collision has occured with any other rigid bodies /// </summary> /// <param name="body"></param> /// <returns></returns> public override MyCollision CheckCollision(MyRigidBody other) { // declare null MyCollision for return MyCollision collision = null; // check for collision with other spheres if(other is MySphereRigidBody && other.name != name) { // combined radius of both spheres float combinedRadius = this.radius + ((MySphereRigidBody)other).radius; // the vector between the centres of the spheres Vector3 direction = other.transform.position - transform.position; // distance between centres float centreDistance = direction.magnitude; // normalise the direction direction /= centreDistance; // check interection if(centreDistance < combinedRadius) { // create a new collision object, containing the vector of the normal and the distance from the sphere collision = new MyCollision(this, other, direction, centreDistance); } } else if(other is MyPlaneRigidBody) //check for collision with a plane { // get the mesh filter for the plane MeshFilter filter = other.gameObject.GetComponent<MeshFilter>(); // make sure it has a mesh filter if (filter && filter.mesh.normals.Length > 0) { // get one of the vertext normals --- first one should do as the are all the same var normal = filter.transform.TransformDirection(filter.mesh.normals[0]); // distance of sphere centre from plane float distanceFromSphereCentre = Mathf.Abs(Vector3.Dot(normal, transform.position)) + (other as MyPlaneRigidBody).distance; // distance of sphere from plane float distanceFromSphere = distanceFromSphereCentre - radius; // check for intersection if (distanceFromSphere < 0) { // create a new collision object, containing the vector of the normal and the distance from the plane collision = new MyCollision(this, other, normal, distanceFromSphere); } } } // return the collision return collision; }
public void MoveLeft() { if (!IsDeath) { if (LookRight) { LookRight = false; } MyRigidBody.MovePosition(transform.position + Vector3.left * Speed * Time.deltaTime); } }
public void MoveRight() { if (!IsDeath) { if (!LookRight) { LookRight = true; } MyRigidBody.MovePosition(transform.position + Vector3.right * Speed * Time.deltaTime); } }
public IEnumerator Knockback(float knockDur, float knockbackPwr, Vector3 knockbackDir) { knockbackDir.y = 1; float timer = 0; while (knockDur > timer) { timer += Time.deltaTime; MyRigidBody.AddForce(new Vector3(knockbackDir.x * 0, knockbackDir.y * -100, transform.position.z)); } yield return(0); }
public void ImpartMomentum(MyRigidBody otherbody, Vector3 ContactPoint) { //Calculate momentums of both rigid bodies Vector3 otherMomentum = otherbody.GetMomentumAtPoint(ContactPoint); Vector3 thisMomentum = GetMomentumAtPoint(ContactPoint); //Sum the momentums Vector3 SummedMomentum = thisMomentum + otherMomentum; //Apply the new momentum onto both objects ApplyMomentum(SummedMomentum); otherbody.ApplyMomentum(SummedMomentum); }
/// <summary> /// Called when rigid body enters sensor. /// </summary> /// <param name="rbo">Rigid body that entered.</param> public void OnEnter(MySensor sensor, MyRigidBody rbo, MyRBElement rbElement) { //smallship var userData = rbo.m_UserData; var physicsBody = userData as MyPhysicsBody; if (physicsBody != null && physicsBody.Entity is MySmallShip) { m_counter++; if (m_counter > 0) { m_owner.OrderToOpen(); } } }
public void OnLeave(MySensor sensor, MyRigidBody rbo, MyRBElement rbElement) { if (rbo == null) { return; } if (m_isOn && rbo.m_UserData != null) { MyEntity entity = (rbo.m_UserData as MyPhysicsBody).Entity; if (entity != null && (Parent == null || Parent != entity)) { RemoveEntityFromDetectedAndObservable(entity); } } }
/// <summary> /// Checks for collisions with other bodies /// </summary> /// <param name="other"></param> /// <returns></returns> public MyCollision CheckCollision(MyRigidBody other) { // no collision by default MyCollision collision = null; // only check for collisions on bodies that we are interested in switch (this.RigidBodyType) { case global::RigidBodyType.SOLID_SPHERE: case global::RigidBodyType.HOLLOW_SPHERE: case global::RigidBodyType.SOLID_BOX: collision = Collided(this, other); break; default: break; } return collision; }
private void HandleMovement(float horizontal) { if (MyRigidBody.velocity.y < 0) { myAnim.SetBool("land", true); } MyRigidBody.velocity = new Vector2(horizontal * movementSpeed, MyRigidBody.velocity.y); if (Jump && MyRigidBody.velocity.y == 0) { MyRigidBody.AddForce(new Vector2(0, jumpForce)); jumpSound.Play(); } myAnim.SetFloat("speed", Mathf.Abs(horizontal)); }
private void LookAtMouse() { if (!_cameraTransform) { return; } Camera cam = _cameraTransform.GetComponent <Camera>(); Ray ray = cam.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, cameraLength, _floorMask)) { Vector3 playerToMouseDirection = hit.point - MyTransform.position; playerToMouseDirection.y = 0f; Quaternion newRotation = Quaternion.LookRotation(playerToMouseDirection); MyRigidBody.MoveRotation(newRotation); } }
/// <summary> /// Called when rigid body leaves sensor. /// </summary> /// <param name="rbo">Rigid body that left.</param> public void OnLeave(MySensor sensor, MyRigidBody rbo, MyRBElement rbElement) { ////TODO: Temporary solution - there must not be rbo==null, fix the error and change to assert //if (rbo == null) // return; Debug.Assert(rbo != null); //smallship var userData = rbo.m_UserData; var physicsBody = userData as MyPhysicsBody; if (physicsBody != null && physicsBody.Entity is MySmallShip) { m_counter--; if (m_counter <= 0) { m_owner.OrderToClose(); } } }
private void HandleMovement(float horizontal) { if (IsFalling) { gameObject.layer = 10; myAnimator.SetBool("Land", true); } if (!SLIDE && (ONGROUND || airControl)) { MyRigidBody.velocity = new Vector2(horizontal * movementspeed, MyRigidBody.velocity.y); } if (JUMP && MyRigidBody.velocity.y == 0) { MyRigidBody.AddForce(new Vector2(0, jumpforce)); } myAnimator.SetFloat("Speed", Mathf.Abs(horizontal)); //*if (myRigidbody.velocity.y < 0) //{ // myAnimator.SetBool("Land", true); //} //if (isGround && jump) //{ // isGround = false; // myRigidbody.AddForce(new Vector2(0,jumpforce)); // myAnimator.SetTrigger("Jump"); //} //if (!myAnimator.GetBool ("Slide") && (isGround || airControl)) //{ // myRigidbody.velocity = new Vector2 (horizontal * movementspeed, myRigidbody.velocity.y); //} //if (slide && !this.myAnimator.GetCurrentAnimatorStateInfo (0).IsName ("SLIDE")) //{ // myAnimator.SetBool ("Slide", true); //} //else if (!this.myAnimator.GetCurrentAnimatorStateInfo (0).IsName ("SLIDE")) //{ // myAnimator.SetBool("Slide",false); //} //myAnimator.SetFloat ("Speed", Mathf.Abs(horizontal)); }
// Returns the position of the circle such that it is no longer intersecting the line. private Vector3 binarySearchRewind(int circleId, int lineId) { int i = 0; MyRigidBody mrb = myRigidBodies[circleId]; while (colliders[circleId].isColliding(colliders[lineId]) && i < binarySearchIterationMin) { if (colliders[circleId].isColliding(colliders[lineId])) { mrb.updatePosition(-Time.deltaTime / Mathf.Pow(2, i)); } else { mrb.updatePosition(Time.deltaTime / Mathf.Pow(2, i)); } i++; } return(mrb.transform.position); }
private MyCollision Collided(MyRigidBody object1, MyRigidBody object2) { MyCollision collision = null; switch(object2.RigidBodyType) { case global::RigidBodyType.PLANE: collision = CollidedWithPlane(object1, object2 as MyPlaneCollider); break; case global::RigidBodyType.SOLID_SPHERE: case global::RigidBodyType.HOLLOW_SPHERE: collision = CollidedWithSphere(object1, object2 as MySphereCollider); break; case global::RigidBodyType.SOLID_BOX: collision = CollidedWithBox(object1, object2 as MyBoxCollider); break; default: break; } return collision; }
// Calculates the resultant velocity of the collision private Vector3 resultingVelocity(int circleId, int lineId, Vector3 normal) { Vector3 startPos = ((LineCollider)colliders[lineId]).startPos; Vector3 endPos = ((LineCollider)colliders[lineId]).endPos; Vector3 tangent = (endPos - startPos).normalized; MyRigidBody mrb = myRigidBodies[circleId]; Vector3 velocity = mrb.velocity; float vt = Vector3.Dot(velocity, tangent); float vnminus = Vector3.Dot(velocity, normal); float vnplus = vnminus - (1 + MyRigidBody.coefficientOfRestitution) * vnminus; Vector3 vtplusVec = tangent * vt; Vector3 vnplusVec = normal * vnplus; Vector3 vPlus = vtplusVec + vnplusVec; mrb.velocity = vPlus; return(vPlus); }
private void HandleMovement(float horizontal) { if (MyRigidBody.velocity.y < 0) { myAnimator.SetBool("land", true); } if (!Attack && !Slide && (OnGround || airControl)) { MyRigidBody.velocity = new Vector2(horizontal * movementSpeed, MyRigidBody.velocity.y); } if (Jump && MyRigidBody.velocity.y == 0) { MyRigidBody.AddForce(new Vector2(0, jumpForce)); } myAnimator.SetFloat("speed", Mathf.Abs(horizontal)); //if (myRigidBody.velocity.y < 0)// removed for refactoring //{ // myAnimator.SetBool("land", true); //} //if (!myAnimator.GetBool("slide") && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attacking")) //{ // myRigidBody.velocity = new Vector2(horizontal * movementSpeed, myRigidBody.velocity.y); //} //else if (!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Slide")) //{ // myAnimator.SetBool("slide", false); //} //myAnimator.SetFloat("speed", Mathf.Abs(horizontal)); }
public override MyCollision CheckCollision(MyRigidBody other) { // declare null MyCollision for return MyCollision collision = null; // check for collision with other spheres //if (other is MySphereRigidBody) //{ // // distance of sphere centre from plane // float distanceFromSphereCentre = Mathf.Abs(Vector3.Dot(normal, other.transform.position)) + distance; // // distance of sphere from plane // float distanceFromSphere = distanceFromSphereCentre - (other as MySphereRigidBody).radius; // // check intersection // if (distanceFromSphere < 0) // { // Debug.LogError("Intersected"); // } //} return collision; }
void Start() { Transform = GetComponentInParent <Trans>(); //Find the transform component Rig = GetComponentInParent <MyRigidBody>(); Rig.BallRadius = BallRadius; }
public void MoveOrbit(ORBIT_Mode _om) { MyRigidBody.MovePosition(Vector3.MoveTowards(transform.position, OrbitPoint[OrbitProgressive].position, Speed * Time.deltaTime)); MyRigidBody.MoveRotation(Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(OrbitPoint[OrbitProgressive].rotation), Torque * Time.deltaTime)); if (transform.position == OrbitPoint[OrbitProgressive].position) { if (OrbitPoint[OrbitProgressive].pointEvent != null) { OrbitPoint[OrbitProgressive].pointEvent.Excute(); } if (Move_ModeCurrent == MOVE_Mode.STEP) { SetOn(false); } if (OrbitProgressive == 0 && Reverse) { switch (_om) { case ORBIT_Mode.CIRCLE: OrbitProgressive = OrbitPoint.Count - 1; break; case ORBIT_Mode.REPEAT: SetProgress(OrbitPoint.Count - 1); break; case ORBIT_Mode.PENDULUM: Reverse = !Reverse; break; case ORBIT_Mode.ONEWAY: SetOn(false); break; } } else if (OrbitProgressive == OrbitPoint.Count - 1 && !Reverse) { switch (_om) { case ORBIT_Mode.CIRCLE: OrbitProgressive = 0; break; case ORBIT_Mode.REPEAT: SetProgress(0); break; case ORBIT_Mode.PENDULUM: Reverse = !Reverse; break; case ORBIT_Mode.ONEWAY: SetOn(false); break; } } else { if (Reverse) { OrbitProgressive--; } else { OrbitProgressive++; } } Torque = Quaternion.Angle(transform.rotation, Quaternion.Euler(OrbitPoint[OrbitProgressive].rotation)) / (Vector3.Distance(transform.position, OrbitPoint[OrbitProgressive].position) / Speed); } }
private MyCollision CollidedWithBox(MyRigidBody body, MyBoxCollider plane) { return null; }
/// <summary> /// Check for a collision between this and another rigid body /// This must be implemented on a sub class of specific type /// </summary> /// <param name="body"></param> /// <returns></returns> public abstract MyCollision CheckCollision(MyRigidBody other);