fan_sensor(phys_sensor_defn const& defn): m_angle(defn.orientation), m_count(0) { const int SegmentsPerQuadrant = 8; auto fan_data = boost::any_cast<fan_sensor_data>(defn.type_specific); const size_t num_segments = (int)std::ceil(SegmentsPerQuadrant * fan_data.field_of_view / (b2_pi / 2)); const size_t max_segments_per_poly = b2_maxPolygonVertices - 2; const size_t num_polys = num_segments / max_segments_per_poly + (num_segments % max_segments_per_poly > 0 ? 1 : 0); size_t segment_idx = 0; for(size_t i = 0; i < num_polys; ++i) { std::vector< b2Vec2 > points; points.emplace_back(b2Vec2{ 0.f, 0.f }); b2Vec2 base(0.f, defn.range); base = b2Mul(b2Rot(-fan_data.field_of_view / 2), base); points.emplace_back(b2Mul(b2Rot(fan_data.field_of_view * segment_idx / num_segments), base)); auto end = std::min(segment_idx + max_segments_per_poly, num_segments); for(; segment_idx < end; ++segment_idx) { points.emplace_back(b2Mul(b2Rot(fan_data.field_of_view * (segment_idx + 1) / num_segments), base)); } b2FixtureDef fd; b2PolygonShape poly; transform_points(points, b2Transform(defn.pos, b2Rot(defn.orientation))); poly.Set(points.data(), points.size()); fd.shape = &poly; fd.isSensor = true; //fd.filter auto fix = defn.body->CreateFixture(&fd); set_fixture_data(fix, entity_fix_data{ entity_fix_data::Type::Sensor, entity_fix_data::val_t{ static_cast<contact_based_sensor*>(this) } }); } }
// this function returns -1 if two objects has collision // And otherwise returns the distance float PlanningProblem::distToObstacle(Station A, const Obstacle &ob, b2Vec2& A_point, b2Vec2& ob_point) { b2DistanceProxy state_proxy, ob_proxy; state_proxy.Set(agent->shape, 0); ob_proxy.Set(ob.shape, 1); b2DistanceInput dist_in; dist_in.proxyA = state_proxy; dist_in.proxyB = ob_proxy; dist_in.transformA = b2Transform(A.getPosition().toB2vec2(), b2Rot(A.getPosition().Teta())); dist_in.transformB = ob.transform; b2SimplexCache dist_cache; dist_cache.count = 0; b2DistanceOutput dis_out; b2Distance(&dis_out, &dist_cache, &dist_in); A_point = dis_out.pointA; ob_point = dis_out.pointB; if(hasCollision(A, ob)) { return -1; } return dis_out.distance; }
bool CoreShape_Imp::GetIsCollidedb2Shapes(CoreShape* shape) { auto shape_Imp = CoreShape2DToImp(shape); for (auto selfShape : GetCollisionShapes()) { for (auto theirsShape : shape_Imp->GetCollisionShapes()) { b2Transform identity = b2Transform(); identity.SetIdentity(); bool isOverlap = b2TestOverlap(selfShape, 1, theirsShape, 1, identity, identity); if (isOverlap) { return true; } } } return false; }
void Personaje::leermovimiento(int direccion, int id_jugador){ if (this->nro_jugador == id_jugador && seleccionado[id_jugador]){ if (direccion == 3 && body->GetLinearVelocity().x < 0.7){ // para la derecha dir_imagen = "TPTaller/imagenes/gusanitoderecha.png"; orientacion=1; this->movio = 1; this->mover(b2Vec2(2,0)); return; } if (direccion == 1 && body->GetLinearVelocity().x > -0.7 ){ // para la izquierda dir_imagen = "TPTaller/imagenes/gusanitoizquierda.png"; orientacion=-1; this->movio = 1; this->mover(b2Vec2(-2,0)); return; } b2Vec2 posicion = this->getPosition(); b2CircleShape* over = new b2CircleShape(); over->m_radius = 0.0001; b2Transform transformOver = b2Transform(posicion+b2Vec2(0,alto/2), b2Rot(0) ); b2World* mundo = body->GetWorld(); bool resultado; for (b2Body* body_actual = mundo->GetBodyList()->GetNext(); body_actual; body_actual = body_actual->GetNext()){ if (body_actual->GetPosition() == body->GetPosition()) continue; b2Transform transformada_actual = body_actual->GetTransform(); b2Fixture* fix = body_actual->GetFixtureList(); b2Shape* shape_actual = fix->GetShape(); resultado = b2TestOverlap(shape_actual,0, over , 0, transformada_actual, transformOver); if (resultado) break; } if(over) delete over; over = NULL; if((direccion == 2 && resultado) || ((direccion == 2) && (body->GetContactList() != NULL))){ // para arriba this->mover(b2Vec2(0,-3)); this->salto = 1; return; } if ( (direccion == 4 && body->GetLinearVelocity().x <0.7 ) && (body->GetContactList() != NULL) ){ // para arriba a la derecha this->mover(b2Vec2(2,-3)); this->salto = 1; // if(body->GetLinearVelocity().y == 0) // this->mover(b2Vec2(2,-3)); // else // this->mover(b2Vec2(1,-1)); // return; } if ( (direccion == 5 && body->GetLinearVelocity().x >-0.7 ) && (body->GetContactList() != NULL)){ // para arriba a la izq this->mover(b2Vec2(-2,-3)); this->salto = 1; // if(body->GetLinearVelocity().y == 0) // this->mover(b2Vec2(-2,-3)); // else // this->mover(b2Vec2(-1,-1)); // return; } if((direccion == 5) && (body->GetContactList() != NULL)){ // para arriba a la izq this->mover(b2Vec2(-2,-3)); this->salto = 1; } } }
void b2d_shape_test_point(int id, double sx, double sy, double sa, double px, double py) { get_shape(b2dshape, id); b2dshape->shape->TestPoint(b2Transform(b2Vec2(sx, sy), b2Rot(sa)), b2Vec2(px, py)); }