public static void GetShapeMeshData(EntityCollidable collidable, List<VertexPositionNormalTexture> vertices, List<ushort> indices) { var compoundCollidable = collidable as CompoundCollidable; if (compoundCollidable == null) throw new ArgumentException("Wrong shape type."); var tempIndices = new List<ushort>(); var tempVertices = new List<VertexPositionNormalTexture>(); for (int i = 0; i < compoundCollidable.Children.Count; i++) { var child = compoundCollidable.Children[i]; ModelDrawer.ShapeMeshGetter shapeMeshGetter; if (ModelDrawer.ShapeMeshGetters.TryGetValue(child.CollisionInformation.GetType(), out shapeMeshGetter)) { shapeMeshGetter(child.CollisionInformation, tempVertices, tempIndices); for (int j = 0; j < tempIndices.Count; j++) { indices.Add((ushort)(tempIndices[j] + vertices.Count)); } RigidTransform localTransform = child.Entry.LocalTransform; Vector3 localPosition = child.CollisionInformation.LocalPosition; for (int j = 0; j < tempVertices.Count; j++) { VertexPositionNormalTexture vertex = tempVertices[j]; Vector3.Add(ref vertex.Position, ref localPosition, out vertex.Position); RigidTransform.Transform(ref vertex.Position, ref localTransform, out vertex.Position); Vector3.Transform(ref vertex.Normal, ref localTransform.Orientation, out vertex.Normal); vertices.Add(vertex); } tempVertices.Clear(); tempIndices.Clear(); } } }
public static void GetShapeMeshData(EntityCollidable collidable, List<VertexPositionNormalTexture> vertices, List<ushort> indices) { var convexHullShape = collidable.Shape as ConvexHullShape; if (convexHullShape == null) throw new ArgumentException("Wrong shape type."); var hullTriangleVertices = new List<Vector3>(); var hullTriangleIndices = new List<int>(); Toolbox.GetConvexHull(convexHullShape.Vertices, hullTriangleIndices, hullTriangleVertices); //The hull triangle vertices are used as a dummy to get the unnecessary hull vertices, which are cleared afterwards. hullTriangleVertices.Clear(); foreach (int i in hullTriangleIndices) { hullTriangleVertices.Add(convexHullShape.Vertices[i]); } var toReturn = new VertexPositionNormalTexture[hullTriangleVertices.Count]; Vector3 normal; for (ushort i = 0; i < hullTriangleVertices.Count; i += 3) { normal = Vector3.Normalize(Vector3.Cross(hullTriangleVertices[i + 2] - hullTriangleVertices[i], hullTriangleVertices[i + 1] - hullTriangleVertices[i])); vertices.Add(new VertexPositionNormalTexture(hullTriangleVertices[i], normal, new Vector2(0, 0))); vertices.Add(new VertexPositionNormalTexture(hullTriangleVertices[i + 1], normal, new Vector2(1, 0))); vertices.Add(new VertexPositionNormalTexture(hullTriangleVertices[i + 2], normal, new Vector2(0, 1))); indices.Add(i); indices.Add((ushort)(i + 1)); indices.Add((ushort)(i + 2)); } }
private void Events_InitialCollisionDetected(EntityCollidable sender, Collidable info, CollidablePairHandler pair, ContactData contact) { try { //var contact = pair.Contacts[0].Contact; //if(contact.PenetrationDepth>2f) //DebugSystem.Instance.DebugCommandUI.Echo(contact.PenetrationDepth.ToString()); // Select collisionInformation for object in contact with instead of the ships own collisionInformation Collidable candidate = (pair.BroadPhaseOverlap.EntryA == racerEntity.CollisionInformation ? pair.BroadPhaseOverlap.EntryB : pair.BroadPhaseOverlap.EntryA) as Collidable; if (candidate.Equals(Physics.currentTrackFloor)) { ReactToTrackHit(contact); } else if (candidate.Equals(Physics.currentTrackWall)) { ReactToWallHit(contact); } else { ReactToShipShipCollision(contact, candidate); } } catch (Exception e) { // System.Diagnostic.Debug.WriteLine("Unfound pair"); } }
public override void collisionEvent(EntityCollidable eCollidable, Collidable collidable, CollidablePairHandler pairHandler) { // bool valid = false; if (collidable.Tag != null && collidable.Tag is CollisionBase) { SceneObject collidee = ((CollisionBase)collidable.Tag).Parent; if (collidee != null && !(collidee is Projectile) && Parent != null && Parent != collidee) { if (collidee is Ship) { collidee.onCollide(this, Damage); /*if (collidee is Ship) { valid = true; }*/ } else { } } else { } } else { } /*if (valid == true) { SceneObjectFactory.createExplosion(Position, Rotation); } else { }*/ }
void HandleCollision(EntityCollidable sender, Collidable other, CollidablePairHandler pair) { if (soundEffect != null) { emitter.Position = body.Position / 5.0f; } }
protected void HandleCollision(EntityCollidable sender, Collidable other, CollidablePairHandler pair) { GameEntity ge = other.Tag as GameEntity; if (ge != null && ge.Name == "player") { Game.DrinkBeer(physicalData.Position); Entity.KillEntity(); } }
protected void HandleCollision(EntityCollidable sender, Collidable other, CollidablePairHandler pair) { GameEntity ge = other.Tag as GameEntity; if (ge != null && ge.Name == "punch") { Game.PunchProp(); Entity.Dead = true; } }
void HandleCollision(EntityCollidable sender, Collidable other, CollidablePairHandler pair) { Console.WriteLine("Got here!"); emitter.Position = body.Position / 5; ; listener.Position = XNAGame.Instance.Camera.Position / 5; soundEffectInstance.Apply3D(listener, emitter); soundEffectInstance.Play(); }
public static void GetShapeMeshData(EntityCollidable collidable, List<VertexPositionNormalTexture> vertices, List<ushort> indices) { var minkowskiShape = collidable.Shape as MinkowskiSumShape; if (minkowskiShape == null) throw new ArgumentException("Wrong shape type."); var points = new List<Vector3>(); Vector3 max; var direction = new Vector3(); float angleChange = MathHelper.TwoPi / NumSamples; for (int i = 1; i < NumSamples / 2 - 1; i++) { float phi = MathHelper.PiOver2 - i * angleChange; var sinPhi = (float)Math.Sin(phi); var cosPhi = (float)Math.Cos(phi); for (int j = 0; j < NumSamples; j++) { float theta = j * angleChange; direction.X = (float)Math.Cos(theta) * cosPhi; direction.Y = sinPhi; direction.Z = (float)Math.Sin(theta) * cosPhi; minkowskiShape.GetLocalExtremePoint(direction, out max); points.Add(max); } } minkowskiShape.GetLocalExtremePoint(Toolbox.UpVector, out max); points.Add(max); minkowskiShape.GetLocalExtremePoint(Toolbox.DownVector, out max); points.Add(max); var hullTriangleVertices = new List<Vector3>(); var hullTriangleIndices = new List<int>(); Toolbox.GetConvexHull(points, hullTriangleIndices, hullTriangleVertices); //The hull triangle vertices are used as a dummy to get the unnecessary hull vertices, which are cleared afterwards. hullTriangleVertices.Clear(); foreach (int i in hullTriangleIndices) { hullTriangleVertices.Add(points[i]); } Vector3 normal; for (ushort i = 0; i < hullTriangleVertices.Count; i += 3) { normal = Vector3.Normalize(Vector3.Cross(hullTriangleVertices[i + 2] - hullTriangleVertices[i], hullTriangleVertices[i + 1] - hullTriangleVertices[i])); vertices.Add(new VertexPositionNormalTexture(hullTriangleVertices[i], normal, new Vector2(0, 0))); vertices.Add(new VertexPositionNormalTexture(hullTriangleVertices[i + 1], normal, new Vector2(1, 0))); vertices.Add(new VertexPositionNormalTexture(hullTriangleVertices[i + 2], normal, new Vector2(0, 1))); indices.Add(i); indices.Add((ushort)(i + 1)); indices.Add((ushort)(i + 2)); } }
public override void CollisionEnded(EntityCollidable sender, Collidable other, CollidablePairHandler collisionPair) { if (other.Tag is CharacterSynchronizer) { if ((other.Tag as CharacterSynchronizer).body.Tag is PlayerCreature) { OnExit(); } } }
public override void InitialCollisionDetected(EntityCollidable sender, Collidable other, CollidablePairHandler collisionPair) { if (other.Tag is CharacterSynchronizer) { if ((other.Tag as CharacterSynchronizer).body.Tag is Creature) { ((other.Tag as CharacterSynchronizer).body.Tag as Creature).Die(); } } }
void HandleCollision(EntityCollidable sender, Collidable other, CollidablePairHandler pair) { if (soundEffect != null) { emitter.Position = body.Position / 5.0f; listener.Position = XNAGame.Instance.Camera.Position / 5.0f; soundEffectInstance.Apply3D(listener, emitter); soundEffectInstance.Play(); } }
//Removes the coin from the game if it contacts the character //coin is sender , other is whatever hits it public void coinHit(EntityCollidable sender, Collidable other, CollidablePairHandler pair) { var otherEntityInformation = other as EntityCollidable; if (otherEntityInformation != null && otherEntityInformation.Entity.Tag == cube) { Components.Remove((CoinModel)sender.Entity.Tag); space.Remove(sender.Entity); Console.WriteLine("removed ring"); numRingsHit++; } }
public override void InitialCollisionDetected(EntityCollidable sender, Collidable other, CollidablePairHandler collisionPair) { base.InitialCollisionDetected(sender, other, collisionPair); //Console.WriteLine(other.Tag + " " + mRunTimer); if ((other.Tag is CharacterSynchronizer || other.Tag is PhysicsProp) && mRunTimer > 0.0f) { float totalImpulse = 0; foreach (ContactInformation c in collisionPair.Contacts) { totalImpulse += c.NormalImpulse; } //Console.WriteLine(totalImpulse); int damage = 0; if (totalImpulse > 150.0f) { damage = 2; } else if (totalImpulse > 45.0f) { damage = 1; } Entity hitEntity; if (other.Tag is CharacterSynchronizer) { hitEntity = (other.Tag as CharacterSynchronizer).body; } else { hitEntity = (other.Tag as PhysicsProp).Entity; } Vector3 impulseVector = Vector3.Zero; Vector3 impulseDirection = hitEntity.Position - Creature.Position; if (impulseDirection != Vector3.Zero) { impulseVector = Vector3.Normalize(impulseDirection); } impulseVector.Y = 0.45f; impulseVector.Normalize(); impulseVector *= damage * DamageImpulseMultiplier; hitEntity.ApplyLinearImpulse(ref impulseVector); if (other.Tag is CharacterSynchronizer) { ((other.Tag as CharacterSynchronizer).body.Tag as Creature).Damage(damage, Creature); ((other.Tag as CharacterSynchronizer).body.Tag as Creature).AllowImpulse(); } } }
/// <summary> /// Constructs the query manager for a character. /// </summary> /// <param name="character">Character to manage queries for.</param> public QueryManager(SphereCharacterController character) { this.character = character; //We can share the real shape with the 'current' query object. queryObject = new ConvexCollidable<SphereShape>(character.Body.CollisionInformation.Shape); //Share the collision rules between the main body and its query objects. That way, the character's queries return valid results. queryObject.CollisionRules = character.Body.CollisionInformation.CollisionRules; SupportRayFilter = SupportRayFilterFunction; }
/// <summary> /// Constructs the query manager for a character. /// </summary> /// <param name="character">Character to manage queries for.</param> public QueryManager(CharacterController character) { this.character = character; //We can share the real shape with the 'current' query object. currentQueryObject = new ConvexCollidable<CylinderShape>(character.Body.CollisionInformation.Shape); standingQueryObject = new ConvexCollidable<CylinderShape>(new CylinderShape(character.StanceManager.StandingHeight, character.Body.Radius)); crouchingQueryObject = new ConvexCollidable<CylinderShape>(new CylinderShape(character.StanceManager.CrouchingHeight, character.Body.Radius)); //Share the collision rules between the main body and its query objects. That way, the character's queries return valid results. currentQueryObject.CollisionRules = character.Body.CollisionInformation.CollisionRules; standingQueryObject.CollisionRules = character.Body.CollisionInformation.CollisionRules; crouchingQueryObject.CollisionRules = character.Body.CollisionInformation.CollisionRules; SupportRayFilter = SupportRayFilterFunction; }
public static void GetShapeMeshData(EntityCollidable collidable, List<VertexPositionNormalTexture> vertices, List<ushort> indices) { ConeShape coneShape = collidable.Shape as ConeShape; if (coneShape == null) throw new ArgumentException("Wrong shape type."); float verticalOffset = -coneShape.Height / 4; float angleBetweenFacets = MathHelper.TwoPi / NumSides; float radius = coneShape.Radius; //Create the vertex list var topVertexPosition = new Vector3(0, coneShape.Height + verticalOffset, 0); for (int i = 0; i < NumSides; i++) { float theta = i * angleBetweenFacets; var position = new Vector3((float)Math.Cos(theta) * radius, verticalOffset, (float)Math.Sin(theta) * radius); Vector3 offset = topVertexPosition - position; Vector3 normal = Vector3.Normalize(Vector3.Cross(Vector3.Cross(offset, Vector3.Up), offset)); //Top vertex vertices.Add(new VertexPositionNormalTexture(topVertexPosition, normal, Vector2.Zero)); //Sloped vertices vertices.Add(new VertexPositionNormalTexture(position, normal, Vector2.Zero)); //Bottom vertices vertices.Add(new VertexPositionNormalTexture(position, Vector3.Down, Vector2.Zero)); } //Create the index list for (ushort i = 0; i < vertices.Count; i += 3) { //Each iteration, the loop advances to the next vertex 'column.' //Four triangles per column (except for the four degenerate cap triangles). //Sloped Triangles indices.Add(i); indices.Add((ushort)(i + 1)); indices.Add((ushort)((i + 4) % vertices.Count)); //Bottom cap triangles. var nextIndex = (ushort)((i + 5) % vertices.Count); if (nextIndex != 2) //Don't add cap indices if it's going to be a degenerate triangle. { indices.Add((ushort)(i + 2)); indices.Add(2); indices.Add(nextIndex); } } }
protected void HandleCollision(EntityCollidable sender, Collidable other, CollidablePairHandler pair) { GameEntity ge = other.Tag as GameEntity; if (ge != null && ge.Name != "player" && ge.Name != "derper") { if (ge.Name == "prop") { if (player) { Game.PunchObject(ge); } else { Game.AddStupid(0); } } Entity.KillEntity(); } }
protected void TryToAdd(EntityCollidable collidable) { CollisionRule rule; if ((rule = CollisionRules.collisionRuleCalculator(DetectorVolume, collidable)) < CollisionRule.NoNarrowPhasePair) { //Clamp the rule to the parent's rule. Always use the more restrictive option. //Don't have to test for NoNarrowPhasePair rule on the parent's rule because then the parent wouldn't exist! if (rule < CollisionRule) rule = CollisionRule; if (!subPairs.ContainsKey(collidable)) { var newPair = NarrowPhaseHelper.GetPairHandler(DetectorVolume, collidable, rule) as DetectorVolumePairHandler; if (newPair != null) { newPair.Parent = this; subPairs.Add(collidable, newPair); } } containedPairs.Add(collidable); } }
public static void GetShapeMeshData(EntityCollidable collidable, List<VertexPositionNormalTexture> vertices, List<ushort> indices) { var triangleShape = collidable.Shape as TriangleShape; if(triangleShape == null) throw new ArgumentException("Wrong shape type."); Vector3 normal = triangleShape.GetLocalNormal(); vertices.Add(new VertexPositionNormalTexture(triangleShape.VertexA, -normal, new Vector2(0, 0))); vertices.Add(new VertexPositionNormalTexture(triangleShape.VertexB, -normal, new Vector2(0, 1))); vertices.Add(new VertexPositionNormalTexture(triangleShape.VertexC, -normal, new Vector2(1, 0))); vertices.Add(new VertexPositionNormalTexture(triangleShape.VertexA, normal, new Vector2(0, 0))); vertices.Add(new VertexPositionNormalTexture(triangleShape.VertexB, normal, new Vector2(0, 1))); vertices.Add(new VertexPositionNormalTexture(triangleShape.VertexC, normal, new Vector2(1, 0))); indices.Add(0); indices.Add(1); indices.Add(2); indices.Add(3); indices.Add(5); indices.Add(4); }
public override void collisionEvent(EntityCollidable eCollidable, Collidable collidable, CollidablePairHandler pairHandler) { if (collidable.Tag != null && collidable.Tag is CollisionBase) { SceneObject collidee = ((CollisionBase)collidable.Tag).Parent; if (collidee != null && !(collidee is Projectile) && collidee != Parent) { collidee.onCollide(this, Damage); } else { // Was another laser or parent if (collidee != null && collidee is Projectile) { LogCat.updateValue("Collide", "With projectile!"); } else { } } } else { } }
public override void InitialCollisionDetected(EntityCollidable sender, Collidable other, CollidablePairHandler collisionPair) { if (mRopeLimit == null) { base.InitialCollisionDetected(sender, other, collisionPair); // If hit physics object link projectile to it then grapple to projectile, otherwise set projectile to kinematic and grapple to projectile if (other.Tag is CharacterSynchronizer) { StickToEntity((other.Tag as CharacterSynchronizer).body); ((other.Tag as CharacterSynchronizer).body.Tag as Creature).Damage(0, (mOwner as Creature)); } else if (other.Tag is IEntityOwner) { StickToEntity((other.Tag as IEntityOwner).Entity); } else { Entity.BecomeKinematic(); } CreateRope(); } }
public override void InitialCollisionDetected(EntityCollidable sender, Collidable other, CollidablePairHandler collisionPair) { if (other.Tag is CharacterSynchronizer && !mLoaded) { CharacterSynchronizer synchronizer = (other.Tag as CharacterSynchronizer); if (synchronizer.body.Tag is PlayerCreature) { // Check that the player has the requested part. bool hasCorrectPart = false; foreach (Creature.PartAttachment partAttachment in (synchronizer.body.Tag as Creature).PartAttachments) { if (partAttachment != null && partAttachment.Part.GetType() == PartType) { hasCorrectPart = true; break; } } if (hasCorrectPart) { // Load the next level. mLoaded = true; // TODO: Investigate last frame object additions to next world. World.Clear(); // TODO: Make scale globally accessible or modifiable. if (LevelManager.Exists(mNextLevel)) { World.AddLevelFromFile(mNextLevel, Vector3.Zero, Quaternion.Identity, new Vector3(8.0f, 0.01f, 8.0f)); } else { InputAction.IsMouseLocked = false; ChimeraGame.PopState(); ChimeraGame.PushState(new SuccessMenu(ChimeraGame.Game)); } } } } }
public static void GetShapeMeshData(EntityCollidable collidable, List<VertexPositionNormalTexture> vertices, List<ushort> indices) { MobileMeshShape shape = collidable.Shape as MobileMeshShape; var tempVertices = new VertexPositionNormalTexture[shape.TriangleMesh.Data.Vertices.Length]; for (int i = 0; i < shape.TriangleMesh.Data.Vertices.Length; i++) { Vector3 position; shape.TriangleMesh.Data.GetVertexPosition(i, out position); tempVertices[i] = new VertexPositionNormalTexture( position, Vector3.Zero, Vector2.Zero); } for (int i = 0; i < shape.TriangleMesh.Data.Indices.Length; i++) { indices.Add((ushort)shape.TriangleMesh.Data.Indices[i]); } for (int i = 0; i < indices.Count; i += 3) { int a = indices[i]; int b = indices[i + 1]; int c = indices[i + 2]; Vector3 normal = Vector3.Normalize(Vector3.Cross( tempVertices[c].Position - tempVertices[a].Position, tempVertices[b].Position - tempVertices[a].Position)); tempVertices[a].Normal += normal; tempVertices[b].Normal += normal; tempVertices[c].Normal += normal; } for (int i = 0; i < tempVertices.Length; i++) { tempVertices[i].Normal.Normalize(); vertices.Add(tempVertices[i]); } }
///<summary> /// Constructs a new morphable entity. ///</summary> ///<param name="collisionInformation">Collidable to use with the entity.</param> ///<param name="mass">Mass of the entity.</param> /// <param name="inertiaTensor">Inertia tensor of the entity.</param> /// <param name="volume">Volume of the entity.</param> public MorphableEntity(EntityCollidable collisionInformation, float mass, Matrix3X3 inertiaTensor, float volume) : base(collisionInformation, mass, inertiaTensor, volume) { }
///<summary> /// Constructs a new morphable entity. ///</summary> ///<param name="collisionInformation">Collidable to use with the entity.</param> ///<param name="mass">Mass of the entity.</param> public MorphableEntity(EntityCollidable collisionInformation, float mass) : base(collisionInformation, mass) { }
///<summary> /// Constructs a new morphable entity. ///</summary> ///<param name="collisionInformation">Collidable to use with the entity.</param> public MorphableEntity(EntityCollidable collisionInformation) : base(collisionInformation) { }
/// <summary> /// Sets the collision information of the entity to another collidable. /// </summary> /// <param name="newCollisionInformation">New collidable to use.</param> /// <param name="newMass">New mass to use for the entity.</param> /// <param name="newInertia">New inertia tensor to use for the entity.</param> public void SetCollisionInformation(EntityCollidable newCollisionInformation, float newMass, Matrix3X3 newInertia) { //Temporarily remove the object from the space. //The reset process will update any systems that need to be updated. //This is not thread safe, but this operation should not be performed mid-frame anyway. ISpace space = Space; if (space != null) Space.Remove(this); CollisionInformation.Entity = null; Initialize(newCollisionInformation, newMass, newInertia); if (space != null) space.Add(this); }
void QueryContacts(Vector3 position, EntityCollidable queryObject) { ClearContacts(); //Update the position and orientation of the query object. RigidTransform transform; transform.Position = position; transform.Orientation = character.Body.Orientation; queryObject.UpdateBoundingBoxForTransform(ref transform, 0); foreach (var collidable in character.Body.CollisionInformation.OverlappedCollidables) { if (collidable.BoundingBox.Intersects(queryObject.BoundingBox)) { var pair = new CollidablePair(collidable, queryObject); var pairHandler = NarrowPhaseHelper.GetPairHandler(ref pair); if (pairHandler.CollisionRule == CollisionRule.Normal) { pairHandler.UpdateCollision(0); foreach (var contact in pairHandler.Contacts) { //Must check per-contact collision rules, just in case //the pair was actually a 'parent pair.' if (contact.Pair.CollisionRule == CollisionRule.Normal) { ContactData contactData; contactData.Position = contact.Contact.Position; contactData.Normal = contact.Contact.Normal; contactData.Id = contact.Contact.Id; contactData.PenetrationDepth = contact.Contact.PenetrationDepth; contacts.Add(contactData); } } } //TODO: It would be nice if this was a bit easier. //Having to remember to clean up AND give it back is a bit weird, especially with the property-diving. //No one would ever just guess this correctly. //At least hide it behind a NarrowPhaseHelper function. pairHandler.CleanUp(); pairHandler.Factory.GiveBack(pairHandler); } } CategorizeContacts(ref position); }
protected void onCollision(EntityCollidable sender, Collidable other, CollidablePairHandler pair) { if(!(other.Tag is Box)) return; joint.Motor.Settings.Servo.Goal = -0.7f; if(!pressed) MediaSystem.PlaySoundEffect(SFXOptions.Machine_Button_Press); pressed = true; }