Exemplo n.º 1
0
        public IBody CreateBody(Blob blob, float radius, float mass, bool isStatic)
        {
            lock (UpdateLock)
            {
                var body = isStatic ? cpBody.NewStatic() : cpBody.New(mass, float.PositiveInfinity);
                body.SetUserData(blob);

                var shape = new cpCircleShape(body, radius, cpVect.Zero);
                shape.SetCollisionType(GenerateCollisionType(blob.GetType()));

                lock (ShapesListLock)
                {
                    _shapesToAdd.Add(shape);
                }

                if (_space.IsLocked)
                {
                    _space.AddPostStepCallback(OnPostStep, null, null);
                }
                else
                {
                    AddShapes();
                }

                _reindexStatic |= isStatic;

                return(new ChipmunkBody(shape, isStatic));
            }
        }
Exemplo n.º 2
0
    public void DrawShape(cpShape shape)
    {
        cpBody  body  = shape.body;
        cpColor color = cp.GetShapeColor(shape);; // ColorForBody(body);

        switch (shape.shapeType)
        {
        case cpShapeType.Circle:
        {
            cpCircleShape circle = (cpCircleShape)shape;

            if ((Flags & cpDrawFlags.BB) == cpDrawFlags.BB || (Flags & cpDrawFlags.All) == cpDrawFlags.All)
            {
                Draw(circle.bb);
            }

            if ((Flags & cpDrawFlags.Shapes) == cpDrawFlags.Shapes || (Flags & cpDrawFlags.All) == cpDrawFlags.All)
            {
                Draw(circle, color);
            }
        }
        break;

        case cpShapeType.Segment:
        {
            cpSegmentShape seg = (cpSegmentShape)shape;
            if ((Flags & cpDrawFlags.BB) == cpDrawFlags.BB || (Flags & cpDrawFlags.All) == cpDrawFlags.All)
            {
                Draw(seg.bb);
            }

            if ((Flags & cpDrawFlags.Shapes) == cpDrawFlags.Shapes || (Flags & cpDrawFlags.All) == cpDrawFlags.All)
            {
                Draw(seg, color);
            }
        }
        break;

        case cpShapeType.Polygon:
        {
            cpPolyShape poly = (cpPolyShape)shape;
            if ((Flags & cpDrawFlags.BB) == cpDrawFlags.BB || (Flags & cpDrawFlags.All) == cpDrawFlags.All)
            {
                Draw(poly.bb);
            }

            if ((Flags & cpDrawFlags.Shapes) == cpDrawFlags.Shapes || (Flags & cpDrawFlags.All) == cpDrawFlags.All)
            {
                Draw(poly, color);
            }
        }
        break;

        default:
            cp.AssertHard(false, "Bad assertion in DrawShape()");
            break;
        }
    }
Exemplo n.º 3
0
    public void Draw(cpCircleShape circle, cpColor color)
    {
        cpVect center = circle.tc;
        float  radius = circle.r;
        cpVect To     = cpVect.cpvadd(cpVect.cpvmult(circle.body.GetRotation(), circle.r), (circle.tc));

        DrawCircle(center, cp.cpfmax(radius, 1.0f), color);
        DrawSegment(center, To, 0.5f, cpColor.Grey);
    }
Exemplo n.º 4
0
        public CCPhysicsShapeCircle(CCPhysicsMaterial material, float radius, CCPoint offset)
        {
            _type = PhysicsType.CIRCLE;

            cpShape shape = new cpCircleShape(CCPhysicsShapeInfo.SharedBody, radius, PhysicsHelper.CCPointToCpVect(offset));

            _info.Add(shape);

            _area   = CalculateArea();
            _mass   = material.density == cp.Infinity ? cp.Infinity : material.density * _area;
            _moment = CalculateDefaultMoment();

            Material = material;
        }
Exemplo n.º 5
0
        protected override void AddedToScene()
        {
            base.AddedToScene();

            //PositionX += (windowSize.Width - 640) * .5d;  //new CCPoint(150, 150);
            space.SetIterations(60);
            space.SetGravity(new cpVect(0, -500));
            space.SetSleepTimeThreshold(0.5f);
            space.SetCollisionSlop(0.5f);
            space.SetSleepTimeThreshold(0.5f);

            this.addFloor();
            this.addWalls();
            float width  = 50;
            float height = 60;
            float mass   = width * height * 1f / 1000f;

            var rock = space.AddBody(new cpBody(mass, cp.MomentForBox(mass, width, height)));

            rock.SetPosition(new cpVect(200, 0));
            rock.SetAngle(1);

            cpPolyShape shape = space.AddShape(cpPolyShape.BoxShape(rock, width, height, 0.0f)) as cpPolyShape;

            shape.SetFriction(0.3f);
            shape.SetElasticity(0.3f);
            //shape.SetFilter(NOT_GRABBABLE_FILTER); //The box cannot be dragg

            for (var i = 1; i <= 6; i++)
            {
                float radius = 20f;
                mass = 3;
                var body = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0f, radius, cpVect.Zero)));
                body.SetPosition(new cpVect(i, (2 * radius + 5) * 1));

                cpCircleShape circle = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero)) as cpCircleShape;
                circle.SetElasticity(0.8f);
                circle.SetFriction(1);
            }

            var ramp = space.AddShape(new cpSegmentShape(space.GetStaticBody(), new cpVect(0, 0), new cpVect(300, 200), 10));

            ramp.SetElasticity(1f);
            ramp.SetFriction(1f);
            ramp.SetFilter(NOT_GRABBABLE_FILTER);

            Schedule();
        }
Exemplo n.º 6
0
        public override void Update(float delta)
        {
            if (_dirty)
            {
                float factor = cp.cpfabs(_newScaleX / _scaleX);

                cpCircleShape shape = (cpCircleShape)_info.GetShapes().FirstOrDefault(); //->getShapes().front();
                cpVect        v     = PhysicsHelper.CCPointToCpVect(Offset);             // cpCircleShapeGetOffset();
                v       = cpVect.cpvmult(v, factor);
                shape.c = v;

                shape.SetRadius(shape.GetRadius() * factor);
            }


            base.Update(delta);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Create a physics body and a circle shape and adds it to the world
        /// NOTE: Be sure to call activate on the body when you are ready for it to be simulated!
        /// </summary>
        /// <param name="world">The world to add it to</param>
        /// <param name="position">The starting position for the circle</param>
        /// <param name="rotation">The starting rotation for the circle</param>
        /// <param name="radius">The radius of the circle</param>
        /// <param name="bodyType">The body type of the physics body</param>
        /// <param name="mass">The mass of the physics body</param>
        /// <param name="moment">The moment of inertia for the physics body</param>
        /// <returns></returns>
        public static PhysicsObject CreateCircle(World world, Vector2 position, float rotation, float radius, cpBodyType bodyType = cpBodyType.DYNAMIC, float mass = 1, float moment = 1)
        {
            //Create a body and set its transform
            cpBody body = CreateBody(world, bodyType, mass, moment);

            body.SetPosition(new cpVect(position.X / PHYSICS_TRANSFORM_SCALE, position.Y / PHYSICS_TRANSFORM_SCALE));
            body.SetAngle(rotation);

            //Create the circle shape and add it to the world
            cpCircleShape circleShape = new cpCircleShape(body, radius / PHYSICS_TRANSFORM_SCALE, cpVect.Zero);

            Worlds[world].AddShape(circleShape);

            //Return the phyics object
            return(new PhysicsObject()
            {
                Body = body, Shape = circleShape
            });
        }
Exemplo n.º 8
0
        public PhysicObject(UIImage firstText, int radius, cpSpace space, bool isKinematic = false) : base(firstText)
        {
            trsf = new cpTransform();
            collCount++;
            physic = new cpBody(rnd.Next(500, 1000), cp.PHYSICS_INFINITY);

            if (isKinematic)
            {
                physic.SetBodyType(cpBodyType.KINEMATIC);
            }
            shp = new cpCircleShape(physic, radius, cpVect.Zero);
            shp.Active();
            shp.SetSensor(true);
            shp.SetCollisionType(1);
            physic.SetPosition(new cpVect((float)Frame.Location.X, (float)Frame.Location.Y));
            if (space != null)
            {
                space.AddBody(physic);
                space.AddShape(shp);
                this.space = space;
            }
        }
Exemplo n.º 9
0
circle2segment(cpCircleShape circleShape, cpSegmentShape segmentShape, cpContact con)
{
	cpVect seg_a = segmentShape.ta;
	cpVect seg_b = segmentShape.tb;
	cpVect center = circleShape.tc;
	
	cpVect seg_delta = cpVect.Sub(seg_b, seg_a);
	double closest_t = cpfclamp01(cpVect.Dot(seg_delta, cpVect.Sub(center, seg_a))/cpVect.LengthSQ(seg_delta));
	cpVect closest = cpVect.Add(seg_a, cpVect.Multiply(seg_delta, closest_t));
	
	if(circle2circleQuery(center, closest, circleShape.r, segmentShape.r, con)){
		cpVect n = con[0].n;
		
		// Reject endcap collisions if tangents are provided.
		if(
			(closest_t == 0.0f && cpVect.Dot(n, segmentShape.a_tangent) < 0.0) ||
			(closest_t == 1.0f && cpVect.Dot(n, segmentShape.b_tangent) < 0.0)
		) return 0;
		
		return 1;
	} else {
		return 0;
	}
}
Exemplo n.º 10
0
        protected override float CalculateArea()
        {
            cpCircleShape circle = (cpCircleShape)_info.GetShapes().FirstOrDefault();

            return(cp.AreaForCircle(0, circle.GetRadius()));
        }
Exemplo n.º 11
0
 public static SupportPoint CircleSupportPoint(cpCircleShape circle, cpVect n)
 {
     return new SupportPoint(circle.tc, 0);
 }
Exemplo n.º 12
0
 public ChipmunkBody(cpCircleShape shape, bool isStatic)
 {
     Instance = shape;
     IsStatic = isStatic;
 }
Exemplo n.º 13
0
cpCircleShapeInit(cpCircleShape circle, cpBody body, double radius, cpVect offset)
{
	circle.c = offset;
	circle.r = radius;
	
	cpShapeInit((cpShape )circle, ref cpCircleShapeClass, body);
	
	return circle;
}
Exemplo n.º 14
0
cpCircleShapeSegmentQuery(cpCircleShape circle, cpVect a, cpVect b, cpSegmentQueryInfo info)
{
	circleSegmentQuery((cpShape )circle, circle.tc, circle.r, a, b, info);
}
Exemplo n.º 15
0
cpCicleShapeNearestPointQuery(cpCircleShape circle, cpVect p, cpNearestPointQueryInfo info)
{
	cpVect delta = cpVect.Sub(p, circle.tc);
	double d = cpvlength(delta);
	double r = circle.r;
	
	info.shape = (cpShape )circle;
	info.p = cpVect.Add(circle.tc, cpVect.Multiply(delta, r/d)); // TODO div/0
	info.d = d - r;
}
Exemplo n.º 16
0
cpCircleShapeCacheData(cpCircleShape circle, cpVect p, cpVect rot)
{
	cpVect c = circle.tc = cpVect.Add(p, cpvrotate(circle.c, rot));
	return cpBBNewForCircle(c, circle.r);
}