static cpSpace *
init(void)
{
	cpSpace *space = cpSpaceNew();
	cpSpaceSetIterations(space, 30);
	cpSpaceSetGravity(space, cpv(0, -100));
	cpSpaceSetSleepTimeThreshold(space, 0.5f);
	cpSpaceSetCollisionSlop(space, 0.5f);
	
	cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
	cpShape *shape;
	
	// Create segments around the edge of the screen.
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
	
	// Add lots of boxes.
	for(int i=0; i<14; i++){
		for(int j=0; j<=i; j++){
			body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForBox(1.0f, 30.0f, 30.0f)));
			cpBodySetPosition(body, cpv(j*32 - i*16, 300 - i*32));
			
			shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 30.0f, 30.0f, 0.5f));
			cpShapeSetElasticity(shape, 0.0f);
			cpShapeSetFriction(shape, 0.8f);
		}
	}
	
	// Add a ball to make things more interesting
	cpFloat radius = 15.0f;
	body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero)));
	cpBodySetPosition(body, cpv(0, -240 + radius+5));

	shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
	cpShapeSetElasticity(shape, 0.0f);
	cpShapeSetFriction(shape, 0.9f);
	
	return space;
}
Esempio n. 2
0
static void
add_box(cpSpace *space)
{
	const cpFloat size = 10.0f;
	const cpFloat mass = 1.0f;
	
	cpVect verts[] = {
		cpv(-size,-size),
		cpv(-size, size),
		cpv( size, size),
		cpv( size,-size),
	};
	
	cpFloat radius = cpvlength(cpv(size, size));
	cpVect pos = rand_pos(radius);
	
	cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, 4, verts, cpvzero, 0.0f)));
	body->velocity_func = planetGravityVelocityFunc;
	cpBodySetPosition(body, pos);

	// Set the box's velocity to put it into a circular orbit from its
	// starting position.
	cpFloat r = cpvlength(pos);
	cpFloat v = cpfsqrt(gravityStrength / r) / r;
	cpBodySetVelocity(body, cpvmult(cpvperp(pos), v));

	// Set the box's angular velocity to match its orbital period and
	// align its initial angle with its position.
	cpBodySetAngularVelocity(body, v);
	cpBodySetAngle(body, cpfatan2(pos.y, pos.x));

	cpShape *shape = cpSpaceAddShape(space, cpPolyShapeNew(body, 4, verts, cpTransformIdentity, 0.0));
	cpShapeSetElasticity(shape, 0.0f);
	cpShapeSetFriction(shape, 0.7f);
}
Esempio n. 3
0
void RCPBody::setPosition(RVector const & pos)
{
    mPosition = pos;
    if (mType != Static && mBody) {
        cpBodySetPosition(mBody, cpv(mPosition.x(), mPosition.y()));
    }
}
Esempio n. 4
0
ETERM *body_set_position(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);
    ETERM *vectorp = erl_element(3, argp);
    ETERM *xp = erl_element(1, vectorp);
    ETERM *yp = erl_element(2, vectorp);

    erlmunk_space *s = NULL;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int body_id = ERL_INT_VALUE(idp);
    erlmunk_body *b = NULL;
    HASH_FIND_INT(s->bodies, &body_id, b);
    if (b == NULL)
        return NULL;

    cpBodySetPosition(b->body, cpv(ERL_FLOAT_VALUE(xp),
                                   ERL_FLOAT_VALUE(yp)));

    // DEBUGF(("body_set_position(x: %f, y: %f) has succeeded",
    //     ERL_FLOAT_VALUE(xp), ERL_FLOAT_VALUE(yp)));
    return NULL;
}
Esempio n. 5
0
ETERM *body_update_position(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);
    ETERM *deltap = erl_element(3, argp);

    erlmunk_space *s = NULL;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int body_id = ERL_INT_VALUE(idp);
    erlmunk_body *b = NULL;
    HASH_FIND_INT(s->bodies, &body_id, b);
    if (b == NULL)
        return NULL;

    cpVect position = cpBodyGetPosition(b->body);
    float angle = deg_to_rad(cpBodyGetAngle(b->body));
    cpVect angleV = cpvforangle(angle);
    cpVect projection = cpvmult(angleV, ERL_FLOAT_VALUE(deltap));
    cpVect new_position = cpvadd(projection, position);
    cpBodySetPosition(b->body, new_position);

    // DEBUGF(("body_update_position(x: %f, y: %f, delta: %f) has succeeded (x: %f, y: %f)",
    //     position.x, position.y, ERL_FLOAT_VALUE(deltap),
    //     new_position.x, new_position.y));
    return NULL;
}
Esempio n. 6
0
ETERM *body_copy(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);
    ETERM *from_idp = erl_element(3, argp);

    erlmunk_space *s;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int body_id = ERL_INT_VALUE(idp);
    erlmunk_body *b;
    HASH_FIND_INT(s->bodies, &body_id, b);
    if (b == NULL)
        return NULL;

    int from_body_id = ERL_INT_VALUE(from_idp);
    erlmunk_body *from_b;
    HASH_FIND_INT(s->bodies, &from_body_id, from_b);

    // DEBUGF(("copying location from body #%d(%p) to #%d(%p)",
    //     from_body_id, from_b, body_id, b));

    // copy position and angle from the from body
    cpBodySetPosition(b->body, cpBodyGetPosition(from_b->body));
    cpBodySetAngle(b->body, cpBodyGetAngle(from_b->body));
    cpBodySetVelocity(b->body, cpBodyGetVelocity(from_b->body));

    return NULL;
}
Esempio n. 7
0
	Shell(cpVect pos, cpVect vel, float angle)
	{
		cpVect vl[4] = {cpv(0, 0), cpv(0.1, 0), cpv(0.07, 0.3), cpv(0.03, 0.3)};
		int vn = sizeof(vl)/sizeof(cpVect);
		
		float mass = cpAreaForPoly(vn, vl, 0) * shell_density;
		float moi = cpMomentForPoly(mass, vn, vl, cpv(0, 0), 0);
		body = cpBodyNew(mass, moi);
		
		cpshape = cpPolyShapeNew(body, vn, vl, cpTransformIdentity, 0);
		cpShapeSetFriction(cpshape, 0.9);

		cpVect centroid = cpCentroidForPoly(vn, vl);		

		shape.setPointCount(vn);
		for (int i = 0; i < vn; i++)
		{
			shape.setPoint(i, sf::Vector2f(vl[i].x, vl[i].y));
		}
		
		cpBodySetCenterOfGravity(body, centroid);
		cpBodySetPosition(body, pos-centroid);
		cpBodySetVelocity(body, vel);
		cpBodySetAngle(body, angle);
		
		cpShapeSetCollisionType(cpshape, 2);
		cpShapeSetUserData(cpshape, this);
	}
Esempio n. 8
0
void PhysicsBody::setPosition(float positionX, float positionY)
{
    cpVect tt;

    tt.x = positionX + _positionOffset.x;
    tt.y = positionY + _positionOffset.y;

    cpBodySetPosition(_cpBody, tt);
}
static void
make_leg(cpSpace *space, cpFloat side, cpFloat offset, cpBody *chassis, cpBody *crank, cpVect anchor)
{
	cpVect a, b;
	cpShape *shape;
	
	cpFloat leg_mass = 1.0f;

	// make leg
	a = cpvzero, b = cpv(0.0f, side);
	cpBody *upper_leg = cpSpaceAddBody(space, cpBodyNew(leg_mass, cpMomentForSegment(leg_mass, a, b, 0.0f)));
	cpBodySetPosition(upper_leg, cpv(offset, 0.0f));
	
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(upper_leg, a, b, seg_radius));
	cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES));
	
	cpSpaceAddConstraint(space, cpPivotJointNew2(chassis, upper_leg, cpv(offset, 0.0f), cpvzero));
	
	// lower leg
	a = cpvzero, b = cpv(0.0f, -1.0f*side);
	cpBody *lower_leg = cpSpaceAddBody(space, cpBodyNew(leg_mass, cpMomentForSegment(leg_mass, a, b, 0.0f)));
	cpBodySetPosition(lower_leg, cpv(offset, -side));
	
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(lower_leg, a, b, seg_radius));
	cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES));
	
	shape = cpSpaceAddShape(space, cpCircleShapeNew(lower_leg, seg_radius*2.0f, b));
	cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES));
	cpShapeSetElasticity(shape, 0.0f);
	cpShapeSetFriction(shape, 1.0f);
	
	cpSpaceAddConstraint(space, cpPinJointNew(chassis, lower_leg, cpv(offset, 0.0f), cpvzero));
	
	cpSpaceAddConstraint(space, cpGearJointNew(upper_leg, lower_leg, 0.0f, 1.0f));
	
	cpConstraint *constraint;
	cpFloat diag = cpfsqrt(side*side + offset*offset);
	
	constraint = cpSpaceAddConstraint(space, cpPinJointNew(crank, upper_leg, anchor, cpv(0.0f, side)));
	cpPinJointSetDist(constraint, diag);
	
	constraint = cpSpaceAddConstraint(space, cpPinJointNew(crank, lower_leg, anchor, cpvzero));
	cpPinJointSetDist(constraint, diag);
}
Esempio n. 10
0
static cpSpace *
init(void)
{
	ChipmunkDemoMessageString = "One way platforms are trivial in Chipmunk using a very simple collision callback.";
	
	cpSpace *space = cpSpaceNew();
	cpSpaceSetIterations(space, 10);
	cpSpaceSetGravity(space, cpv(0, -100));

	cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
	cpShape *shape;

	// Create segments around the edge of the screen.
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
	
	// Add our one way segment
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-100), cpv(160,-100), 10.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetCollisionType(shape, COLLISION_TYPE_ONE_WAY);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
	
	// We'll use the data pointer for the OneWayPlatform struct
	platformInstance.n = cpv(0, 1); // let objects pass upwards
	cpShapeSetUserData(shape, &platformInstance);
	
	
	// Add a ball to test it out
	cpFloat radius = 15.0f;
	body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero)));
	cpBodySetPosition(body, cpv(0, -200));
	cpBodySetVelocity(body, cpv(0, 170));

	shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
	cpShapeSetElasticity(shape, 0.0f);
	cpShapeSetFriction(shape, 0.9f);
	cpShapeSetCollisionType(shape, 2);
	
	cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, COLLISION_TYPE_ONE_WAY);
	handler->preSolveFunc = PreSolve;
	
	return space;
}
Esempio n. 11
0
static cpBody *MakeBody(const float x, const float y, const float w)
{
	cpBody *body = cpSpaceAddBody(space.Space, cpBodyNewStatic());
	cpBodySetPosition(body, cpv(x + w / 2, y - GAP_HEIGHT / 2));
	cpShape *shape = cpSpaceAddShape(
		space.Space, cpBoxShapeNew(body, w, GAP_HEIGHT, 0.0));
	cpShapeSetElasticity(shape, BLOCK_ELASTICITY);
	cpShapeSetFriction(shape, 1.0f);
	return body;
}
Esempio n. 12
0
	void RigidBody2D::CopyBodyData(cpBody* body)
	{
		cpBodySetAngle(m_handle, cpBodyGetAngle(body));
		cpBodySetAngularVelocity(m_handle, cpBodyGetAngularVelocity(body));
		cpBodySetCenterOfGravity(m_handle, cpBodyGetCenterOfGravity(body));
		cpBodySetForce(m_handle, cpBodyGetForce(body));
		cpBodySetPosition(m_handle, cpBodyGetPosition(body));
		cpBodySetTorque(m_handle, cpBodyGetTorque(body));
		cpBodySetVelocity(m_handle, cpBodyGetVelocity(body));
	}
Esempio n. 13
0
	void RigidBody2D::CopyBodyData(cpBody* from, cpBody* to)
	{
		cpBodySetAngle(to, cpBodyGetAngle(from));
		cpBodySetAngularVelocity(to, cpBodyGetAngularVelocity(from));
		cpBodySetCenterOfGravity(to, cpBodyGetCenterOfGravity(from));
		cpBodySetForce(to, cpBodyGetForce(from));
		cpBodySetPosition(to, cpBodyGetPosition(from));
		cpBodySetTorque(to, cpBodyGetTorque(from));
		cpBodySetVelocity(to, cpBodyGetVelocity(from));
	}
Esempio n. 14
0
	void RigidBody2D::SetPosition(const Vector2f& position)
	{
		cpBodySetPosition(m_handle, cpv(position.x, position.y));
		if (m_isStatic)
		{
			m_world->RegisterPostStep(this, [](Nz::RigidBody2D* body)
			{
				cpSpaceReindexShapesForBody(body->GetWorld()->GetHandle(), body->GetHandle());
			});
		}
	}
Esempio n. 15
0
static cpBody *
add_ball(cpSpace *space, cpVect pos)
{
	cpBody *body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForCircle(1.0f, 30, 0, cpvzero)));
	cpBodySetPosition(body, pos);
	
	cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, 30, cpvzero));
	cpShapeSetElasticity(shape, 0.0f);
	cpShapeSetFriction(shape, 0.5f);
	
	return body;
}
Esempio n. 16
0
static cpSpace *
init(void)
{
	ChipmunkDemoMessageString = "Sticky collisions using the cpArbiter data pointer.";
	
	cpSpace *space = cpSpaceNew();
	cpSpaceSetIterations(space, 10);
	cpSpaceSetGravity(space, cpv(0, -1000));
	cpSpaceSetCollisionSlop(space, 2.0);

	cpBody *staticBody = cpSpaceGetStaticBody(space);
	cpShape *shape;

	// Create segments around the edge of the screen.
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-340,-260), cpv(-340, 260), 20.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv( 340,-260), cpv( 340, 260), 20.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-340,-260), cpv( 340,-260), 20.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
	
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-340, 260), cpv( 340, 260), 20.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
	
	for(int i=0; i<200; i++){
		cpFloat mass = 0.15f;
		cpFloat radius = 10.0f;
		
		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero)));
		cpBodySetPosition(body, cpv(cpflerp(-150.0f, 150.0f, frand()), cpflerp(-150.0f, 150.0f, frand())));

		cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius + STICK_SENSOR_THICKNESS, cpvzero));
		cpShapeSetFriction(shape, 0.9f);
		cpShapeSetCollisionType(shape, COLLISION_TYPE_STICKY);
	}
	
	cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, COLLISION_TYPE_STICKY);
	handler->preSolveFunc = StickyPreSolve;
	handler->separateFunc = StickySeparate;
	
	return space;
}
Esempio n. 17
0
void PhysicsSprite::setPosition(float x, float y)
{
#if CC_ENABLE_CHIPMUNK_INTEGRATION

    cpVect cpPos = cpv(x, y);
    cpBodySetPosition(_CPBody, cpPos);

#elif CC_ENABLE_BOX2D_INTEGRATION
    
    float angle = _pB2Body->GetAngle();
    _pB2Body->SetTransform(b2Vec2(x / _PTMRatio, y / _PTMRatio), angle);
#endif
}
Esempio n. 18
0
	Ship(int vn, cpVect* vl, cpVect pos, Genome* ng = 0)
	{
		float mass = cpAreaForPoly(vn, vl, 0) * ship_density;
		float moi = cpMomentForPoly(mass, vn, vl, cpv(0, 0), 0);
		body = cpBodyNew(mass, moi);
		
		cpshape = cpPolyShapeNew(body, vn, vl, cpTransformIdentity, 0);
		cpShapeSetFriction(cpshape, 0.9);

		cpVect centroid = cpCentroidForPoly(vn, vl);		

		shape.setPointCount(vn);
		for (int i = 0; i < vn; i++)
		{
			shape.setPoint(i, sf::Vector2f(vl[i].x, vl[i].y));
		}
		
		cpBodySetCenterOfGravity(body, centroid);
		cpBodySetPosition(body, pos-centroid);
		cpBodySetVelocity(body, cpv(0, 0));
		
		cpShapeSetCollisionType(cpshape, 1);
		cpShapeSetUserData(cpshape, this);
		
		
		last_fired = 0;
		
		nose_angle = PI/2;
		
		
		player = false;
		target = 0;
		score = 0;
		if (ng == 0)
		{
			Genome* braingenome = mutate(readgenome("shipmind.mind"));
			brain = braingenome->makenetwork();
			delete braingenome;
		}
		else
		{
			brain = ng->makenetwork();
		}
		
		score = 0;
	}	
void DynamicObjectStabilizator::fixatePoints()
{
//	vector<IDynamicObject *>::iterator begin = m_DynamicObjects.begin();
//	vector<IDynamicObject *>::iterator end = m_DynamicObjects.end();
//	vector<IDynamicObject *>::iterator iter = begin + 1;
//	for(  ; iter != end ; iter++ )
	size_t count = m_DynamicObjects.size();
	for( size_t object_i = 0 ; object_i < 2 ; object_i++ )
	{
		const IGeometryObject & geometryObjext = m_DynamicObjects[object_i]->getGeometryObject();
		if( geometryObjext.getType() != GEOMETRYOBJECT_POINT )
		{
//			assert( false );
			continue;
		}
		cpBody * kineticBody = cpBodyNewKinematic();
		cpSpaceAddBody( m_Space, kineticBody );
		m_KineticBodies.push_back( kineticBody );

		const GeometryPoint & geometryPoint = dynamic_cast<const GeometryPoint &>( geometryObjext );

		int x = geometryPoint.getX();
		int y = geometryPoint.getY();
		cpVect mousePoint = cpv( x, y );
		cpShape * shape = cpSpacePointQueryNearest( m_Space, mousePoint, 100.0, GRAB_FILTER, 0 );

		if( 0 == shape )
		{
			return;
		}

		cpVect new_mouse_position = cpv( x, y );
		cpBodySetPosition( kineticBody, new_mouse_position );

		cpBody * trackingBody = cpShapeGetBody( shape );


		cpConstraint * joint = cpPivotJointNew2( kineticBody, trackingBody, cpvzero, cpvzero );
		cpSpaceAddConstraint( m_Space, joint );

		m_Joints.push_back( joint );

		break; //one pointb
	}
}
Esempio n. 20
0
void Slice::ClipPoly(cpSpace *space, cpShape *shape, cpVect n, cpFloat dist)
{
	cpBody *body = cpShapeGetBody(shape);
	
	int count = cpPolyShapeGetCount(shape);
	int clippedCount = 0;
	
	cpVect *clipped = (cpVect *)alloca((count + 1)*sizeof(cpVect));
	
	for(int i=0, j=count-1; i<count; j=i, i++){
		cpVect a = cpBodyLocalToWorld(body, cpPolyShapeGetVert(shape, j));
		cpFloat a_dist = cpvdot(a, n) - dist;
		
		if(a_dist < 0.0){
			clipped[clippedCount] = a;
			clippedCount++;
		}
		
		cpVect b = cpBodyLocalToWorld(body, cpPolyShapeGetVert(shape, i));
		cpFloat b_dist = cpvdot(b, n) - dist;
		
		if(a_dist*b_dist < 0.0f){
			cpFloat t = cpfabs(a_dist)/(cpfabs(a_dist) + cpfabs(b_dist));
			
			clipped[clippedCount] = cpvlerp(a, b, t);
			clippedCount++;
		}
	}
	
	cpVect centroid = cpCentroidForPoly(clippedCount, clipped);
	cpFloat mass = cpAreaForPoly(clippedCount, clipped, 0.0f)*DENSITY;
	cpFloat moment = cpMomentForPoly(mass, clippedCount, clipped, cpvneg(centroid), 0.0f);
	
	cpBody *new_body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
	cpBodySetPosition(new_body, centroid);
	cpBodySetVelocity(new_body, cpBodyGetVelocityAtWorldPoint(body, centroid));
	cpBodySetAngularVelocity(new_body, cpBodyGetAngularVelocity(body));
	
	cpTransform transform = cpTransformTranslate(cpvneg(centroid));
	cpShape *new_shape = cpSpaceAddShape(space, cpPolyShapeNew(new_body, clippedCount, clipped, transform, 0.0));
	// Copy whatever properties you have set on the original shape that are important
	cpShapeSetFriction(new_shape, cpShapeGetFriction(shape));
}
Esempio n. 21
0
/* Preparing the tiles
 * -------------------
 *
 * We traverse the tilemap string, detecting any "lit" tile
 * using the character #. For each "lit" tile, we create a
 * Chipmunk2D body and position it in screen coordinates.
 */
static void init_tiles(struct state* state)
{
    cpFloat radius = state->tile_img->width / 2;
    cpFloat mass = 1;
    cpFloat moment = cpMomentForCircle(mass, 0, radius, cpvzero);
    for (unsigned int i = 0; i < strlen(tile_map); i++) {
        if (tile_map[i] == '#') {
            state->tiles[i].is_active = true;
            state->tiles[i].body =
                cpSpaceAddBody(state->space, cpBodyNew(mass, moment));
            state->tiles[i].shape = cpSpaceAddShape(
                state->space,
                cpCircleShapeNew(state->tiles[i].body, radius, cpvzero));
            cpShapeSetFriction(state->tiles[i].shape, 0.7);
            cpVect pp = cpv((i % 54) * 6 + 4, (i / 54) * 6 + 40);
            cpBodySetPosition(state->tiles[i].body, pp);
        } else
            state->tiles[i].is_active = false;
    }
}
Esempio n. 22
0
static void
update(cpSpace *space, double dt)
{
	cpFloat coef = (2.0f + ChipmunkDemoKeyboard.y)/3.0f;
	cpFloat rate = ChipmunkDemoKeyboard.x*30.0f*coef;
	
	cpSimpleMotorSetRate(motor, rate);
	cpConstraintSetMaxForce(motor, rate ? 1000000.0f : 0.0f);

	cpSpaceStep(space, dt);
	
	for(int i=0; i<numBalls; i++){
		cpBody *ball = balls[i];
		cpVect pos = cpBodyGetPosition(ball);
		
		if(pos.x > 320.0f){
			cpBodySetVelocity(ball, cpvzero);
			cpBodySetPosition(ball, cpv(-224.0f, 200.0f));
		}
	}
}
Esempio n. 23
0
/* Mouse handling is a bit tricky. We want the user to move
 * tiles using the mouse but because tiles are dynamic bodies
 * managed by Chipmunk2D, we cannot directly control them.
 * This is resolved by creating a pivot joint between an
 * invisible mouse body that we can control and the tile body
 * that we cannot directly control.
 */
static void apply_mouse_motion(struct state* state)
{
    struct mouse m;
    update_mouse(&m);
    int w, h;
    get_screen_size(&w, &h);
    int x = m.x_position * w;
    int y = m.y_position * h;
    cpVect mouse_pos = cpv(x, y);
    cpVect new_point =
        cpvlerp(cpBodyGetPosition(state->mouse_body), mouse_pos, 0.25f);
    cpBodySetVelocity(
        state->mouse_body,
        cpvmult(cpvsub(new_point, cpBodyGetPosition(state->mouse_body)),
                60.0f));
    cpBodySetPosition(state->mouse_body, new_point);
    if (m.left_click && state->mouse_joint == NULL) {
        cpFloat radius = 5.0;
        cpPointQueryInfo info = { 0 };
        cpShape* shape = cpSpacePointQueryNearest(state->space, mouse_pos,
                                                  radius, GRAB_FILTER, &info);
        if (shape && cpBodyGetMass(cpShapeGetBody(shape)) < INFINITY) {
            cpVect nearest = (info.distance > 0.0f ? info.point : mouse_pos);
            cpBody* body = cpShapeGetBody(shape);
            state->mouse_joint =
                cpPivotJointNew2(state->mouse_body, body, cpvzero,
                                 cpBodyWorldToLocal(body, nearest));
            cpConstraintSetMaxForce(state->mouse_joint, 5000000.0f);
            cpConstraintSetErrorBias(state->mouse_joint,
                                     cpfpow(1.0f - 0.15f, 60.0f));
            cpSpaceAddConstraint(state->space, state->mouse_joint);
        }
    }
    if (m.left_click == false && state->mouse_joint != NULL) {
        cpSpaceRemoveConstraint(state->space, state->mouse_joint);                                                 
        cpConstraintFree(state->mouse_joint);                                                               
        state->mouse_joint = NULL;  
    }
}
Esempio n. 24
0
DynamicObject::DynamicObject(float x, float y, float scale, float mass, float elast, float fric, int type, std::string gpuPath, std::string vPath, std::string fPath)
{
    gpuDataList.push_back(gpuStore.add(gpuPath, 3.1415f));
    shaderList.push_back(shaderStore.add(vPath, fPath));

    transformOverrides = false;

    height = scale;
    modelScale = glm::vec3(scale);

    width = height*gpuDataList[0]->whRatio;

    /*** Set physics data ***/
    body = cpBodyNew(mass, 0);
    cpSpaceAddBody(space, body);
    cpBodySetPosition(body, cpv(x, y));

    ObjGPUData* gpuData = gpuDataList[0];
    int vertCount = gpuData->vList.size();
    cpVect vertices[vertCount];

    glm::vec3 pos(x, y, 0);

    for(int i = 0; i < vertCount; i++)
    {
        glm::vec4 currentVert = glm::translate(glm::mat4(1.0f), pos) * glm::scale(glm::mat4(1.0f), modelScale) * gpuData->rotation * gpuData->unitScale * glm::vec4(gpuData->vList[i],0);
        vertices[i] = cpv(currentVert.x, currentVert.y);
    }

    shape = cpSpaceAddShape(space, (cpShape*) cpPolyShapeInitRaw(cpPolyShapeAlloc(), body, vertCount, vertices, 1.0f));
    cpBodySetMoment(body, abs(cpMomentForPoly(mass, vertCount, vertices, cpvzero, 1.0f)));
    cpShapeSetElasticity(shape, elast);
    cpShapeSetFriction(shape, fric);
    cpShapeSetUserData(shape, this);
    cpShapeSetCollisionType(shape, type);

    draw = true;
}
Esempio n. 25
0
File: physics.c Progetto: dns/CLove
static int l_physics_setBodyPosition(lua_State* state)
{
    l_tools_checkUserDataPlusErrMsg(state, 1, "You must provide a body");
    l_tools_checkUserDataPlusErrMsg(state, 2, "You must provide a space");

    l_physics_Body* body = (l_physics_Body*)lua_touserdata(state, 1);
    l_physics_PhysicsData* space = (l_physics_PhysicsData*)lua_touserdata(state, 2);

    cpVect vec = cpvzero;
    vec.x = l_tools_toNumberOrError(state, 3);
    vec.y = l_tools_toNumberOrError(state, 4);

    cpBodySetPosition(body->body, vec);
    /*
     * Docs:
     * When changing the position you may also want to call cpSpaceReindexShapesForBody()
     * to update the collision detection information for the attached shapes if plan to
     * make any queries against the space.
     */
    cpSpaceReindexShapesForBody(space->physics->space, body->body);

    return 0;
}
Esempio n. 26
0
static cpSpace *
init(void)
{
	ChipmunkDemoMessageString = "Control the crane by moving the mouse. Right click to release.";
	
	cpSpace *space = cpSpaceNew();
	cpSpaceSetIterations(space, 30);
	cpSpaceSetGravity(space, cpv(0, -100));
	cpSpaceSetDamping(space, 0.8);
	
	cpBody *staticBody = cpSpaceGetStaticBody(space);
	cpShape *shape;
	
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
	
	// Add a body for the dolly.
	dollyBody = cpSpaceAddBody(space, cpBodyNew(10, INFINITY));
	cpBodySetPosition(dollyBody, cpv(0, 100));
	
	// Add a block so you can see it.
	cpSpaceAddShape(space, cpBoxShapeNew(dollyBody, 30, 30, 0.0));
	
	// Add a groove joint for it to move back and forth on.
	cpSpaceAddConstraint(space, cpGrooveJointNew(staticBody, dollyBody, cpv(-250, 100), cpv(250, 100), cpvzero));
	
	// Add a pivot joint to act as a servo motor controlling it's position
	// By updating the anchor points of the pivot joint, you can move the dolly.
	dollyServo = cpSpaceAddConstraint(space, cpPivotJointNew(staticBody, dollyBody, cpBodyGetPosition(dollyBody)));
	// Max force the dolly servo can generate.
	cpConstraintSetMaxForce(dollyServo, 10000);
	// Max speed of the dolly servo
	cpConstraintSetMaxBias(dollyServo, 100);
	// You can also change the error bias to control how it slows down.
	//cpConstraintSetErrorBias(dollyServo, 0.2);
	
	
	// Add the crane hook.
	cpBody *hookBody = cpSpaceAddBody(space, cpBodyNew(1, INFINITY));
	cpBodySetPosition(hookBody, cpv(0, 50));
	
	// Add a sensor shape for it. This will be used to figure out when the hook touches a box.
	shape = cpSpaceAddShape(space, cpCircleShapeNew(hookBody, 10, cpvzero));
	cpShapeSetSensor(shape, cpTrue);
	cpShapeSetCollisionType(shape, HOOK_SENSOR);
	
	// Add a slide joint to act as a winch motor
	// By updating the max length of the joint you can make it pull up the load.
	winchServo = cpSpaceAddConstraint(space, cpSlideJointNew(dollyBody, hookBody, cpvzero, cpvzero, 0, INFINITY));
	// Max force the dolly servo can generate.
	cpConstraintSetMaxForce(winchServo, 30000);
	// Max speed of the dolly servo
	cpConstraintSetMaxBias(winchServo, 60);
	
	// TODO: cleanup
	// Finally a box to play with
	cpBody *boxBody = cpSpaceAddBody(space, cpBodyNew(30, cpMomentForBox(30, 50, 50)));
	cpBodySetPosition(boxBody, cpv(200, -200));
	
	// Add a block so you can see it.
	shape = cpSpaceAddShape(space, cpBoxShapeNew(boxBody, 50, 50, 0.0));
	cpShapeSetFriction(shape, 0.7);
	cpShapeSetCollisionType(shape, CRATE);
	
	cpCollisionHandler *handler = cpSpaceAddCollisionHandler(space, HOOK_SENSOR, CRATE);
	handler->beginFunc = (cpCollisionBeginFunc)HookCrate;
	
	
	return space;
}
Esempio n. 27
0
struct gun instantiate_gun(struct obj *owner, const char *gunType,
                           const char *bulletType)
{
//    fprintf(stderr, "\n\n\n%s\n\n\n", bulletType);
    struct gun ret = {.bulletType = strdup(bulletType), .owner = owner, .cooldown = owner->room->ticks};
    ret.fire =  guntbl_find(&fires, gunType);
    return ret;
}

void machinegun_fire(struct gun *self)
{
    if (self->owner->room->ticks - self->cooldown > 1) {
        struct obj *bullet = instantiate_object(self->bulletType,
                                                self->owner->objectVect,
                                                self->owner->room);

        cpBodySetPosition(bullet->body,
                          cpBodyLocalToWorld(self->owner->body,
                                             cpv(0, -self->owner->radius - bullet->radius - 2)));

        cpBodySetMoment(bullet->body,
                        cpMomentForCircle(10000.0, 0.0,
                                          bullet->radius*2, cpvzero));
        cpBodySetAngle(bullet->body, cpBodyGetAngle(self->owner->body) + M_PI);
        ((struct bullet_extra*)bullet->extra)->speed = 20.0;
        ((struct bullet_extra*)bullet->extra)->owner = self->owner;

        cpBodyApplyImpulseAtLocalPoint(self->owner->body,
                                       cpv(0, 50.0),
                                       cpv(0, -(self->owner->radius * 2)));
        
        self->cooldown = self->owner->room->ticks;
    }
}
struct gunpair {
    const char *name;
    const gunmethod method;
};

void register_guns(void)
{
    struct gunpair pairs[] = {{"machine gun", machinegun_fire}};

    for (size_t i = 0; i < ARRAY_LENGTH(pairs); i++) {
        char *currentName = pairs[i].name;
        gunmethod currentMethod = pairs[i].method;

        if ((!currentName && currentMethod) || (currentName && !currentMethod)) {
            CROAK("Unevenly sized methodpair array");
        }
        guntbl_insert(&fires, currentName, currentMethod);
    }
}


void guntbl_insert(struct guntbl *table, const char *key, gunmethod method)
{
    int offset = hash(key) % 32;
    if (table->keys[offset]) {
		if (strcmp(table->keys[offset], key) == 0 ) {
			table->methods[offset] = method;
		} else {
			if (!table->next)
			{
				table->next = malloc(sizeof(struct guntbl));
				*table->next = (struct guntbl){.next = NULL};
			}
			guntbl_insert(table->next, key, method);
		} 
	} else {
Esempio n. 28
0
cpSpace *Chains::Init()
{
    ChipmunkDemo::Init();

    space = cpSpaceNew();
    cpSpaceSetIterations(space, 30);
    cpSpaceSetGravity(space, cpv(0, -100));
    cpSpaceSetSleepTimeThreshold(space, 0.5f);

    cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
    cpShape *shape;

    // Create segments around the edge of the screen.
    shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
    cpShapeSetElasticity(shape, 1.0f);
    cpShapeSetFriction(shape, 1.0f);
    cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

    shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
    cpShapeSetElasticity(shape, 1.0f);
    cpShapeSetFriction(shape, 1.0f);
    cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

    shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
    cpShapeSetElasticity(shape, 1.0f);
    cpShapeSetFriction(shape, 1.0f);
    cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

    shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f));
    cpShapeSetElasticity(shape, 1.0f);
    cpShapeSetFriction(shape, 1.0f);
    cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

    cpFloat mass = 1;
    cpFloat width = 20;
    cpFloat height = 30;

    cpFloat spacing = width*0.3;

    // Add lots of boxes.
    for(int i=0; i<CHAIN_COUNT; i++){
        cpBody *prev = NULL;

        for(int j=0; j<LINK_COUNT; j++){
            cpVect pos = cpv(40*(i - (CHAIN_COUNT - 1)/2.0), 240 - (j + 0.5)*height - (j + 1)*spacing);

            body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, width, height)));
            cpBodySetPosition(body, pos);

            shape = cpSpaceAddShape(space, cpSegmentShapeNew(body, cpv(0, (height - width)/2.0), cpv(0, (width - height)/2.0), width/2.0));
            cpShapeSetFriction(shape, 0.8f);

            cpFloat breakingForce = 80000;

            cpConstraint *constraint = NULL;
            if(prev == NULL){
                constraint = cpSpaceAddConstraint(space, cpSlideJointNew(body, staticBody, cpv(0, height/2), cpv(pos.x, 240), 0, spacing));
            } else {
                constraint = cpSpaceAddConstraint(space, cpSlideJointNew(body, prev, cpv(0, height/2), cpv(0, -height/2), 0, spacing));
            }

            cpConstraintSetMaxForce(constraint, breakingForce);
            cpConstraintSetPostSolveFunc(constraint, BreakableJointPostSolve);
            cpConstraintSetCollideBodies(constraint, cpFalse);

            prev = body;
        }
    }

    cpFloat radius = 15.0f;
    body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero)));
    cpBodySetPosition(body, cpv(0, -240 + radius+5));
    cpBodySetVelocity(body, cpv(0, 300));

    shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
    cpShapeSetElasticity(shape, 0.0f);
    cpShapeSetFriction(shape, 0.9f);
	
	return space;
}
Esempio n. 29
0
void Body::setPosition(glm::vec2 pos){
	cpBodySetPosition(body, toChipmunk(pos));
}
Esempio n. 30
0
static cpSpace *
init(void)
{
	cpSpace *space = cpSpaceNew();
	cpSpaceSetIterations(space, 5);
	space->damping = 0.1;
	
	cpFloat mass = 1.0f;
	
	{
		cpFloat size = 100.0;
		
		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, size, size)));
		cpBodySetPosition(body, cpv(100.0, 50.0f));
		
		shape1 = cpSpaceAddShape(space, cpBoxShapeNew(body, size, size, 0.0));
		shape1->group = 1;
	}{
		cpFloat size = 100.0;
		
		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, size, size)));
		cpBodySetPosition(body, cpv(120.0, -40.0f));
		cpBodySetAngle(body, 1e-2);
		
		shape2 = cpSpaceAddShape(space, cpBoxShapeNew(body, size, size, 0.0));
		shape2->group = 1;
	}
	
//	{
//		cpFloat size = 100.0;
//		const int NUM_VERTS = 5;
//		
//		cpVect verts[NUM_VERTS];
//		for(int i=0; i<NUM_VERTS; i++){
//			cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS);
//			verts[i] = cpv(size/2.0*cos(angle), size/2.0*sin(angle));
//		}
//		
//		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero)));
//		cpBodySetPosition(body, cpv(100.0, 50.0f));
//		
//		shape1 = cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero));
//		shape1->group = 1;
//	}
//	{
//		cpFloat size = 100.0;
//		const int NUM_VERTS = 4;
//		
//		cpVect verts[NUM_VERTS];
//		for(int i=0; i<NUM_VERTS; i++){
//			cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS);
//			verts[i] = cpv(size/2.0*cos(angle), size/2.0*sin(angle));
//		}
//		
//		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero)));
//		cpBodySetPosition(body, cpv(100.0, -50.0f));
//		
//		shape2 = cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero));
//		shape2->group = 1;
//	}
//	
//	{
//		cpFloat size = 150.0;
//		cpFloat radius = 25.0;
//		
//		cpVect a = cpv( size/2.0, 0.0);
//		cpVect b = cpv(-size/2.0, 0.0);
//		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b)));
//		cpBodySetPosition(body, cpv(0, 25));
//		
//		shape1 = cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, radius));
//		shape1->group = 1;
//	}
//	{
//		cpFloat radius = 50.0;
//		
//		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero)));
//		cpBodySetPosition(body, cpv(0, -25));
//		
//		shape2 = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
//		shape2->group = 1;
//	}

	return space;
}