public void setDepth(int depth) { CollisionCategory newCat = PhysicsHelper.depthToCollisionCategory(depth); geom.CollisionCategories = newCat; geom.CollidesWith = newCat; }
internal Fixture(Body body, Shape shape, float density) { //Fixture defaults Friction = 0.2f; _collisionCategories = CollisionCategory.All; _collidesWith = CollisionCategory.All; IsSensor = false; Body = body; #if ConserveMemory Shape = shape; #else Shape = shape.Clone(); #endif // Reserve proxy space int childCount = Shape.ChildCount; Proxies = new FixtureProxy[childCount]; for (int i = 0; i < childCount; ++i) { Proxies[i] = new FixtureProxy(); Proxies[i].Fixture = null; Proxies[i].ProxyId = BroadPhase.NullProxy; } ProxyCount = 0; Density = density; FixtureId = _fixtureIdCounter++; }
//use the following constructor when wanting to create collisdable groups of sprites public sprite(theGame reference, String contentAssetName, float width, float height, float X, float Y, CollisionCategory collCat,CollisionCategory collWith) { asset = reference.Content.Load<Texture2D>(contentAssetName); spriteOrigin = new Vector2(asset.Width / 2f, asset.Height / 2f); this.X = X; this.Y = Y; this.width = width; this.height = height; rectangle = new Rectangle((int)this.X, (int)this.Y, (int)width, (int)height); this.identity = identity; this.reference = reference; rectBody = BodyFactory.Instance.CreateRectangleBody(reference.ps, width, height, 1);//PHYSICS rectBody.Position = new FarseerGames.FarseerPhysics.Mathematics.Vector2(this.X, this.Y);//PHYSICS rectGeom = GeomFactory.Instance.CreateRectangleGeom(reference.ps, rectBody, this.width, this.height);//PHYSICS rectGeom.SetBody(rectBody); rectGeom.CollisionEnabled = true; this.rectGeom.CollisionResponseEnabled = true; this.rectGeom.CollisionCategories = collCat; this.rectGeom.CollidesWith = collWith; //this.rectGeom.CollisionCategories = CollisionCategory. rectBody.Enabled = true;//PHYSICS rectGeom.OnSeparation += OnSeperation; rectGeom.OnCollision += OnCollision; reference.ps.BroadPhaseCollider.OnBroadPhaseCollision += OnBroadPhaseCollision; reference.ps.Add(rectBody); reference.ps.Add(rectGeom); }
public Fixture(Body body, Shape shape) { //Fixture defaults Friction = 0.2f; Restitution = 0; _collisionCategories = CollisionCategory.Cat1; _collidesWith = CollisionCategory.All; _collisionGroup = 0; IsSensor = false; Body = body; if (Settings.ConserveMemory) { Shape = shape; } else { Shape = shape.Clone(); } // Reserve proxy space int childCount = Shape.ChildCount; Proxies = new FixtureProxy[childCount]; for (int i = 0; i < childCount; ++i) { Proxies[i] = new FixtureProxy(); Proxies[i].Fixture = null; Proxies[i].ProxyId = BroadPhase.NullProxy; } ProxyCount = 0; FixtureId = _fixtureIdCounter++; if ((Body.Flags & BodyFlags.Active) == BodyFlags.Active) { BroadPhase broadPhase = Body.World.ContactManager.BroadPhase; CreateProxies(broadPhase, ref Body.Xf); } Body.FixtureList.Add(this); // Adjust mass properties if needed. if (Shape._density > 0.0f) { Body.ResetMassData(); } // Let the world know we have a new fixture. This will cause new contacts // to be created at the beginning of the next time step. Body.World.Flags |= WorldFlags.NewFixture; if (Body.World.FixtureAdded != null) { Body.World.FixtureAdded(this); } }
public LocalPlayer(Game game, long sessionID, int id, string imageAssetPath, Vector2 position, float angle, PhysicsSimulator physicsSimulator, float speed, float mass, CollisionCategory collisionCategories, short index, KeyboardControls controls, ProjectileFactory projectileFactory) : base(game, sessionID, id, imageAssetPath, position, angle, physicsSimulator, speed, mass, collisionCategories, index) { Controls = controls; this.projectileFactory = projectileFactory; Projectiles = new List<ProjectileLocal>(); Geometry.OnCollision += OnCollision; Body.LinearDragCoefficient = 100; Body.RotationalDragCoefficient = 6000; }
/// <summary> /// Creates rectangular geoms that match the size of the bodies. /// Then adds the geometries to the given physics simulator. /// </summary> /// <param name="collisionCategory">The collision category of the geometries.</param> /// <param name="collidesWith">The collisioncategory the geometries should collide with..</param> /// <param name="physicsSimulator">The physics simulator.</param> public void CreateGeoms(CollisionCategory collisionCategory, CollisionCategory collidesWith, PhysicsSimulator physicsSimulator) { CreateGeoms(collisionCategory, collidesWith); foreach (Geom geom in _geoms) { physicsSimulator.Add(geom); } }
public PhysicsBody(GameObject owner, IBox body, CollisionCategory collisionCategory = CollisionCategory.dynamic) { Owner = owner; BoxCollider = body; Category = collisionCategory; ChildHitboxes = new Dictionary <string, Hitbox>(); HitboxesToRemove = new List <string>(); CurrentCollisions = new List <Hitbox>(); }
/// <summary> /// Creates rectangular geoms that match the size of the bodies. /// </summary> /// <param name="collisionCategory">The collision category.</param> /// <param name="collidesWith">What collision group geometries collides with.</param> public void CreateGeoms(CollisionCategory collisionCategory, CollisionCategory collidesWith) { foreach (Body body in _bodies) { Geom geom = GeomFactory.Instance.CreateRectangleGeom(body, _width, _height); geom.CollisionCategories = collisionCategory; geom.CollidesWith = collidesWith; _geoms.Add(geom); } }
static CollisionCategory CollidesWith(CollisionCategory categories) { ///Categories: /// 1: Local Player /// 2: Remote Players /// 3: Local Projecties /// 4: Remote Projectiles switch (categories) { case CollisionCategory.Cat1: return CollisionCategory.All & ~CollisionCategory.Cat3; case CollisionCategory.Cat2: return CollisionCategory.All & ~CollisionCategory.Cat4; case CollisionCategory.Cat3: return CollisionCategory.All & ~CollisionCategory.Cat1; case CollisionCategory.Cat4: return CollisionCategory.All & ~CollisionCategory.Cat2; default: return CollisionCategory.All; } }
public PhysicsGameObject(Game game, long sessionID, int id, string imageAssetPath, Vector2 position, float angle, PhysicsSimulator physicsSimulator, float speed, float mass, CollisionCategory collisionCategories) : base(game, sessionID, id, imageAssetPath) { PhysicsSimulator = physicsSimulator; Speed = speed; Mass = mass; uint[] colorData = new uint[Texture.Width * Texture.Height]; Texture.GetData(colorData); Vertices vertices = Vertices.CreatePolygon(colorData, Texture.Width, Texture.Height); Origin = vertices.GetCentroid(); Body = BodyFactory.Instance.CreatePolygonBody(PhysicsSimulator, vertices, mass); Position = position; Angle = angle; Geometry = GeomFactory.Instance.CreatePolygonGeom(physicsSimulator, Body, vertices, 0); Geometry.CollisionCategories = collisionCategories; Geometry.CollidesWith = CollidesWith(collisionCategories); }
public static int collisionCategoryToDepth(CollisionCategory category) { switch (category) { case CollisionCategory.Cat1: return(0); case CollisionCategory.Cat2: return(1); case CollisionCategory.Cat3: return(2); case CollisionCategory.Cat4: return(3); } return(-1); }
private void ConstructClone(Body bodyToSet, Geom geometry, Vector2 offset, float rotationOffset) { Id = GetNextId(); _collisionGridCellSize = geometry._collisionGridCellSize; grid = geometry.grid.Clone(); restitutionCoefficient = geometry.restitutionCoefficient; frictionCoefficient = geometry.frictionCoefficient; collisionGroup = geometry.collisionGroup; collisionEnabled = geometry.collisionEnabled; collisionResponseEnabled = geometry.collisionResponseEnabled; _collisionGridCellSize = geometry._collisionGridCellSize; _offset = offset; _rotationOffset = rotationOffset; collisionCategories = geometry.collisionCategories; collidesWith = geometry.collidesWith; SetVertices(geometry.localVertices); SetBody(bodyToSet); }
private void ConstructClone(Body bodyToSet, Geom geometry, Vector2 positionOffset, float rotationOffset) { id = GetNextId(); _positionOffset = positionOffset; _rotationOffset = rotationOffset; RestitutionCoefficient = geometry.RestitutionCoefficient; FrictionCoefficient = geometry.FrictionCoefficient; GridCellSize = geometry.GridCellSize; //IsSensor = geometry.IsSensor; CollisionGroup = geometry.CollisionGroup; CollisionEnabled = geometry.CollisionEnabled; CollisionResponseEnabled = geometry.CollisionResponseEnabled; CollisionCategories = geometry.CollisionCategories; CollidesWith = geometry.CollidesWith; SetVertices(geometry.localVertices); SetBody(bodyToSet); //Make sure that the clone also gets the associated distance grid if (PhysicsSimulator.NarrowPhaseCollider == NarrowPhaseCollider.DistanceGrid) { DistanceGrid.Instance.Copy(geometry.id, id); } }
public ProjectileLocal(Game game, long sessionID, int id, string imageAssetPath, Vector2 position, float angle, PhysicsSimulator physicsSimulator, float speed, float mass, CollisionCategory collisionCategories) : base(game, sessionID, id, imageAssetPath, position, angle, physicsSimulator, speed, mass, collisionCategories) { Geometry.OnCollision += OnCollision; //Body.ApplyForce((Position + Velocity)); }
protected Player(Game game, long sessionID, int id, string imageAssetPath, Vector2 position, float angle, PhysicsSimulator physicsSimulator, float speed, float mass, CollisionCategory collisionCategories, short index) : base(game, sessionID, id, imageAssetPath, position, angle, physicsSimulator, speed, mass, collisionCategories) { Index = index; }
public ProjectileRemote(Game game, long sessionID, int id, string imageAssetPath, Vector2 position, float angle, PhysicsSimulator physicsSimulator, float speed, float mass, CollisionCategory collisionCategories) : base(game, sessionID, id, imageAssetPath, position, angle, physicsSimulator, speed, mass, collisionCategories) { }
//use the following constructor when wanting to create collisdable groups of sprites public sprite(theGame reference, String contentAssetName, float width, float height, float X, float Y, CollisionCategory collCat, CollisionCategory collWith) { asset = reference.Content.Load <Texture2D>(contentAssetName); spriteOrigin = new Vector2(asset.Width / 2f, asset.Height / 2f); this.X = X; this.Y = Y; this.width = width; this.height = height; rectangle = new Rectangle((int)this.X, (int)this.Y, (int)width, (int)height); this.identity = identity; this.reference = reference; rectBody = BodyFactory.Instance.CreateRectangleBody(reference.ps, width, height, 1); //PHYSICS rectBody.Position = new FarseerGames.FarseerPhysics.Mathematics.Vector2(this.X, this.Y); //PHYSICS rectGeom = GeomFactory.Instance.CreateRectangleGeom(reference.ps, rectBody, this.width, this.height); //PHYSICS rectGeom.SetBody(rectBody); rectGeom.CollisionEnabled = true; this.rectGeom.CollisionResponseEnabled = true; this.rectGeom.CollisionCategories = collCat; this.rectGeom.CollidesWith = collWith; //this.rectGeom.CollisionCategories = CollisionCategory. rectBody.Enabled = true;//PHYSICS rectGeom.OnSeparation += OnSeperation; rectGeom.OnCollision += OnCollision; reference.ps.BroadPhaseCollider.OnBroadPhaseCollision += OnBroadPhaseCollision; reference.ps.Add(rectBody); reference.ps.Add(rectGeom); }