private void StartGame(object sender, GestureEventArgs e) { if (_started) { return; } _started = true; EntityManager.Find <TextBlock>("ToasterPosition").Text = "Start"; Entity toastModel = new Entity("toastModel") .AddComponent(new Transform3D()) .AddComponent(new MaterialsMap(new BasicMaterial(Color.Gray))) .AddComponent(Model.CreateCube()) .AddComponent(new SphereCollider()) .AddComponent(new RigidBody3D() { Mass = 1, EnableContinuousContact = true }) .AddComponent(new ModelRenderer()); EntityManager.Add(toastModel); RigidBody3D rigidBody = toastModel.FindComponent <RigidBody3D>(); rigidBody.ResetPosition(Vector3.Zero); rigidBody.ApplyLinearImpulse(25 * Vector3.UnitY); WaveServices.Input.TouchPanelState.Clear(); EntityManager.Find <FixedCamera>("MainCamera").Entity.AddComponent(new FlightBehavior(toastModel)); //EntityManager.Find<FixedCamera>("MainCamera").Entity.AddComponent(new RigidBody3D() { IsKinematic = true, }); }
protected override void Initialize() { base.Initialize(); this.actionsQueue = new EngineActions[MaxActions]; this.numActions = 0; this.dust = this.Owner.FindComponentInChildren <DustBehavior>(); this.particleActivators = new Dictionary <EngineActions, List <BaseImpulseActivator> >(); this.allParticleActivators = new List <BaseImpulseActivator>(); var components = this.EntityManager.FindComponentsOfType <BaseImpulseActivator>(false); foreach (var particleActivator in components) { if (!this.particleActivators.ContainsKey(particleActivator.Action)) { this.particleActivators[particleActivator.Action] = new List <BaseImpulseActivator>(); } this.particleActivators[particleActivator.Action].Add(particleActivator); this.allParticleActivators.Add(particleActivator); } if (!string.IsNullOrEmpty(this.ShipDeployEntityPath) && !string.IsNullOrEmpty(this.FootFrontPath) && !string.IsNullOrEmpty(this.FootBackPath) && !string.IsNullOrEmpty(this.FootLeftPath) && !string.IsNullOrEmpty(this.FootRightPath)) { var shipDeployEntity = this.Owner.Find(this.ShipDeployEntityPath); var shipDeployTransform = shipDeployEntity.FindComponent <Transform3D>(); this.shipDeployBB = shipDeployEntity.FindComponent <MeshComponent>(false).BoundingBox.Value; this.shipDeployBB.Max = (this.shipDeployBB.Max * shipDeployTransform.Scale) + shipDeployTransform.Position; this.shipDeployBB.Min = (this.shipDeployBB.Min * shipDeployTransform.Scale) + shipDeployTransform.Position; this.footFront = this.Owner.Find(this.FootFrontPath).FindComponent <RigidBody3D>(); this.footBack = this.Owner.Find(this.FootBackPath).FindComponent <RigidBody3D>(); this.footLeft = this.Owner.Find(this.FootLeftPath).FindComponent <RigidBody3D>(); this.footRight = this.Owner.Find(this.FootRightPath).FindComponent <RigidBody3D>(); this.footFront.BeginCollision += FootFrontBody_BeginCollision; this.footFront.EndCollision += FootFrontBody_EndCollision; this.footBack.BeginCollision += FootBackBody_BeginCollision; this.footBack.EndCollision += FootBackBody_EndCollision; this.footLeft.BeginCollision += FootLeftBody_BeginCollision; this.footLeft.EndCollision += FootLeftBody_EndCollision; this.footRight.BeginCollision += FootRightBody_BeginCollision; this.footRight.EndCollision += FootRightBody_EndCollision; this.shipBody.BeginCollision += this.Body_BeginCollision; this.shipBody.EndCollision += this.Body_EndCollision; } }
/// <summary> /// Create Sphere /// </summary> /// <param name="name"></param> /// <param name="position"></param> private void CreateSphere(Vector3 position) { Entity primitive = new Entity("Sphere" + instances++) .AddComponent(new Transform3D() { Position = position, Scale = Vector3.One / 2 }) .AddComponent(new SphereCollider()) .AddComponent(Model.CreateSphere()) .AddComponent(new RigidBody3D() { KineticFriction = 10, Restitution = 1 }) .AddComponent(new MaterialsMap(new BasicMaterial(GetRandomColor()))) .AddComponent(new ModelRenderer()); RigidBody3D rigidBody3D = primitive.FindComponent <RigidBody3D>(false); if (rigidBody3D != null) { rigidBody3D.OnPhysic3DCollision += rigidBody3D_OnPhysic3DCollision; } EntityManager.Add(primitive); // Sets sphere Time To Live. Remove timer from WaveServices after use. WaveServices.TimerFactory.CreateTimer("Timer" + primitive.Name, TimeSpan.FromSeconds(10), () => { EntityManager.Remove(primitive); WaveServices.TimerFactory.RemoveTimer("Timer" + primitive.Name); }); }
protected override void Update(TimeSpan gameTime) { touchPanelState = WaveServices.Input.TouchPanelState; if (touchPanelState.IsConnected && touchPanelState.Count > 0) { if (!pressed) { pressed = true; if (ball == null) { ball = Helpers.CreateSphere("ball", Vector3.Zero, ballSize, BALL_MASS, Color.Gray); EntityManager.Add(ball); } RigidBody3D rigidBody = ball.FindComponent <RigidBody3D>(); rigidBody.ResetPosition(Camera.Position); var direction = Camera.Transform.WorldTransform.Forward; direction.Normalize(); rigidBody.ApplyLinearImpulse(direction * 100f); } } else { pressed = false; } }
private void CreateSphere(Vector3 position) { RigidBody3D rigidBody; Entity primitive = new Entity() .AddComponent(new Transform3D() { Position = position, Scale = Vector3.One / 2 }) .AddComponent(new SphereCollider3D()) .AddComponent(Model.CreateSphere()) .AddComponent(rigidBody = new RigidBody3D() { KineticFriction = 10, Restitution = 1 }) .AddComponent(new MaterialsMap() { DefaultMaterialPath = WaveContent.Assets.basicMaterial }) .AddComponent(new TimeAliveBehavior()) .AddComponent(new ModelRenderer()); this.EntityManager.Add(primitive); rigidBody.OnPhysic3DCollision += this.rigidBody_OnPhysic3DCollision; }
public void Test() { Scene scene = new Scene(); GameObject ground = new GameObject(); ground.SetPosition(new Vec3(0f, -3f, -3f)) .SetBounds(new Vec3(10f, 1f, 10f)); ground.AddComponent(new SolidBoxObject3D { Colour = Color4.Red }); Vec3 p1 = ground.Transform.Position; ground.AddComponent(new BoxCollider()); RigidBody3D rig1 = new RigidBody3D(); ground.AddComponent(rig1); rig1.RigidBody.IsStatic = true; scene.AddObject(ground); GameObject player = new GameObject(); player.SetPosition(new Vec3(2, 0, 0)) .SetBounds(Vec3.One); player.AddComponent(new SolidBoxObject3D { Colour = Color4.Green }); Vec3 p2 = player.Transform.Position; player.AddComponent(new BoxCollider()); RigidBody3D rig2 = new RigidBody3D(); player.AddComponent(rig2); player.AddComponent(new PlayerComponent()); scene.AddObject(player); GameObject camera = new GameObject(); camera.SetPosition(new Vec3(0, 0, -10)); camera.AddComponent(new StaticCamera()); scene.AddObject(camera); GameObject g1 = new GameObject(); GlobalPhysicsComponent3D physics = new GlobalPhysicsComponent3D(); physics.AddRigidBody(rig1); physics.AddRigidBody(rig2); g1.AddComponent(physics); scene.AddObject(g1); IGame game = new GameBuilder() .SetDefaultEvents() .Build(); game.LoadScene(scene); game.Run(60.0f); }
// Start is called before the first frame update void Start() { debug = false; meshFilter = GetComponent <MeshFilter>(); bounds = meshFilter.mesh.bounds; size = Vector3.Scale(bounds.size, transform.localScale); rigidBody = GetComponent <RigidBody3D>(); }
public void AddRigidBody(RigidBody3D rb) { if (rb as PlaneBody != null) { lines.Add(rb); } else { bodies.Add(rb); } }
private void RefreshFiredEntity() { if (!string.IsNullOrEmpty(this.firedEntityPath)) { var ball = this.EntityManager.Find(this.firedEntityPath); if (ball != null) { this.ballRigidBody = ball.FindComponent <RigidBody3D>(); } } }
protected override void Update(TimeSpan gameTime) { var touches = WaveServices.Input.TouchPanelState; if (touches.Count > 0) { if (!pressed) { pressed = true; RigidBody3D rigidBody; if (this.ball == null || this.ball.IsDisposed) { this.ball = new Entity() { Tag = "Removable" } .AddComponent(new Transform3D() { Scale = new Vector3(0.25f) }) .AddComponent(new MaterialsMap(this.ballMaterial)) .AddComponent(new Model("Content/Models/ball.FBX")) .AddComponent(new SphereCollider()) .AddComponent(rigidBody = new RigidBody3D() { Mass = 2, EnableContinuousContact = true, CollisionGroup = this.collisionGroup }) .AddComponent(new ModelRenderer()); EntityManager.Add(this.ball); } else { rigidBody = this.ball.FindComponent <RigidBody3D>(); } var position = camera.Position; var direction = camera.LookAt - camera.Position; direction.Normalize(); rigidBody.ResetPosition(position); rigidBody.ApplyLinearImpulse(100 * direction); } } else { pressed = false; } }
public RigidBody3D rb;//= new RigidBody3D(); //public float timer; // Start is called before the first frame update void Start() { // timer = 0.0f; debug = false; meshFilter = GetComponent <MeshFilter>(); rb = GetComponent <RigidBody3D>(); bounds = meshFilter.mesh.bounds; size = bounds.size; // rb.restitution=0.8f; // rb.friction=0.6f; rb.mass = 0.2f; }
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; } } }
public Contact3D(Vector3 normal, float d, RigidBody3D a, RigidBody3D b, Vector3 pointA, Vector3 pointB) { Normal = normal; Dist = d; A = a; B = b; this.pointA = pointA; this.pointB = pointB; Normal = new Vector3((float)(Normal.X + (rand.NextDouble() - 0.5) / 100f), (float)(Normal.Y + (rand.NextDouble() - 0.5) / 100f), (float)(Normal.Z + (rand.NextDouble() - 0.5) / 100f)); //I = (1+e)*N*(Vr • N) / (1/Ma + 1/Mb) //Impulse = (Vector3.Dot(b.Vel - a.Vel, normal) / (a.InvMass + b.InvMass)); ImpulseN = 0; }
protected override void Update(TimeSpan gameTime) { RigidBody3D rigidBody = _toast.FindComponent <RigidBody3D>(); if (rigidBody.Transform3D.Position.Y >= 0) { Camera.Position.Y = rigidBody.Transform3D.Position.Y; Camera.LookAt.Y = rigidBody.Transform3D.Position.Y; } else { SceneManager.Instance.To <EvaluationScene>(); this.IsActive = false; } EntityManager.Find <TextBlock>("ToasterPosition").Text = "Höhe: " + rigidBody.Transform3D.Position.Y; }
protected override void Update(TimeSpan gameTime) { var touches = WaveServices.Input.TouchPanelState; if (touches.Count > 0) { if (!pressed) { pressed = true; var sphere = new Entity() .AddComponent(new Transform3D() { Scale = new Vector3(1) }) .AddComponent(new MaterialsMap() { DefaultMaterialPath = WaveContent.Assets.basicMaterial }) .AddComponent(Model.CreateSphere()) .AddComponent(new SphereCollider3D()) .AddComponent(new RigidBody3D() { Mass = 2, EnableContinuousContact = true }) .AddComponent(new TimeAliveBehavior()) .AddComponent(new ModelRenderer()); this.EntityManager.Add(sphere); RigidBody3D rigidBody = sphere.FindComponent <RigidBody3D>(); rigidBody.ResetPosition(Camera.Position); var direction = Camera.Transform.WorldTransform.Forward; direction.Normalize(); rigidBody.ApplyLinearImpulse(100 * direction); } } else { pressed = false; } }
protected override void Update(TimeSpan gameTime) { var touches = WaveServices.Input.TouchPanelState; if (touches.Count > 0) { if (!pressed) { pressed = true; if (sphere == null) { sphere = new Entity("ball") .AddComponent(new Transform3D() { Scale = new Vector3(1) }) .AddComponent(new MaterialsMap(new BasicMaterial(Color.Red))) .AddComponent(Model.CreateSphere()) .AddComponent(new SphereCollider()) .AddComponent(new RigidBody3D() { Mass = 2, EnableContinuousContact = true }) .AddComponent(new ModelRenderer()); EntityManager.Add(sphere); } RigidBody3D rigidBody = sphere.FindComponent <RigidBody3D>(); rigidBody.ResetPosition(Camera.Position); var direction = Camera.LookAt - Camera.Position; direction.Normalize(); rigidBody.ApplyLinearImpulse(100 * direction); } } else { pressed = false; } }
protected override void Update(TimeSpan gameTime) { var touches = WaveServices.Input.TouchPanelState; if (touches.Count > 0) { if (!pressed) { pressed = true; RigidBody3D rigidBody = linkedEntity.FindComponent <RigidBody3D>(); rigidBody.ResetPosition(Camera.Position); var direction = Camera.Transform.WorldTransform.Forward; direction.Normalize(); rigidBody.ApplyLinearImpulse(100 * direction); } } else { pressed = false; } }
/// <summary> /// Create Sphere /// </summary> /// <param name="name"></param> /// <param name="position"></param> private void CreateSphere(Vector3 position, bool isBlueBall) { Entity primitive = new Entity("Sphere" + instances++) .AddComponent(new Transform3D() { Position = position, Scale = Vector3.One / 2 }) .AddComponent(new SphereCollider()) .AddComponent(Model.CreateSphere()) .AddComponent(new RigidBody3D()) .AddComponent(new MaterialsMap(new BasicMaterial(isBlueBall ? Color.Red : Color.Blue))) .AddComponent(new ModelRenderer()); EntityManager.Add(primitive); // Collision Grouping. Blue ball is groupA. Red Ball is groupB RigidBody3D rigidBody = primitive.FindComponent <RigidBody3D>(); if (rigidBody != null) { rigidBody.CollisionGroup = isBlueBall ? groupA : groupB; } }
public Constraint3D(RigidBody3D bodyA, RigidBody3D bodyB) { BodyA = bodyA; BodyB = bodyB; }
public override void GenerateContacts(ref List <RigidBody3D> bodies, ref List <Contact3D> contacts, float dt) { //Clear all cells for (int i = 0; i < cells.Length; i++) { cells[i].Clear(); } //Insert bodies into all cells they overlap for (int i = 0; i < bodies.Count; i++) { AABB3D bounds = bodies[i].MotionBounds; Vector3 min = bounds.GetMin(); Vector3 max = bounds.GetMax(); int startRow = (int)(min.X * invWidth * rows); startRow = (int)Math.Max(startRow, 0); int startCol = (int)(min.Y * invHeight * cols); startCol = (int)Math.Max(startCol, 0); int startStack = (int)(min.Z * invDepth * stacks); startStack = (int)Math.Max(startStack, 0); int endRow = (int)(max.X * invWidth * rows); endRow = (int)Math.Min(endRow, rows - 1); int endCol = (int)(max.Y * invHeight * cols); endCol = (int)Math.Min(endCol, cols - 1); int endStack = (int)(max.Z * invDepth * stacks); endStack = (int)Math.Min(endStack, stacks - 1); for (int j = startRow; j <= endRow; j++) { for (int k = startCol; k <= endCol; k++) { for (int l = startStack; l <= endStack; l++) { cells[(j * cols) + (k * rows) + l].AddObject(i); } } } } //Loop through each cell, add speculative contacts to each object in the cell for (int i = 0; i < cells.Length; i++) { if (cells[i].Indices.Count <= 0) { continue; } Cell3D currentCell = cells[i]; foreach (int index1 in currentCell.Indices) { foreach (int index2 in currentCell.Indices) { if (index1 == index2) { continue; } RigidBody3D a = bodies[index1]; RigidBody3D b = bodies[index2]; if (a.InvMass != 0 || b.InvMass != 0) { //Add a speculative contact contacts.Add(a.GenerateContact(b, dt)); } } } } }
public DistanceConstraint3D(RigidBody3D bodyA, RigidBody3D bodyB, float dist) : base(bodyA, bodyB) { distance = dist; }
public override void OnInitialze() { base.OnInitialze(); _body = GameObject.GetComponentUnsafe <RigidBody3D>().Value; }
public static void CheckAABBs(CubeBehaviour a, CubeBehaviour b) { Contact contactB = new Contact(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)) { // determine the distances between the contact extents float[] distances = { (b.max.x - a.min.x), (a.max.x - b.min.x), (b.max.y - a.min.y), (a.max.y - b.min.y), (b.max.z - a.min.z), (a.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]; } } // set the contact properties contactB.face = face; contactB.penetration = penetration; // check if contact does not exist if (!a.contacts.Contains(contactB)) { // remove any contact that matches the name but not other parameters for (int i = a.contacts.Count - 1; i > -1; i--) { if (a.contacts[i].cube.name.Equals(contactB.cube.name)) { a.contacts.RemoveAt(i); } } if (contactB.face == Vector3.down) { a.gameObject.GetComponent <RigidBody3D>().Stop(); a.gameObject.GetComponent <RigidBody3D>().velocity.y = 0.0f; a.isGrounded = true; } // add the new contact a.contacts.Add(contactB); a.isColliding = true; } // Resolving even if they had a collision before RigidBody3D a_rid = a.GetComponentInParent <RigidBody3D>(); RigidBody3D b_rid = b.GetComponentInParent <RigidBody3D>(); if (a_rid.bodyType == BodyType.DYNAMIC && b_rid.bodyType == BodyType.STATIC) { a.transform.position = a.transform.position - contactB.face * contactB.penetration * 1.0f; } else if (a_rid.bodyType == BodyType.DYNAMIC && b_rid.bodyType == BodyType.DYNAMIC) { float prop1 = 0.5f; float prop2 = 0.5f; if (contactB.face == Vector3.up) { prop1 = 0.0f; prop2 = 0.5f; } else if (contactB.face == Vector3.down) { prop1 = 0.5f; prop2 = 0.0f; } a.transform.position = a.transform.position - contactB.face * contactB.penetration * prop1; b.transform.position = b.transform.position + contactB.face * contactB.penetration * prop2; } } else if (a.contacts.Exists(x => x.cube.gameObject.name == b.gameObject.name)) { { a.contacts.Remove(a.contacts.Find(x => x.cube.gameObject.name.Equals(b.gameObject.name))); a.isColliding = false; if (a.gameObject.GetComponent <RigidBody3D>().bodyType == BodyType.DYNAMIC) { a.gameObject.GetComponent <RigidBody3D>().isFalling = 1; a.isGrounded = false; } } } }
private Scene GetInitScene() { Scene scene = new Scene(); GameObject g1 = new GameObject(); g1.SetPosition(new Vec3(0f, 0f, 0f)) .SetBounds(new Vec3(1f, 1f, 0f)); g1.AddComponent(new SolidBoxObject2D { Colour = Color4.White }); g1.AddComponent(new RotateComponent()); scene.AddObject(g1); GameObject g2 = new GameObject(); g2.SetPosition(new Vec3(-2f, -1f, 0f)) .SetBounds(new Vec3(2f, 2f, 2f)); g2.AddComponent(new SolidBoxObject3D { Colour = Color4.Pink, Layer = 99 }); //g2.AddComponent(new RotateComponent()); g2.AddComponent(new BoxCollider()); RigidBody3D body3D = new RigidBody3D(); g2.AddComponent(body3D); scene.AddObject(g2); GameObject plane = new GameObject(); plane.SetPosition(new Vec3(-5f, -4f, 0f)) .SetBounds(new Vec3(8f, 2f, 8f)); plane.AddComponent(new SolidBoxObject3D { Colour = Color4.Green, Layer = 99 }); plane.AddComponent(new BoxCollider()); RigidBody3D body3D1 = new RigidBody3D(); plane.AddComponent(body3D1); body3D1.RigidBody.IsStatic = true; scene.AddObject(plane); GameObject g3 = new GameObject(); SolidPolygonObject2D poly = new SolidPolygonObject2D { Colour = Color4.Gold, }; g3.SetPosition(new Vec3(-3f, -3f, 0f)); poly.Layer = 1; poly.Points.Add(new Vec3(1f, 1.5f, 0)); poly.Points.Add(new Vec3(0f, 0, 0)); poly.Points.Add(new Vec3(2f, 0, 0)); g3.AddComponent(poly); g3.AddComponent(new RotateComponent()); scene.AddObject(g3); GameObject g4 = new GameObject(); SolidPolygonObject3D poly3 = new SolidPolygonObject3D { Colour = Color4.Gray, }; g4.SetPosition(new Vec3(1f, 1f, 0f)) .SetBounds(new Vec3(0, 0, 3f)); poly3.Layer = 1; poly3.Points.Add(new Vec3(0, 0)); poly3.Points.Add(new Vec3(1f, 0)); poly3.Points.Add(new Vec3(1.5f, 0.86f)); poly3.Points.Add(new Vec3(1.0f, 1.72f)); poly3.Points.Add(new Vec3(0, 1.72f)); poly3.Points.Add(new Vec3(-0.5f, 0.87f)); g4.AddComponent(poly3); g4.AddComponent(new RotateComponent()); scene.AddObject(g4); GameObject g5 = new GameObject(); g5.SetPosition(new Vec3(2, 2, 0)); g5.AddComponent(new PointsObject2D { Colour = Color4.White, PointSize = 10 } ); scene.AddObject(g5); GameObject g6 = new GameObject(); g6.AddComponent(new CircleObject2D { Radius = 0.5f, Colour = Color4.Red }); g6.SetPosition(new Vec3(3.5f, 1f, 0)) .SetBounds(new Vec3(1, 1, 0)); scene.AddObject(g6); GameObject g7 = new GameObject(); g7.SetBounds(new Vec3(3, 3, 0)) .SetPosition(new Vec3(-2, -2, 0)); g7.AddComponent(new RawTexture2D("Images/download.png")); scene.AddObject(g7); GameObject g8 = new GameObject(); g8.SetBounds(new Vec3(1, 1, 0)) .SetPosition(new Vec3(0.5f, 0, 0)); g8.AddComponent(new TextRenderer(100, 100) { FontColor = Color.Red, Text = "Hello" }); scene.AddObject(g8); IAudio audio = ResourceManager.LoadWave("Sounds/Mixdown2.wav"); audio.SetLoop(true); //audio.Play(); GameObject physics = new GameObject(); GlobalPhysicsComponent3D physicsComponent3D = new GlobalPhysicsComponent3D(); physicsComponent3D.AddRigidBody(g2.GetComponentUnsafe <RigidBody3D>().Value); physicsComponent3D.AddRigidBody(plane.GetComponentUnsafe <RigidBody3D>().Value); physics.AddComponent(physicsComponent3D); scene.AddObject(physics); GameObject camera = new GameObject(); camera.SetPosition(new Vec3(0, 0, -10f)); camera.AddComponent(new StaticCamera()); scene.AddObject(camera); scene.Save("scene.json"); return(scene); }
private void RefreshFiredEntity() { if (!string.IsNullOrEmpty(this.firedEntityPath)) { var ball = this.EntityManager.Find(this.firedEntityPath); if (ball != null) { this.ballRigidBody = ball.FindComponent<RigidBody3D>(); } } }
public static void CheckAABBs(CubeBehaviour a, CubeBehaviour b) { Contact contactB = new Contact(b); RigidBody3D aRigidBody = a.GetComponent <RigidBody3D>(); RigidBody3D bRigidBody = b.GetComponent <RigidBody3D>(); 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)) { // determine the distances between the contact extents float[] distances = { (b.max.x - a.min.x), (a.max.x - b.min.x), (b.max.y - a.min.y), (a.max.y - b.min.y), (b.max.z - a.min.z), (a.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]; } } // set the contact properties contactB.face = face; contactB.penetration = penetration; // check if contact does not exist if (!a.contacts.Contains(contactB)) { // remove any contact that matches the name but not other parameters for (int i = a.contacts.Count - 1; i > -1; i--) { if (a.contacts[i].cube.name.Equals(contactB.cube.name)) { a.contacts.RemoveAt(i); } } if (contactB.face == Vector3.down) // if the cubes are on top of each other { if (b.tag == "Floor" && a.tag != "Player") // keep moving if it hits the floor { aRigidBody.velocity.y = 0; aRigidBody.acceleration.y = 0; a.isGrounded = true; } else { a.gameObject.GetComponent <RigidBody3D>().Stop(); a.isGrounded = true; } } if (a.tag == "Player" && bRigidBody.bodyType == BodyType.DYNAMIC) { // keep the cube on the ground //bRigidBody.velocity.y = 0; //bRigidBody.acceleration.y = 0; Vector3 normalizedVelocity = Vector3.Normalize(aRigidBody.velocity); bRigidBody.forceX = a.GetComponent <PlayerBehaviour>().forceMag *normalizedVelocity.x; bRigidBody.forceZ = a.GetComponent <PlayerBehaviour>().forceMag *normalizedVelocity.z; // acceleration = force/mass Vector3 impulse = new Vector3(bRigidBody.forceX / bRigidBody.mass, 0.0f, bRigidBody.forceZ / bRigidBody.mass); Debug.Log("Player to Cube Collision"); b.transform.position += Vector3.Normalize(aRigidBody.velocity) * a.GetComponent <PlayerBehaviour>().speed / 5 * contactB.penetration; // resolution // bRigidBody.velocity = Vector3.Normalize(aRigidBody.velocity) * a.GetComponent<PlayerBehaviour>().speed / 10 * Time.deltaTime; // match the velocity to the player bRigidBody.velocity = impulse * Time.deltaTime; } // add the new contact a.contacts.Add(contactB); a.isColliding = true; } } else { if (a.contacts.Exists(x => x.cube.gameObject.name == b.gameObject.name)) { a.contacts.Remove(a.contacts.Find(x => x.cube.gameObject.name.Equals(b.gameObject.name))); a.isColliding = false; if (a.gameObject.GetComponent <RigidBody3D>().bodyType == BodyType.DYNAMIC) { a.gameObject.GetComponent <RigidBody3D>().isFalling = true; a.isGrounded = false; } } if (a.isGrounded && a.tag != "Player") { aRigidBody.velocity.y = 0; aRigidBody.acceleration.y = 0; } if (a.contacts.Count == 0 && aRigidBody.isFalling) { aRigidBody.acceleration.y = -0.001f; } } }
protected override void Update(TimeSpan gameTime) { var touches = WaveServices.Input.TouchPanelState; if (touches.Count > 0) { if (!pressed) { pressed = true; RigidBody3D rigidBody; if (this.ball == null || this.ball.IsDisposed) { this.ball = new Entity() { Tag = "Removable" } .AddComponent(new Transform3D() { Scale = new Vector3(0.25f) }) .AddComponent(new MaterialsMap(this.ballMaterial)) .AddComponent(new Model("Content/Models/ball.FBX")) .AddComponent(new SphereCollider()) .AddComponent(rigidBody = new RigidBody3D() { Mass = 2, EnableContinuousContact = true, CollisionGroup = this.collisionGroup }) .AddComponent(new ModelRenderer()); EntityManager.Add(this.ball); } else { rigidBody = this.ball.FindComponent<RigidBody3D>(); } var position = camera.Position; var direction = camera.LookAt - camera.Position; direction.Normalize(); rigidBody.ResetPosition(position); rigidBody.ApplyLinearImpulse(100 * direction); } } else { pressed = false; } }
public static void CheckAABBs(CubeBehaviour a, CubeBehaviour b) { Contact contactB = new Contact(b); Contact contactA = new Contact(a); RigidBody3D aRb = a.GetComponentInParent(typeof(RigidBody3D)) as RigidBody3D; RigidBody3D bRb = b.GetComponentInParent(typeof(RigidBody3D)) as RigidBody3D; 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)) { // determine the distances between the contact extents float[] distances = { (b.max.x - a.min.x), (a.max.x - b.min.x), (b.max.y - a.min.y), (a.max.y - b.min.y), (b.max.z - a.min.z), (a.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]; } } // set the contact properties contactB.face = face; contactB.penetration = penetration; // check if contact does not exist if (!a.contacts.Contains(contactB)) { // remove any contact that matches the name but not other parameters for (int i = a.contacts.Count - 1; i > -1; i--) { if (a.contacts[i].cube.name.Equals(contactB.cube.name)) { a.contacts.RemoveAt(i); } } if (a.name == "SquareBullet(Clone)") { Debug.Log("SQUAREBULLET HIT"); SquareBulletBehaviour sbBehaviour = a.GetComponentInParent <SquareBulletBehaviour>(); if ((contactB.face == Vector3.forward) || (contactB.face == Vector3.back)) { Debug.Log("BOUNCING BACK"); sbBehaviour.direction = new Vector3(sbBehaviour.direction.x, sbBehaviour.direction.y, -sbBehaviour.direction.z); } else if ((contactB.face == Vector3.right) || (contactB.face == Vector3.left)) { sbBehaviour.direction = new Vector3(-sbBehaviour.direction.x, sbBehaviour.direction.y, sbBehaviour.direction.z); } else if ((contactB.face == Vector3.up) || (contactB.face == Vector3.down)) { sbBehaviour.direction = new Vector3(sbBehaviour.direction.x, -sbBehaviour.direction.y, sbBehaviour.direction.z); } return; } else { if (contactB.face == Vector3.down) { a.gameObject.GetComponent <RigidBody3D>().Stop(); a.isGrounded = true; } else if (contactB.face != Vector3.up) { //DIDN'T WORK QUITE RIGHT. HAPPENED EVERY FRAME RESULTING IN EXTRAORDINARY VELOCITY OF IMPULSE // Relative velocity = Vr // Collision Normal = n // Coefficient of Restitution = e // magnitude of the impulse = j //Debug.Log("--------NEW COLLISION---------" + a + b); //Vector3 relativeVelocity = bRb.velocity - aRb.velocity; //Debug.Log("Relative Velocity: " + relativeVelocity); //float relativeNormal = Vector3.Dot(relativeVelocity, contactB.face); //Debug.Log("Relative Normal: " + relativeNormal); //float coefficientOfRestitution = Mathf.Min(aRb.bounciness, bRb.bounciness); //Debug.Log("coefficientOfRestitution: " + coefficientOfRestitution); //float magnitudeOfVelocityAfterCollision = (-coefficientOfRestitution) * relativeNormal; //Debug.Log("magnitudeOfVelocityAfterCollision: " + magnitudeOfVelocityAfterCollision); //float magnitudeOfImpulse = (-(1 + coefficientOfRestitution) * (relativeNormal)) / ((1 / aRb.mass) + (1 / bRb.mass)); //Debug.Log("magnitudeOfImpulse: " + magnitudeOfImpulse); //aRb.velocity = aRb.velocity - ((magnitudeOfImpulse / aRb.mass) * contactA.face); //Debug.Log("A velocity: " + aRb.velocity); //bRb.velocity = bRb.velocity - ((magnitudeOfImpulse / bRb.mass) * contactB.face); //Debug.Log("B Velocity: " + bRb.velocity); //Debug.Log("--------END OF COLLISION---------"); // THIS WORKS if (b.gameObject.GetComponent <RigidBody3D>().bodyType == BodyType.DYNAMIC) { bRb.transform.position += contactB.face * penetration; return; } } } // add the new contact a.contacts.Add(contactB); a.isColliding = true; } } else { if (a.contacts.Exists(x => x.cube.gameObject.name == b.gameObject.name)) { a.contacts.Remove(a.contacts.Find(x => x.cube.gameObject.name.Equals(b.gameObject.name))); a.isColliding = false; if (a.gameObject.GetComponent <RigidBody3D>().bodyType == BodyType.DYNAMIC) { a.gameObject.GetComponent <RigidBody3D>().isFalling = true; a.isGrounded = false; } } } }