shared_ptr<Shape> Space::pointQueryFirst(Vect p, Layers layers, Group group) const { cpShapeFilter filter{static_cast<cpGroup>(group), static_cast<cpBitmask>(layers), static_cast<cpBitmask>(layers)}; cpPointQueryInfo i; return findPtr(cpSpacePointQueryNearest(space, p, 100, filter, &i)); }
PhysicsShape* PhysicsWorld::getShape(const Vec2& point) const { cpShape* shape = cpSpacePointQueryNearest(_cpSpace, PhysicsHelper::point2cpv(point), 0, CP_SHAPE_FILTER_ALL, nullptr); return shape == nullptr ? nullptr : static_cast<PhysicsShape*>(cpShapeGetUserData(shape)); }
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 } }
/* 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; } }