public void CheckBorders(CubeBehaviour box) { var dx = box.transform.position.x - transform.position.x; var dy = box.transform.position.y - transform.position.y; var dz = box.transform.position.z - transform.position.z; if (Mathf.Abs(dx) > Mathf.Abs(dy) && Mathf.Abs(dx) > Mathf.Abs(dz)) // dx is the biggest { if (dx < 0) { transform.position = new Vector3(box.max.x + 0.5f, transform.position.y, transform.position.z); } else { transform.position = new Vector3(box.min.x - 0.5f, transform.position.y, transform.position.z); } } else if (Mathf.Abs(dy) > Mathf.Abs(dx) && Mathf.Abs(dy) > Mathf.Abs(dz)) // dy is the biggest { } else // dz is the biggest { if (dz < 0) { transform.position = new Vector3(transform.position.x, transform.position.y, box.max.z + 0.5f); } else { transform.position = new Vector3(transform.position.x, transform.position.y, box.min.z - 0.5f); } } }
public static void CheckAABBs(CubeBehaviour a, CubeBehaviour b) { if ((a.min.x <= b.max.x && a.max.x >= b.min.x) && (a.min.y <= b.max.y && a.max.y >= b.min.y) && (a.min.z <= b.max.z && a.max.z >= b.min.z)) { if (!a.contacts.Contains(b)) { a.contacts.Add(b); a.isColliding = true; b.isColliding = true; b.speed = a.force; b.direction = a.direction; a.direction *= -1; //a.velocity = Vector3.zero; } } else { if (a.contacts.Contains(b)) { a.contacts.Remove(b); a.isColliding = false; b.isColliding = false; } } }
private void GivenANewCube() { CubeBehaviour prefab = Resources.Load <CubeBehaviour>(prefabPath); cubeBehaviour = GameObject.Instantiate(prefab); meshFilter = cubeBehaviour.GetComponent <MeshFilter>(); }
public static Vector3 GetAABBClosestPoint(SphereBehaviour sphere, CubeBehaviour box) { var points = new List <Vector3> { new Vector3(box.min.x, box.min.y, box.min.z), new Vector3(box.min.x, box.min.y, box.max.z), new Vector3(box.max.x, box.min.y, box.max.z), new Vector3(box.max.x, box.min.y, box.min.z), new Vector3(box.min.x, box.max.y, box.min.z), new Vector3(box.min.x, box.max.y, box.max.z), new Vector3(box.max.x, box.max.y, box.max.z), new Vector3(box.max.x, box.max.y, box.min.z) }; var minDistancePoint = points[0]; var minDistance = Mathf.Infinity; foreach (var point in points) { var dist = (point - sphere.transform.position).magnitude; if (!(dist < minDistance)) { continue; } minDistance = dist; minDistancePoint = point; } return(minDistancePoint); }
public static void CheckAABBs(CubeBehaviour a, CubeBehaviour b) { if ((a.min.x <= b.max.x && a.max.x >= b.min.x) && (a.min.y <= b.max.y && a.max.y >= b.min.y) && (a.min.z <= b.max.z && a.max.z >= b.min.z)) { if (a.contacts.Contains(b)) { return; } a.contacts.Add(b); a.isColliding = true; } else { if (!a.contacts.Contains(b)) { return; } a.contacts.Remove(b); a.isColliding = false; } }
public static void CheckAABBSphere(SphereBehavior a, CubeBehaviour b) { var x = Mathf.Max(b.min.x, Mathf.Min(a.transform.position.x, b.max.x)); var y = Mathf.Max(b.min.y, Mathf.Min(a.transform.position.y, b.max.y)); var z = Mathf.Max(b.min.z, Mathf.Min(a.transform.position.z, b.max.z)); var distance = Mathf.Sqrt((x - a.transform.position.x) * (x - a.transform.position.x) + (y - a.transform.position.y) * (y - a.transform.position.y) + (z - a.transform.position.z) * (x - a.transform.position.z)); if (distance < a.size.x / 2) { if (!a.contacts.Contains(b)) { a.contacts.Add(b); a.isColliding = true; Debug.Log("collision "); if (b.isGround) { a.direction.Set(a.direction.x, -a.direction.y, a.direction.z); } else { a.direction = (((a.mass - b.mass) * a.velocity) / (a.mass + b.mass)) + ((2 * b.mass * b.velocity) / (a.mass + b.mass)); a.speed = a.direction.magnitude; if (a.speed != 0) { a.direction.Set(a.direction.x / a.direction.magnitude, a.direction.y / a.direction.magnitude, a.direction.z / a.direction.magnitude); } //a.collisionNormal.x = (a.transform.position.x - x); //a.collisionNormal.y = (a.transform.position.x - y); //a.collisionNormal.z = (a.transform.position.x - z); //if (a.collisionNormal.magnitude != 0) // a.collisionNormal.Set(a.collisionNormal.x / a.collisionNormal.magnitude, a.collisionNormal.y / a.collisionNormal.magnitude, a.collisionNormal.z / a.collisionNormal.magnitude); //a.relativeVelocity.Set(b.velocity.x - a.velocity.x, b.velocity.y - a.velocity.y, b.velocity.z - a.velocity.z); //a.collisionRestitution = Mathf.Min(a.coefficientOfRestitution, b.coefficientOfRestitution); //a.impulse = ((1 + a.collisionRestitution) * (Vector3.Dot(a.relativeVelocity, a.collisionNormal))) / ((1 / a.mass) + (1 / b.mass)); //a.t = a.relativeVelocity - (Vector3.Dot(a.relativeVelocity, a.collisionNormal) * a.collisionNormal); //a.jt = ((1 + a.collisionRestitution) * (Vector3.Dot(a.relativeVelocity, a.t))) / ((1 / a.mass) + (1 / b.mass)); //float friction = Mathf.Sqrt(b.coFriction * a.coFriction); //a.jt = Mathf.Max(a.jt, -1 * a.impulse * friction); //a.jt = Mathf.Min(a.jt, a.impulse * friction); } } } else { if (a.contacts.Contains(b)) { a.contacts.Remove(b); a.isColliding = false; // a.impulse = 0.0f; } } }
public static void PlayPickUpDiamondSound(this CubeBehaviour target) { if (CurrentUserInfo.IsSoundEnabled) { target.GetComponent <AudioSource>().Play(); } }
// Modeify from CubeAABBs public static void CheckAABBsCubeBullet(BulletBehaviour bullet, CubeBehaviour cube) { Contact contactB = new Contact(cube); if ((bullet.min.x <= cube.max.x && bullet.max.x >= cube.min.x) && (bullet.min.y <= cube.max.y && bullet.max.y >= cube.min.y) && (bullet.min.z <= cube.max.z && bullet.max.z >= cube.min.z)) { float[] distances = { (bullet.max.x - cube.min.x), (cube.max.x - bullet.min.x), (bullet.max.y - cube.min.y), (cube.max.y - bullet.min.y), (bullet.max.z - cube.min.z), (cube.max.z - bullet.min.z), }; float penetration = float.MaxValue; Vector3 face = Vector3.zero; for (int i = 0; i < 6; i++) { if (distances[i] < penetration) { penetration = distances[i]; face = faces[i]; } } Reflect(bullet); bullet.penetration = penetration; bullet.collisionNormal = -face; } }
public static void CheckSphereAABB(BulletBehaviour a, CubeBehaviour b) { float ax = a.transform.position.x; float ay = a.transform.position.y; float az = a.transform.position.z; float x = Mathf.Max(b.min.x, Mathf.Min(ax, b.max.x)); float y = Mathf.Max(b.min.y, Mathf.Min(ay, b.max.y)); float z = Mathf.Max(b.min.z, Mathf.Min(az, b.max.z)); float distance = Mathf.Sqrt((x - ax) * (x - ax) + (y - ay) * (y - ay) + (z - az) * (z - az)); if (distance < a.radius + 0.5f) { if (!a.contacts.Contains(b)) { a.contacts.Add(b); a.isColliding = true; b.gotHit(a); b.hitByBullet = true; a.stop = true; } } else { if (a.contacts.Contains(b)) { a.contacts.Remove(b); a.isColliding = false; } } }
void Fire() { Transform cam = Camera.main.transform; NextFire = Time.time + FireRate; Vector3 rayorigin = cam.position; LazerLine.SetPosition(0, transform.up * -10); RaycastHit hit; if (Physics.Raycast(rayorigin, cam.forward, out hit, FireRange)) { LazerLine.SetPosition(1, hit.point); CubeBehaviour cubectrl = hit.collider.GetComponent <CubeBehaviour>(); if (cubectrl != null) { if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * HitForce); cubectrl.Hit(LazerDamage); } } } else { LazerLine.SetPosition(1, cam.forward * FireRange); } StartCoroutine("LaserFX"); }
public void CalculateMomentum(CubeBehaviour box) { Vector3 lostMomentum = bulletBehaviour.momentum * EnergyLostCoeficient; box.cube.momentum += lostMomentum; box.cube.velocity = box.cube.momentum / box.cube.mass; }
// This helper function reflects the bullet when it hits an AABB face private static void Reflect(BulletBehaviour s, CubeBehaviour c) { // Mass calculations float bulletMass = s.mass; float cubeMass = c.mass; float combinedMass = bulletMass + cubeMass; float prevBulletSpeed = s.speed; Vector3 prevCubeVelocity = c.velocity; if ((s.collisionNormal == Vector3.forward) || (s.collisionNormal == Vector3.back)) { if (!c.isStatic) { c.velocity.z = (((2.0f * bulletMass) / combinedMass) * (prevBulletSpeed * s.direction.z)) + (((c.mass - s.mass) / combinedMass) * prevCubeVelocity.z); } s.direction = new Vector3(s.direction.x, s.direction.y, -s.direction.z); } else if ((s.collisionNormal == Vector3.right) || (s.collisionNormal == Vector3.left)) { if (!c.isStatic) { c.velocity.x = (((2.0f * bulletMass) / combinedMass) * (prevBulletSpeed * s.direction.z)) + (((c.mass - s.mass) / combinedMass) * prevCubeVelocity.x); } s.direction = new Vector3(-s.direction.x, s.direction.y, s.direction.z); } else if ((s.collisionNormal == Vector3.up) || (s.collisionNormal == Vector3.down)) { if (!c.isStatic) { c.velocity.y = (((2.0f * bulletMass) / combinedMass) * (prevBulletSpeed * s.direction.z)) + (((c.mass - s.mass) / combinedMass) * prevCubeVelocity.y); } s.direction = new Vector3(s.direction.x, -s.direction.y, s.direction.z); } s.speed = (((s.mass - c.mass) / combinedMass) * prevBulletSpeed) + (((2.0f * cubeMass) / (combinedMass)) * prevCubeVelocity.magnitude); }
// removes a cube from the list via its index // if 'keepOrder' is true, then the array is reshuffled. public CubeBehaviour RemoveCubeFromList(int index, bool keepOrder = false) { // item not found if (index < 0 || index >= cubes.Length) { return(null); } // gets the cube and switches places with last index CubeBehaviour cb = cubes[index]; cubes[index] = cubes[cubes.Length - 1]; cubes[cubes.Length - 1] = cb; // removes last component Array.Resize <CubeBehaviour>(ref cubes, cubes.Length - 1); // if the order should be kept if (keepOrder) { // moves swapped item back to its original position for (int i = index; i < cubes.Length - 1; i++) { CubeBehaviour temp = cubes[i]; cubes[i] = cubes[i + 1]; cubes[i + 1] = temp; } } return(cb); }
public void SpawnFirstCube() { var cube = SpawnCube(); cube.Init(null, GenerateColor()); _lastCube = cube; _firstCube = cube; }
public static void CheckSphereAABB(BulletBehaviour bullet, CubeBehaviour cube) { // get box closest point to sphere center by clamping var x = Mathf.Max(cube.min.x, Mathf.Min(bullet.transform.position.x, cube.max.x)); var y = Mathf.Max(cube.min.y, Mathf.Min(bullet.transform.position.y, cube.max.y)); var z = Mathf.Max(cube.min.z, Mathf.Min(bullet.transform.position.z, cube.max.z)); var distance = Math.Sqrt((x - bullet.transform.position.x) * (x - bullet.transform.position.x) + (y - bullet.transform.position.y) * (y - bullet.transform.position.y) + (z - bullet.transform.position.z) * (z - bullet.transform.position.z)); if ((distance < bullet.radius) && (!bullet.isColliding)) { // determine the distances between the contact extents float[] distances = { (cube.max.x - bullet.transform.position.x), (bullet.transform.position.x - cube.min.x), (cube.max.y - bullet.transform.position.y), (bullet.transform.position.y - cube.min.y), (cube.max.z - bullet.transform.position.z), (bullet.transform.position.z - cube.min.z) }; float penetration = float.MaxValue; Vector3 face = Vector3.zero; // check each face to see if it is the one that connected for (int i = 0; i < 6; i++) { if (distances[i] < penetration) { // determine the penetration distance penetration = distances[i]; face = faces[i]; } } bullet.penetration = penetration; bullet.collisionNormal = face; if (cube.hitByBullet == false) { cube.direction = new Vector3(0.0f, -1.0f, 0.0f); cube.speed = bullet.force; cube.hitByBullet = true; } else if (cube.isColliding) { cube.direction = bullet.collisionNormal; cube.speed = bullet.force; } bullet.speed *= 0.6f; Reflect(bullet); } }
public void CalculateMomentum(CubeBehaviour box) { Vector3 lostMomentum = cube.momentum * 0.1f; cube.momentum -= lostMomentum; box.cube.momentum += lostMomentum; cube.velocity = cube.momentum / cube.mass; box.cube.velocity = box.cube.momentum / box.cube.mass; }
// Sphere AABB public static void CheckSphereAABB(BulletBehaviour s, CubeBehaviour b) { // if either is set to null, don't continue. if (s == null || b == null) { return; } // get box closest point to sphere center by clamping var x = Mathf.Max(b.min.x, Mathf.Min(s.transform.position.x, b.max.x)); var y = Mathf.Max(b.min.y, Mathf.Min(s.transform.position.y, b.max.y)); var z = Mathf.Max(b.min.z, Mathf.Min(s.transform.position.z, b.max.z)); var distance = Math.Sqrt((x - s.transform.position.x) * (x - s.transform.position.x) + (y - s.transform.position.y) * (y - s.transform.position.y) + (z - s.transform.position.z) * (z - s.transform.position.z)); if ((distance < s.radius) && (!s.isColliding)) { // determine the distances between the contact extents float[] distances = { (b.max.x - s.transform.position.x), (s.transform.position.x - b.min.x), (b.max.y - s.transform.position.y), (s.transform.position.y - b.min.y), (b.max.z - s.transform.position.z), (s.transform.position.z - b.min.z) }; float penetration = float.MaxValue; Vector3 face = Vector3.zero; // check each face to see if it is the one that connected for (int i = 0; i < 6; i++) { if (distances[i] < penetration) { // determine the penetration distance penetration = distances[i]; face = faces[i]; } } s.penetration = penetration; s.collisionNormal = face; //s.isColliding = true; Reflect(s); // collision has happened. Uncomment to keep cubes. b.gameObject.SetActive(false); } }
private void AddNewCubeToStack(Transform cubeTransform)//gelen yeni cube en uste player cubeun altina eklenir { topCubePosition = topCube.position; topCube.position += new Vector3(0, 0.65f, 0); cubeTransform.position = topCubePosition; CubeBehaviour cubeBehaviour = cubeTransform.GetComponent <CubeBehaviour>(); cubeBehaviour.enabled = true; cubeBehaviour.PrepareForMovement(); GameController.Instance.IncreaseScore(); }
public override void OnInspectorGUI() { DrawDefaultInspector(); CubeBehaviour script = (CubeBehaviour)target; if (GUILayout.Button("Feed")) { script.Feed(script.Data); } }
void LeftKdTree() { if (GUI.Button(new Rect(0, 0, 150, 150), "kd item")) { if (_mQueue.Count == 0) { Debug.LogWarning("Queue is empty"); return; } CubeBehaviour tmp = _mQueue.Dequeue() as CubeBehaviour; _mKdList.Add(tmp); } else if (GUI.Button(new Rect(0, 200, 150, 150), "load item")) { if (null != _mSphere || _mArrayData.Length != _mKdList.Count) { Debug.LogWarning("Sphere Alrealdy loaded or kd tree is not finished"); return; } var tmp = new Vector3(Random.Range(-10, 10), Random.Range(-4, 4f), 0f); _mSphere = Instantiate(_mLocalSphere, tmp, Quaternion.identity); } else if (GUI.Button(new Rect(0, 400, 150, 150), "match item")) { if (null == _mSphere) { Debug.LogWarning("please load Sphere first"); return; } float dis = 0f; var item = _mKdList.FindClosest(_mSphere.transform.position, out dis); item.SetChoosen(); } else if (GUI.Button(new Rect(0, 600, 150, 150), "reset")) { if (null != _mSphere) { Destroy(_mSphere); } _mSphere = null; var list = _mKdList.ToList(); foreach (var item in list) { item.ResetItem(); _mQueue.Enqueue(item); } _mKdList.Clear(); } }
// Update is called once per frame void Update() { bullets = FindObjectsOfType <BulletBehaviour>(); // check each AABB with every other AABB in the scene for (int i = 0; i < cubes.Length; i++) { for (int j = 0; j < cubes.Length; j++) { if (i != j) { CheckAABBs(cubes[i], cubes[j]); } } } // Check each sphere against each AABB in the scene foreach (var sphere in bullets) { foreach (var cube in cubes) { if (cube.name != "Player") { CheckCubeAABB(sphere, cube); } } } for (int i = 0; i < boxes.Length; i++) { CheckBox_Player(player, boxes[i]); } int count = 0; for (int i = 0; i < boxes.Length; i++) { CubeBehaviour a = player.cube; CubeBehaviour b = boxes[i].cube; if ((a.min.x <= b.max.x && a.max.x >= b.min.x) && (a.min.y <= b.max.y && a.max.y >= b.min.y) && (a.min.z <= b.max.z && a.max.z >= b.min.z)) { if (boxes[i].body.bodyType == BodyType.STATIC) { count++; } } } if (count == 0) { player.blocked = false; } }
public static void CheckCubeBulletAABB(BulletBehaviour s, CubeBehaviour b) { if ((s.min.x <= b.max.x && s.max.x >= b.min.x) && (s.min.y <= b.max.y && s.max.y >= b.min.y) && (s.min.z <= b.max.z && s.max.z >= b.min.z)) { if (!s.contacts.Contains(b)) { s.contacts.Add(b); s.isColliding = true; // determine the distances between the contact extents float[] distances = { (b.max.x - s.min.x), (s.max.x - b.min.x), (b.max.y - s.min.y), (s.max.y - b.min.y), (b.max.z - s.min.z), (s.max.z - b.min.z) }; float penetration = float.MaxValue; Vector3 face = Vector3.zero; // check each face to see if it is the one that connected for (int i = 0; i < 6; i++) { if (distances[i] < penetration) { // determine the penetration distance penetration = distances[i]; face = faces[i]; } s.penetration = penetration; s.collisionNormal = face * -1.0f; //s.isColliding = true; Reflect(s); } } } else { if (s.contacts.Contains(b)) { s.contacts.Remove(b); s.isColliding = false; } } }
public static void CheckAABBSphere1(SphereBehavior a, CubeBehaviour b) { var x = Mathf.Max(b.min.x, Mathf.Min(a.transform.position.x, b.max.x)); var y = Mathf.Max(b.min.y, Mathf.Min(a.transform.position.y, b.max.y)); var z = Mathf.Max(b.min.z, Mathf.Min(a.transform.position.z, b.max.z)); var distance = Mathf.Sqrt((x - a.transform.position.x) * (x - a.transform.position.x) + (y - a.transform.position.y) * (y - a.transform.position.y) + (z - a.transform.position.z) * (x - a.transform.position.z)); // Debug.Log("Collider Center : " + a.transform.position.x); if (distance < a.size.x / 2) { if (!b.sp_contacts.Contains(a)) { b.direction = (((b.mass - a.mass) * b.velocity) / (b.mass + a.mass)) + ((2 * a.mass * a.velocity) / (b.mass + a.mass)); b.speed = b.direction.magnitude; if (a.speed != 0) { b.direction.Set(b.direction.x / b.direction.magnitude, b.direction.y / b.direction.magnitude, b.direction.z / b.direction.magnitude); } b.sp_contacts.Add(a); b.isColliding = true; //b.collisionNormal.x = (x - a.transform.position.x); //b.collisionNormal.y = (y - a.transform.position.y); //b.collisionNormal.z = (z - a.transform.position.z); //if (b.collisionNormal.magnitude != 0) // b.collisionNormal.Set(b.collisionNormal.x / b.collisionNormal.magnitude, b.collisionNormal.y / b.collisionNormal.magnitude, b.collisionNormal.z / b.collisionNormal.magnitude); //b.relativeVelocity.Set(a.velocity.x - b.velocity.x, a.velocity.y - b.velocity.y, a.velocity.z - b.velocity.z); //b.collisionRestitution = Mathf.Min(b.coefficientOfRestitution, a.coefficientOfRestitution); //b.t = b.relativeVelocity - (Vector3.Dot(b.relativeVelocity, b.collisionNormal) * b.collisionNormal); //b.impulse = ((1 + b.collisionRestitution) * (Vector3.Dot(b.relativeVelocity, b.collisionNormal))) / ((1 / a.mass) + (1 / b.mass)); //b.jt = ((1 + b.collisionRestitution) * (Vector3.Dot(b.relativeVelocity, b.t))) / ((1 / a.mass) + (1 / b.mass)); //float friction = Mathf.Sqrt(b.coFriction * a.coFriction); //b.jt = Mathf.Max(b.jt, -1 * b.impulse * friction); //b.jt = Mathf.Min(b.jt, b.impulse * friction); } } else { if (b.sp_contacts.Contains(a)) { b.sp_contacts.Remove(a); b.isColliding = false; // b.impulse = 0.0f; } } }
public override void OnInspectorGUI() { CubeBehaviour myTarget = (CubeBehaviour)target; base.OnInspectorGUI(); myTarget.maxDamage = EditorGUILayout.FloatField("Max Damage", myTarget.maxDamage); myTarget.maxDistanceToDamage = EditorGUILayout.FloatField("Max Distance To Damage", myTarget.maxDistanceToDamage); float maxRange = myTarget.maxDamage / myTarget.maxDistanceToDamage; myTarget.distanceMultiplier = EditorGUILayout.Slider("Distance Multiplier", myTarget.distanceMultiplier, 0, maxRange); serializedObject.Update(); serializedObject.ApplyModifiedProperties(); }
private static void Push(CubeBehaviour a, CubeBehaviour b) { if (a.name == "Player") { RigidBody3D playerRB = a.gameObject.GetComponent <RigidBody3D>(); if (a.isColliding) { Vector3 pushVel = new Vector3(playerRB.velocity.x, 0.0f, playerRB.velocity.z) * 60f; b.gameObject.GetComponent <RigidBody3D>().velocity.y = 0; b.gameObject.GetComponent <RigidBody3D>().transform.position += pushVel; } } }
private void PlayerVsStatic(CubeBehaviour staticObject, CubeBehaviour player) { Contact contactPlayerToStatic = staticObject.contacts.Find(x => x.cube.gameObject.name == player.gameObject.name); player.transform.position += (contactPlayerToStatic.penetration) * (contactPlayerToStatic.face); for (int i = 0; i < 3; ++i) { if (contactPlayerToStatic.face[i] * player.rigidBody.velocity[i] < 0) { player.rigidBody.velocity[i] = 0; } } }
private void DynamicVsStatic(CubeBehaviour dynamicObject, CubeBehaviour staticObject) { Vector3 directionDynamicVelocity = dynamicObject.rigidBody.velocity.normalized; Contact contactDynamicToStatic = staticObject.contacts.Find(x => x.cube.gameObject.name == dynamicObject.gameObject.name); float dotProduct = Vector3.Dot(contactDynamicToStatic.face, -directionDynamicVelocity); if (Mathf.Abs(dotProduct) > 0.001f) { dynamicObject.transform.position += -directionDynamicVelocity * ((contactDynamicToStatic.penetration) / dotProduct); } dynamicObject.rigidBody.velocity = Vector3.Reflect(dynamicObject.rigidBody.velocity, contactDynamicToStatic.face) * 0.5f; }
private void PlayerVsDynamic(CubeBehaviour dynamicObject, CubeBehaviour player) { Vector3 directionPlayerToDynamic = (dynamicObject.transform.position - player.transform.position).normalized; Contact contactDynamicToPlayer = player.contacts.Find(x => x.cube.gameObject.name == dynamicObject.gameObject.name); float dotProduct = Vector3.Dot(contactDynamicToPlayer.face, directionPlayerToDynamic); if (Mathf.Abs(dotProduct) > 0.001f) { Vector3 dynamicDeltaPosition = directionPlayerToDynamic * (contactDynamicToPlayer.penetration / dotProduct); dynamicDeltaPosition.y = 0; dynamicObject.transform.position += dynamicDeltaPosition; } }
public static float AABBToSphereDistanceSq(SphereBehaviour sphere, CubeBehaviour box) { var dx = Mathf.Max(box.min.x - sphere.transform.position.x, 0); dx = Mathf.Max(dx, sphere.transform.position.x - box.max.x); var dy = Mathf.Max(box.min.y - sphere.transform.position.y, 0); dy = Mathf.Max(dy, sphere.transform.position.y - box.max.y); var dz = Mathf.Max(box.min.z - sphere.transform.position.z, 0); dz = Mathf.Max(dz, sphere.transform.position.z - box.max.z); return(dx * dx + dy * dy + dz * dz); }
//public static void CheckCubeAABB(BulletBehaviour ab, CubeBehaviour a, CubeBehaviour b) public static void CheckCubeAABB(BulletBehaviour ab, CubeBehaviour b) { //var a = new CubeBehaviour(); //a.transform.position = ab.transform.position; var amin = Vector3.Scale(new Vector3(0.0f, 0.0f, 0.0f), ab.transform.localScale) + ab.transform.position; var amax = Vector3.Scale(new Vector3(ab.radius, ab.radius, ab.radius) * 2.0f, ab.transform.localScale) + ab.transform.position; //a.transform.SetParent(ab.transform); //a.transform.position = ab.transform.position; //a.transform.SetParent(ab.transform); Contact contactB = new Contact(b); if ((amin.x <= b.max.x && amax.x >= b.min.x) && (amin.y <= b.max.y && amax.y >= b.min.y) && (amin.z <= b.max.z && amax.z >= b.min.z)) { // determine the distances between the contact extents float[] distances = { (b.max.x - amin.x), (amax.x - b.min.x), (b.max.y - amin.y), (amax.y - b.min.y), (b.max.z - amin.z), (amax.z - b.min.z) }; float penetration = float.MaxValue; Vector3 face = Vector3.zero; // check each face to see if it is the one that connected for (int i = 0; i < 6; i++) { if (distances[i] < penetration) { // determine the penetration distance penetration = distances[i]; face = faces[i]; } } // set the contact properties contactB.face = face; contactB.penetration = penetration; ReflectCube(ab, face); } }
// Use this for initialization void Start() { cube = GetComponents<CubeBehaviour>()[0]; }