public DynamicBody(Vector2 position, ICollisionShape shape) : base(position, shape) { destination = null; direction = null; MovementSpeed = 3f; InverseMass = 1f; }
public StaticBody AddStaticBody(Vector2 position, ICollisionShape shape) { var body = new StaticBody(position, shape); bodies.Add(body); return(body); }
public bool CollidesWith(ICollisionShape otherShape) { if (otherShape is CollisionSet) { return(otherShape.CollidesWith(this)); } var circleShape = otherShape as CollisionCircle; if (circleShape != null) { return(CollidesWith(circleShape.circle)); } var rectShape = otherShape as CollisionRect; if (rectShape != null) { return(CollidesWith(rectShape.rectangle)); } var pointShape = otherShape as CollisionPoint; if (pointShape != null) { return(circle.Contains(pointShape.Position)); } return(false); }
private void Awake() { var collider = GetComponent <BoxCollider>(); _shape = new BoxShape(collider.bounds, false); _shape.Center = transform.position; }
public bool CollideWith(ICollisionShape other) { if (other is PointCollisionShape) { return(CollisionHelper.IntersectsPointAndLine(Start, End, (other as PointCollisionShape).Center)); } if (other is SphereCollisionShape) { return(CollisionHelper.IntersectCircleAndLine(Start, End, (other as SphereCollisionShape).Center, (other as SphereCollisionShape).Radius)); } if (other is SquareCollisionShape) { return(CollisionHelper.IntersectLineAndSquare(Start, End, (other as SquareCollisionShape).Position, (other as SquareCollisionShape).Size)); } if (other is LineCollisionShape) { return(CollisionHelper.IntersectLineAndLine(Start, End, (other as LineCollisionShape).Start, (other as LineCollisionShape).End)); } return(false); }
public static bool Collide(ICollisionShape s1, ICollisionShape s2) { if (s1 is CompoundCollisionShape) { if (s2 is CompoundCollisionShape) return Collide(s1 as CompoundCollisionShape, s2 as CompoundCollisionShape); if (s2 is RectangleCollisionShape) return Collide(s2 as RectangleCollisionShape, s1 as CompoundCollisionShape); if (s2 is CircleCollisionShape) return Collide(s2 as CircleCollisionShape, s1 as CompoundCollisionShape); } if (s1 is RectangleCollisionShape) { if (s2 is CompoundCollisionShape) return Collide(s1 as RectangleCollisionShape, s2 as CompoundCollisionShape); if (s2 is RectangleCollisionShape) return Collide(s1 as RectangleCollisionShape, s2 as RectangleCollisionShape); if (s2 is CircleCollisionShape) return Collide(s1 as RectangleCollisionShape, s2 as CircleCollisionShape); } if (s1 is CircleCollisionShape) { if (s2 is CompoundCollisionShape) return Collide(s1 as CircleCollisionShape, s2 as CompoundCollisionShape); if (s2 is RectangleCollisionShape) return Collide(s2 as RectangleCollisionShape, s1 as CircleCollisionShape); if (s2 is CircleCollisionShape) return Collide(s1 as CircleCollisionShape, s2 as CircleCollisionShape); } throw new Exception("Collision Shape unknown"); }
public bool CollideWith(ICollisionShape other) { if (other is PointCollisionShape) { return(CollisionHelper.IntersectsPointAndSquare(((PointCollisionShape)other).Center, Position, Size)); } if (other is SphereCollisionShape) { return(CollisionHelper.IntersectsCircleAndSquare((other as SphereCollisionShape).Center, (other as SphereCollisionShape).Radius, Position, Size)); } if (other is SquareCollisionShape) { return(CollisionHelper.IntersectsSquareAndSquare(GetVertices(), (other as SquareCollisionShape).GetVertices())); } if (other is LineCollisionShape) { return(CollisionHelper.IntersectLineAndSquare((other as LineCollisionShape).Start, (other as LineCollisionShape).End, Position, Size)); } return(false); }
public void SetPhysics(ICollisionShape shape) { if (gameObj == null) { return; } gameObj.GetComponent <CollisionRenderer>().Shape = shape; }
public bool CollidesWith(ICollisionShape otherShape) { if (otherShape is CollisionPoint) { var otherPoint = otherShape as CollisionPoint; return(Vector2.Distance(Position, otherPoint.Position) < MIN_COLLISION_DIST); } return(otherShape.CollidesWith(this)); }
public void AddCollisionShape(ICollisionShape collisionShape) { var primitive = collisionShape as Primitive; if (primitive == null) throw new ArgumentException("Invalid collision shape type.", "collisionShape"); var material = new MaterialProperties( collisionShape.Restitution, collisionShape.StaticFriction, collisionShape.DynamicFriction); AddPrimitive(primitive, material); }
protected override void InitializeProperties(ICollisionShape instance, Actor actor, CollisionShapeConfig config) { var box = instance as IBoxCollisionShape; var boundingBox = actor.ActorModel.LocalBoundingBox; box.Position = boundingBox.Min; box.SideLengths = boundingBox.GetSideLengths(); base.InitializeProperties(instance, actor, config); }
protected override void InitializeProperties(ICollisionShape instance, Actor actor, CollisionShapeConfig config) { var concreteShape = instance as ICubeHeightmapCollisionShape; concreteShape.Position = actor.Position; var concreteActor = actor as TerrainActor; var actorModel = concreteActor.ActorModel as CubeTerrainActorModel; concreteShape.Heightmap = actorModel.Terrain.Heightmap; base.InitializeProperties(instance, actor, config); }
public bool CollidesWith(ICollisionShape otherShape) { foreach (var subShape in subShapes) { if (subShape.CollidesWith(otherShape)) { return(true); } } return(false); }
public CollisionJoint( ICollisionShape shapeA, ICollisionShape shapeB, double restoreCoeff, double springCoeff) { ShapeA = shapeA; ShapeB = shapeB; KeyIndex = GetHashCode(); SpringCoefficient = springCoefficient; RestoreCoefficient = restoreCoefficient; }
public FixedJoint( ICollisionShape shapeA, ICollisionShape shapeB, double restoreCoeff, double springCoeff) { fixedJointConstraint = new FixedJointConstraint( ((IMapper)shapeA).GetShape(), ((IMapper)shapeB).GetShape(), restoreCoeff, springCoeff); }
public bool CollidesWith(ICollisionShape collisionShape) { if (collisionShape != null) { if (collisionShape.CollisionShapeType == CollisionShapeTypes.Rectangle) { CollisionRect collRect = (CollisionRect)collisionShape; return(Rect.Intersects(collRect.Rect)); } } return(false); }
public bool CollidesWith(ICollisionShape otherShape) { if (otherShape is CollisionRect) { var otherRect = otherShape as CollisionRect; return(CollidesWith(((CollisionRect)otherShape).rectangle)); } if (otherShape is CollisionPoint) { return(rectangle.Contains(((CollisionPoint)otherShape).Position)); } return(otherShape.CollidesWith(this)); }
protected override void InitializeProperties(ICollisionShape instance, Actor actor, CollisionShapeConfig config) { var concreteShape = instance as IMeshCollisionShape; var modelActorModel = actor.ActorModel as ModelActorModel; if (modelActorModel == null) { throw new InvalidOperationException( "IMeshCollisionShape must be bound to the actor with ModelActorModel."); } concreteShape.CreateMesh(modelActorModel.Model); base.InitializeProperties(instance, actor, config); }
public BallAndSocketJoint( ICollisionShape shapeA, ICollisionShape shapeB, Vector3d startAnchorPos, double restoreCoeff, double springCoeff) { ballAndSocketConstraint = new BallAndSocketConstraint( ((IMapper)shapeA).GetShape(), ((IMapper)shapeB).GetShape(), startAnchorPos, restoreCoeff, springCoeff); }
///// Methods ///// public CollisionResult TestCollision(ICollisionShape other) { var result = new CollisionResult(); if (other is BoxShape) { result.Collides = BoxVsBox(this, (BoxShape)other, ref result, TwoD); } else { Debug.LogErrorFormat("Collision test not implemented: {0}-{1}", GetType(), other.GetType()); } return(result); }
public HingeJoint( ICollisionShape shapeA, ICollisionShape shapeB, Vector3d startAnchorPosition, Vector3d hingeAxis, double restoreCoefficient, double springCoefficient) { hingeConstraint = new HingeConstraint( ((IMapper)shapeA).GetShape(), ((IMapper)shapeB).GetShape(), startAnchorPosition, hingeAxis, restoreCoefficient, springCoefficient); }
public SliderJoint( ICollisionShape shapeA, ICollisionShape shapeB, Vector3d startAnchorPosition, Vector3d sliderAxis, double restoreCoefficient, double springCoefficient) { sliderConstraint = new SliderConstraint( ((IMapper)shapeA).GetShape(), ((IMapper)shapeB).GetShape(), startAnchorPosition, sliderAxis, restoreCoefficient, springCoefficient); }
public PistonJoint( ICollisionShape shapeA, ICollisionShape shapeB, Vector3d startAnchorPosition, Vector3d pistonAxis, double restoreCoefficient, double springCoefficient) { pistonConstraint = new PistonConstraint( ((IMapper)shapeA).GetShape(), ((IMapper)shapeB).GetShape(), startAnchorPosition, pistonAxis, restoreCoefficient, springCoefficient); }
public UniversalJoint( ICollisionShape shapeA, ICollisionShape shapeB, Vector3d startAnchorPosition, Vector3d hingeAxis, Vector3d rotationAxis, double restoreCoefficient, double springCoefficient) { universalConstraint = new UniversalConstraint( ((IMapper)shapeA).GetShape(), ((IMapper)shapeB).GetShape(), startAnchorPosition, hingeAxis, rotationAxis, restoreCoefficient, springCoefficient); }
public AngularJoint( ICollisionShape shapeA, ICollisionShape shapeB, Vector3d startAnchorPos, Vector3d hingeAxis, Vector3d rotationAxis, double restoreCoefficient, double springCoefficientHingeAxis, double springCoefficientRotationAxis) { angularConstraint = new AngularConstraint( ((IMapper)shapeA).GetShape(), ((IMapper)shapeB).GetShape(), startAnchorPos, hingeAxis, rotationAxis, restoreCoefficient, springCoefficientHingeAxis, springCoefficientRotationAxis); }
/// <summary> /// Create and return a random shape object. /// </summary> private GameObject CreateRandomShape() { // tilemap actual size float tilemapActualSize = tilemapSize * tileSize; // collision shape and type ICollisionShape collisionShape = null; ShapeMeshes shapeType = ShapeMeshes.Cylinder; // random collision shape and type int randType = rand.Next(0, 2); switch (randType) { case 0: shapeType = ShapeMeshes.SphereSmooth; collisionShape = new CollisionSphere(); break; case 1: shapeType = ShapeMeshes.Cube; collisionShape = new CollisionBox(2f, 2f, 2f); break; } // create shape float sizeFactor = 1.0f + (float)rand.NextDouble() * 1.25f; GameObject shape = new GameObject(); var shapeRenderer = shape.AddComponent(new ShapeRenderer(shapeType)) as ShapeRenderer; shapeRenderer.MaterialOverride.DiffuseColor = new Color((rand.Next(0, 255)), (rand.Next(0, 255)), (rand.Next(0, 255))); var body = shape.AddComponent(new RigidBody(collisionShape, sizeFactor, 1f, 1f)) as RigidBody; body.Scale = Vector3.One * tileSize * 0.25f * sizeFactor; body.Restitution = 0.5f; body.Position = new Vector3( (float)rand.NextDouble() * tilemapActualSize, 10f + (float)rand.NextDouble() * 45f, (float)rand.NextDouble() * tilemapActualSize); return(shape); }
public void AddShape(ICollisionShape simulationObject) { CollisionShapes.Add(simulationObject); IShape simObj = ((IMapper)simulationObject).GetShape(); if (simObj is ISoftShape softShape) { softShape.SetID(HsGenerator.GetHash()); foreach (var point in softShape.ShapePoints) { point.SetID(HsGenerator.GetHash()); } } else { simObj.SetID(HsGenerator.GetHash()); } if (Shapes != null && Shapes.Length > 0) { var bufferList = Shapes.ToList(); bufferList.Add(simObj); Shapes = bufferList.ToArray(); } else { var bufferList = new List <IShape> { simObj }; Shapes = bufferList.ToArray(); } SoftShapes = Shapes.Where(x => (x as ISoftShape) != null) .Cast <ISoftShape>() .ToArray(); CollisionEngine.AddShape(simObj); }
public Hinge2Joint( ICollisionShape shapeA, ICollisionShape shapeB, ICollisionShape externalSyncShape, Vector3d startAnchorPosition, Vector3d hingeAxis, Vector3d rotationAxis, double restoreCoefficient, double springCoefficientHingeAxis, double springCoefficient) { hinge2Constraint = new Hinge2Constraint( ((IMapper)shapeA).GetShape(), ((IMapper)shapeB).GetShape(), ((IMapper)externalSyncShape).GetShape(), startAnchorPosition, hingeAxis, rotationAxis, restoreCoefficient, springCoefficientHingeAxis, springCoefficient); }
/// <summary> /// Test if a finite line segment intersects a collider /// </summary> /// <param name="collider">Collider to test</param> /// <param name="start">Start point of line</param> /// <param name="end">End point of line</param> /// <returns>True if line intersects collider</returns> public static bool SegmentIntersects(ICollisionShape collider, Vector3 start, Vector3 end) { Vector3 d = (end - start) * 0.5f; Vector3 e = (collider.Max - collider.Min) * 0.5f; Vector3 c = start + d - (collider.Min + collider.Max) * 0.5f; var ad = new Vector3(Mathf.Abs(d.x), Mathf.Abs(d.y), Mathf.Abs(d.z)); if (Mathf.Abs(c.x) > e.x + ad.x) { return(false); } if (Mathf.Abs(c.y) > e.y + ad.y) { return(false); } if (Mathf.Abs(c.z) > e.z + ad.z) { return(false); } if (Mathf.Abs(d.y * c.z - d.z * c.y) > e.y * ad.z + e.z * ad.y + Mathf.Epsilon) { return(false); } if (Mathf.Abs(d.z * c.x - d.x * c.z) > e.z * ad.x + e.x * ad.z + Mathf.Epsilon) { return(false); } if (Mathf.Abs(d.x * c.y - d.y * c.x) > e.x * ad.y + e.y * ad.x + Mathf.Epsilon) { return(false); } return(true); }
public virtual void AddCollisionShape(ICollisionShape collisionShape) { }
public bool Intersects(ICollisionShape other, out Vector3 point) { throw new NotImplementedException(); }
public ICollisionShape[] LoadSimulationObjects() { var xmlDoc = new XmlDocument(); xmlDoc.Load(FileNameObjectProperties); XmlNodeList xmlList = xmlDoc.SelectNodes(nodePathObjects); ICollisionShape[] objects = new ICollisionShape[xmlList.Count]; for (int i = 0; i < xmlList.Count; i++) { //Rotation Status var versor = new Vector3d( Convert.ToDouble(xmlList[i][rotationStatusAttribute].Attributes["x"].Value), Convert.ToDouble(xmlList[i][rotationStatusAttribute].Attributes["y"].Value), Convert.ToDouble(xmlList[i][rotationStatusAttribute].Attributes["z"].Value)); double angle = Convert.ToDouble(xmlList[i][rotationStatusAttribute].Attributes["angle"].Value); //Position var position = new Vector3d( Convert.ToDouble(xmlList[i][positionAttribute].Attributes["x"].Value), Convert.ToDouble(xmlList[i][positionAttribute].Attributes["y"].Value), Convert.ToDouble(xmlList[i][positionAttribute].Attributes["z"].Value)); XmlNodeList xmlGeometryList = xmlList[i].SelectNodes(nodePathGeometry); GeometryProperties[] objGeometry = new GeometryProperties[xmlGeometryList.Count]; double[] mass = new double[xmlGeometryList.Count]; Vector3d[] startCompositePosition = new Vector3d[xmlGeometryList.Count]; for (int j = 0; j < xmlGeometryList.Count; j++) { //Scale float scale = Convert.ToSingle(xmlGeometryList[j][scaleAttribute].InnerText); //Object geometry file name string geometryFileName = xmlGeometryList[j][objectGeometryAttribute].InnerText; //Object mass mass[j] = Convert.ToDouble(xmlGeometryList[j][massAttribute].InnerText); objGeometry[j] = GetObjectGeometry(geometryFileName, scale, mass[j]); startCompositePosition[j] = new Vector3d( Convert.ToDouble(xmlGeometryList[j][compositePosition].Attributes["x"].Value), Convert.ToDouble(xmlGeometryList[j][compositePosition].Attributes["y"].Value), Convert.ToDouble(xmlGeometryList[j][compositePosition].Attributes["z"].Value)); } if (xmlGeometryList.Count > 1) { objects[i] = new CompoundRigidShape( objGeometry.Select(x => x.VertexPoint).ToList(), objGeometry.Select(x => x.TriagleIdx).ToList(), startCompositePosition, mass); } else { if ((ObjectType)Convert.ToInt32(xmlList[i][objectType].InnerText) == ObjectType.RigidBody) { objects[i] = new ConvexShape(objGeometry[0].VertexPoint, objGeometry[0].TriagleIdx, position, mass[0]); } else if (Convert.ToInt32(xmlList[i][objectType].InnerText) == 1) { objects[i] = new ConvexShape(objGeometry[0].VertexPoint, objGeometry[0].TriagleIdx, position, mass[0], true); } } objects[i].SetPosition(position); objects[i].SetRotationStatus(new Quaternion(versor, angle)); //Linear Velocity objects [i].SetLinearVelocity(new Vector3d( Convert.ToDouble(xmlList [i] [linearVelAttribute].Attributes ["x"].Value), Convert.ToDouble(xmlList [i] [linearVelAttribute].Attributes ["y"].Value), Convert.ToDouble(xmlList [i] [linearVelAttribute].Attributes ["z"].Value))); //Angular Velocity objects [i].SetAngularVelocity(new Vector3d( Convert.ToDouble(xmlList [i] [angularVelAttribute].Attributes ["x"].Value), Convert.ToDouble(xmlList [i] [angularVelAttribute].Attributes ["y"].Value), Convert.ToDouble(xmlList [i] [angularVelAttribute].Attributes ["z"].Value))); //Restitution Coefficient objects [i].SetRestitutionCoeff(Convert.ToDouble(xmlList [i] [restitutionCoeffAttribute].InnerText)); //Dynamic friction objects[i].SetDynamicFrictionCoeff(Convert.ToDouble(xmlList [i][dynamicFrictionAttribute].InnerText)); //Static friction objects [i].SetStaticFrictionCoeff(Convert.ToDouble(xmlList [i] [staticFrictionAttribute].InnerText)); //Collision detection objects[i].ExcludeFromCollisionDetection(Convert.ToBoolean(xmlList [i] [excludeFromCollisionDetection].InnerText)); //Baumgarte Stabilization value objects[i].SetErrorReductionParam(0.5); } return(objects); }
private static bool TestCollisionShapes(ICollisionShape a, ICollisionShape b, ref CollisionResult result) { result = a.TestCollision(b); return(result.Collides); }
protected virtual void InitializeProperties(ICollisionShape instance, Actor actor, CollisionShapeConfig config) { instance.StaticFriction = config.StaticFriction; instance.DynamicFriction = config.DynamicFriction; instance.Restitution = config.Restitution; }
public void AddCollisionShape(ICollisionShape shape) { _shapes.Add(shape); }
public override Vector2 Destination => Position; // the body only wants to be where it already is... because it's already there public StaticBody(Vector2 position, ICollisionShape shape) : base(position, shape) { InverseMass = 0f; MovementSpeed = 0f; }
private ICollisionShape[] getSphereObject() { ICollisionShape[] objects = new ICollisionShape[4]; #region Terrain Base ShapeFilename[0] = new string[1] { "cube1.obj" }; ShapeScale[0] = new float[1] { 25 }; TextureFilename[0] = new string[1] { "texture/woodbox.bmp" }; GeometryProperties geom0 = GetObjectGeometry(ShapeFilename[0][0], ShapeScale[0][0]); objects[0] = new ConvexShape( geom0.VertexPoint, geom0.TriagleIdx, new Vector3d(0.0, -4.0, 0.0), 0.0, true); objects[0].SetRotationStatus(new Quaternion(new Vector3d(0.0, 0.0, 0.0), 0.0)); objects[0].SetLinearVelocity(new Vector3d(0.0, 0.0, 0.0)); objects[0].SetAngularVelocity(new Vector3d(0.0, 0.0, 0.0)); objects[0].SetRestitutionCoeff(0.1); objects[0].SetDynamicFrictionCoeff(1.0); objects[0].SetStaticFrictionCoeff(1.0); objects[0].ExcludeFromCollisionDetection(false); objects[0].SetErrorReductionParam(30.0); #endregion ShapeFilename[1] = new string[1] { "sphere.obj" }; ShapeScale[1] = new float[1] { 1 }; TextureFilename[1] = new string[1] { "texture/woodbox.bmp" }; GeometryProperties geom1 = GetObjectGeometry(ShapeFilename[1][0], ShapeScale[1][0]); objects[1] = new ConvexShape(geom1.VertexPoint, geom1.TriagleIdx, new Vector3d(3.0, 4.0, 7.0), 1.0); objects[1].SetRotationStatus(new Quaternion(1.0, 0.0, 0.0, 0.0)); objects[1].SetLinearVelocity(new Vector3d(0.0, 0.0, 0.0)); objects[1].SetAngularVelocity(new Vector3d(0.0, 0.0, 0.0)); objects[1].SetRestitutionCoeff(0.1); objects[1].SetDynamicFrictionCoeff(0.8); objects[1].SetStaticFrictionCoeff(0.9); objects[1].ExcludeFromCollisionDetection(false); objects[1].SetErrorReductionParam(30.0); ShapeFilename[2] = new string[1] { "sphere.obj" }; ShapeScale[2] = new float[1] { 1 }; TextureFilename[2] = new string[1] { "texture/woodbox.bmp" }; GeometryProperties geom2 = GetObjectGeometry(ShapeFilename[2][0], ShapeScale[2][0]); objects[2] = new ConvexShape(geom2.VertexPoint, geom2.TriagleIdx, new Vector3d(0.0, 1.2, 7.0), 1.0); objects[2].SetRotationStatus(new Quaternion(1.0, 0.0, 0.0, 0.0)); objects[2].SetLinearVelocity(new Vector3d(0.0, 0.0, 0.0)); objects[2].SetAngularVelocity(new Vector3d(0.0, 0.0, 0.0)); objects[2].SetRestitutionCoeff(0.1); objects[2].SetDynamicFrictionCoeff(0.8); objects[2].SetStaticFrictionCoeff(0.9); objects[2].ExcludeFromCollisionDetection(false); objects[2].SetErrorReductionParam(30.0); ShapeFilename[3] = new string[1] { "sphere.obj" }; ShapeScale[3] = new float[1] { 1 }; TextureFilename[3] = new string[1] { "texture/woodbox.bmp" }; GeometryProperties geom3 = GetObjectGeometry(ShapeFilename[3][0], ShapeScale[3][0]); objects[3] = new ConvexShape(geom3.VertexPoint, geom3.TriagleIdx, new Vector3d(-3.0, 1.2, 7.0), 1.0); objects[3].SetRotationStatus(new Quaternion(1.0, 0.0, 0.0, 0.0)); objects[3].SetLinearVelocity(new Vector3d(0.0, 0.0, 0.0)); objects[3].SetAngularVelocity(new Vector3d(0.0, 0.0, 0.0)); objects[3].SetRestitutionCoeff(0.5); objects[3].SetDynamicFrictionCoeff(0.8); objects[3].SetStaticFrictionCoeff(0.9); objects[3].ExcludeFromCollisionDetection(false); objects[3].SetErrorReductionParam(30.0); return(objects); }