// ### Experimental racing function double race_to(double curr_coord[2], double x, double y){ double steering = 1.8; // Ratio between the speed of each wheels. double error_margin_angle = 3; // Degrees double speed = 70; double dx = x - curr_coord[0]; double dy = y - curr_coord[1]; double targetA = atan2(dx, dy); double dangle = targetA - face_angle; dangle += (dangle > to_rad(180)) ? -to_rad(360) : (dangle < -to_rad(180)) ? to_rad(360) : 0; double distance = fabs(sqrt(dx*dx + dy*dy)); // If we need to turn, perform steering maneuver if(fabs(dangle) >= to_rad(error_margin_angle)){ if (to_degree(dangle) > 0){ set_motors( speed*steering, speed/steering); } else { set_motors(speed/steering, speed*steering); } printf("Steering: %f \n", to_degree(dangle)); } else { // Otherwise just go straight; set_motors(speed, speed); } position_tracker(curr_coord); return distance; }
void test_azimuth(double lon1, double lat1, double lon2, double lat2, double expected_azimuth_deg) { // Set radius type, but for integer coordinates we want to have floating point radius type typedef typename bg::promote_floating_point < typename bg::coordinate_type<PS>::type >::type rtype; typedef bg::srs::spheroid<rtype> stype; typedef bg::detail::andoyer_inverse<rtype, false, true> andoyer_inverse_type; rtype a_formula = andoyer_inverse_type::apply(to_rad(lon1), to_rad(lat1), to_rad(lon2), to_rad(lat2), stype()).azimuth; rtype azimuth_deg = to_deg(a_formula); if (bg::math::equals(azimuth_deg, -180.0)) azimuth_deg = 180.0; if (bg::math::equals(expected_azimuth_deg, -180.0)) expected_azimuth_deg = 180.0; if (bg::math::equals(expected_azimuth_deg, 0.0)) { BOOST_CHECK(bg::math::equals(azimuth_deg, expected_azimuth_deg)); } else { BOOST_CHECK_CLOSE(azimuth_deg, expected_azimuth_deg, 0.001); } }
//Position tracking on servo encoder reading. double position_tracker(double curr_coord[2]) { // Function update global var face_angle. // Function update current coordinates. // Return the distance traveled. int currenc[2] = {0, 0}; get_motor_encoders(¤c[LEFT], ¤c[RIGHT]); double dl = enc_to_dist(currenc[LEFT] - prevenc[LEFT]); double dr = enc_to_dist(currenc[RIGHT] - prevenc[RIGHT]); double dx = 0, dy = 0; double dangle = ( dl - dr )/ width; if (dangle != 0) { double rl = dl / dangle; double rr = dr / dangle; double rm = ( rl + rr ) / 2; dx = rm * ( cos(face_angle) - cos(face_angle + dangle) ); dy = rm * ( sin(face_angle + dangle) - sin(face_angle) ); face_angle += dangle; face_angle += (face_angle > to_rad(180)) ? -to_rad(360) : (face_angle < -to_rad(180)) ? to_rad(360) : 0; } else { dy = dl * cos(face_angle); dx = dr * sin(face_angle); } // Encoders update prevenc[LEFT] = currenc[LEFT]; prevenc[RIGHT] = currenc[RIGHT]; // Coordinates update curr_coord[0] += dx; curr_coord[1] += dy; return sqrt(dx*dx + dy*dy); }
void draw_point(int x, int y, int radius) { SDL_Rect rect; int iterations; int i; //if (radius < 2) { // radius = 2; //} iterations = radius/2 + 1; for (i = 0; i < iterations; i++) { double angle; int ix; int iy; angle = (90.0 * (i + .5)) / (iterations); ix = radius * cos(to_rad(angle)); iy = radius * sin(to_rad(angle)); rect.x = x - ix; rect.y = y - iy; rect.w = 2*ix + 1; rect.h = 2*iy + 1; SDL_FillRect(screen, &rect, current_color); } }
//Draw tile highlight void draw_hilite(vector3f p){ static GLuint index; if( !index ){ index = glGenLists(1); glNewList(index,GL_COMPILE); int d = 360; float r = 0.3; glNormal3f(0,1,0); glBegin(GL_QUAD_STRIP); for (int i = 0; i < 7; ++i) { glVertex3f(r * 0.9 * cos(to_rad(d)), 0, r * 0.9 * sin(to_rad(d))); glVertex3f(r * cos(to_rad(d)), 0, r * sin(to_rad(d))); d -= 60; } glEnd(); glEndList(); } glPushMatrix(); glTranslatef(p.x,p.y + 0.01,p.z); glCallList(index); glPopMatrix(); }
void plot_vector(double x, double y, double angle, double magnitude) { double x2, y2; x2 = x + (cos(to_rad(angle)) * magnitude); y2 = y + (sin(to_rad(angle)) * magnitude); plot_line(x, y, x2, y2); }
int main(){ connect_to_robot(); initialize_robot(); set_origin(); double curr_coord[2] = {0, 0}; spin(curr_coord, to_rad(90)); sleep(1); spin(curr_coord, to_rad(180)); return 0; }
// Generates birds and places them at a random position with random heading Birds::Birds(){ B = new bird[MB]; for (int i = 0; i < MB; i++){ float radius = 5 * real_rand(); float rad = to_rad(rand()%360); vector3f h(cos(to_rad(rand()%360)),0,sin(to_rad(rand()%360))); h = h.unit() * 3.0f; B[i].pos.set(radius * cos(rad), real_rand() * 0.3f + 0.3f, radius * sin(rad)); B[i].heading.set(h.x,h.y,h.z); } }
void move_to(double curr_coord[2], double x, double y){ double dx = x - curr_coord[0]; double dy = y - curr_coord[1]; double targetA = atan2(dx, dy); double dangle = targetA - face_angle; dangle += (dangle > to_rad(180)) ? -to_rad(360) : (dangle < -to_rad(180)) ? to_rad(360) : 0; spin(curr_coord, dangle); go_straight(curr_coord, fabs(sqrt(dx*dx + dy*dy))); printf("Moving Done : X = %f, Y = %f, face_angle = %f \n", curr_coord[0], curr_coord[1], to_degree(face_angle)); }
/** @brief performs a axis angle rotation around the arbitrary axis (_x,_y,_z) @param r the result matrix @param m the target matrix @param angle the angle (in degrees) to rotate @param _x the x component of the vector to use as a axis @param _y the y component of the vector to use as a axis @param _z the z component of the vector to use as a axis */ void rotateMM(float *r,float *m, float angle, float _x, float _y, float _z){ float s = sin(to_rad(angle)); float c = cos(to_rad(angle)); float magnitude = sqrt(_x*_x + _y*_y + _z*_z); float x = _x / magnitude; float y = _y / magnitude; float z = _z / magnitude; float rotationMatrix[16] = {c+(x*x)*(1-c),y*x*(1-c)+z*s, z*x*(1-c) - y*s,0, y*x*(1-c)-z*s,c+(y*y)*(1-c), z*y*(1-c) + x*s,0, z*x*(1-c)+y*s,y*z*(1-c)-x*s, c+z*z*(1-c) ,0, 0,0,0,1}; multiplyMM(r, m, rotationMatrix); return; }
//=========== void rotate() { uchar speed_flag; target_angle = ((long)target_angle) % 360; #ifdef debug_printf printf("ROT: deg %.2f", target_angle); #endif target_angle = to_rad(target_angle); //target_angle = target_angle%(2*M_PI); if(target_angle>=0) { if(target_angle<=M_PI) { speed_flag=FLAG_RIGHT; } else { speed_flag=FLAG_LEFT; target_angle=2*M_PI-target_angle; } } else { target_angle=-target_angle; if(target_angle<=M_PI) { speed_flag=FLAG_LEFT; } else { speed_flag=FLAG_RIGHT; target_angle=2*M_PI-target_angle; } } #ifdef debug_printf printf(" rad %.2f flag %d\n", target_angle, speed_flag); #endif if(target_angle>(to_rad(ANGLE_TRESHOLD))) { //set_motors_speed_dir(ROTATION_SPEED,ROTATION_SPEED, speed_flag); while((target_angle-abs(me.angle))>to_rad(ANGLE_OVERFLOW)) //including inertion overturn ~23.3 degrees (speed~70) { set_motors_speed_dir(ROTATION_SPEED, ROTATION_SPEED, speed_flag); usleep(MOTOR_SLEEP); } stop_motors(); } }
CoordinateGridBasicTest() : la00lo00(0,0), la10lo10(to_rad(10),to_rad(10)), nullposition(), nullposition_init(0.0,0.0), northpole(to_rad( 90.0),0.0), southpole(to_rad(-90.0),0.0), sw(to_rad(55),to_rad(16)), ne(to_rad(59.5),to_rad(16.5)), center(0.0,0.0) { vdirection d = inverse(sw,ne); center = direct(sw,d.bearing1,d.distance/2.0); }
void draw_still_water(){ static GLuint index; if(!index){ index = glGenLists(1); glNewList(index,GL_COMPILE); int rate = 10, r = 80; glNormal3f(0,1,0); glColor4ub(137,205,252,200); glBegin(GL_TRIANGLE_FAN); glVertex3f(0,-1,0); for( int i = 0; i >= -360; i -= rate ){ vector3f p(r * cos(to_rad(i)),-1, r * sin(to_rad(i))); glVertex3f(p.x,p.y,p.z); } glEnd(); glEndList(); } glDisable(GL_FOG); glCallList(index); glEnable(GL_FOG); }
void test_distance(double lon1, double lat1, double lon2, double lat2, double expected_km) { // Set radius type, but for integer coordinates we want to have floating point radius type typedef typename bg::promote_floating_point < typename bg::coordinate_type<P1>::type >::type rtype; typedef bg::srs::spheroid<rtype> stype; typedef bg::strategy::distance::andoyer<stype> andoyer_type; typedef bg::detail::andoyer_inverse<rtype, true, false> andoyer_inverse_type; BOOST_CONCEPT_ASSERT ( (bg::concepts::PointDistanceStrategy<andoyer_type, P1, P2>) ); andoyer_type andoyer; typedef typename bg::strategy::distance ::services::return_type<andoyer_type, P1, P2>::type return_type; P1 p1; P2 p2; bg::assign_values(p1, lon1, lat1); bg::assign_values(p2, lon2, lat2); return_type d_strategy = andoyer.apply(p1, p2); return_type d_function = bg::distance(p1, p2, andoyer); return_type d_formula = andoyer_inverse_type::apply(to_rad(lon1), to_rad(lat1), to_rad(lon2), to_rad(lat2), stype()).distance; BOOST_CHECK_CLOSE(d_strategy / 1000.0, expected_km, 0.001); BOOST_CHECK_CLOSE(d_function / 1000.0, expected_km, 0.001); BOOST_CHECK_CLOSE(d_formula / 1000.0, expected_km, 0.001); }
int main() { double n, topx, topy, btmx, btmy, scale; while( scanf( "%lf", &n ) == 1 ) { scale = sin( to_rad( 9 ) ) / sin( to_rad( 63 ) ); topx = n/2; topy = tan( to_rad( 72 ) ) * topx; btmx = ( cos( to_rad( 108 ) ) + scale * cos( to_rad( 72 ) ) ) * n; btmy = ( sin( to_rad( 108 ) ) - scale * sin( to_rad( 72 ) ) ) * n; printf( "%.10f\n", hypot( topx - btmx, topy - btmy ) ); } }
int main(){ connect_to_robot(); initialize_robot(); set_origin(); set_ir_angle(LEFT, -45); set_ir_angle(RIGHT, 45); initialize_maze(); reset_motor_encoders(); int i; for (i = 0; i < 17; i++){ set_point(nodes[i]->x, nodes[i]->y); } double curr_coord[2] = {0, 0}; map(curr_coord, nodes[0]); breadthFirstSearch(nodes[0]); reversePath(nodes[16]); printPath(nodes[0]); struct point* tail = malloc(sizeof(struct point)); tail->x = nodes[0]->x; tail->y = nodes[0]->y; struct point* startpoint = tail; build_path(tail, nodes[0]); // Traverse to end node. while(tail->next){ set_point(tail->x, tail->y); // Visual display for Simulator only. tail = tail->next; } tail->next = NULL; // Final node point to null. printf("tail: X = %f Y = %f \n", tail->x, tail->y); parallel(curr_coord); spin(curr_coord, to_rad(180)); sleep(2); set_ir_angle(LEFT, 45); set_ir_angle(RIGHT, -45); mazeRace(curr_coord, nodes[0]); return 0; }
int point4d_transform(POINT4D *pt, projPJ srcpj, projPJ dstpj) { int* pj_errno_ref; POINT4D orig_pt; /* Make a copy of the input point so we can report the original should an error occur */ orig_pt.x = pt->x; orig_pt.y = pt->y; orig_pt.z = pt->z; if (pj_is_latlong(srcpj)) to_rad(pt) ; LWDEBUGF(4, "transforming POINT(%f %f) from '%s' to '%s'", orig_pt.x, orig_pt.y, pj_get_def(srcpj,0), pj_get_def(dstpj,0)); /* Perform the transform */ pj_transform(srcpj, dstpj, 1, 0, &(pt->x), &(pt->y), &(pt->z)); /* For NAD grid-shift errors, display an error message with an additional hint */ pj_errno_ref = pj_get_errno_ref(); if (*pj_errno_ref != 0) { if (*pj_errno_ref == -38) { lwnotice("PostGIS was unable to transform the point because either no grid shift files were found, or the point does not lie within the range for which the grid shift is defined. Refer to the ST_Transform() section of the PostGIS manual for details on how to configure PostGIS to alter this behaviour."); lwerror("transform: couldn't project point (%g %g %g): %s (%d)", orig_pt.x, orig_pt.y, orig_pt.z, pj_strerrno(*pj_errno_ref), *pj_errno_ref); return 0; } else { lwerror("transform: couldn't project point (%g %g %g): %s (%d)", orig_pt.x, orig_pt.y, orig_pt.z, pj_strerrno(*pj_errno_ref), *pj_errno_ref); return 0; } } if (pj_is_latlong(dstpj)) to_dec(pt); return 1; }
void spin(double curr_coord[2], double angle){ if (angle == 0) return; printf("Start Spinning: from %f, with :%f\n",to_degree(face_angle), to_degree(angle)); double initial_angle = face_angle; int speed = 15; int tempspeed = 0; double angle_turned = 0; double abs_angle = fabs(angle); double side = angle/fabs(angle); int tempenc[2] = {0, 0}; while (angle_turned < abs_angle){ get_motor_encoders(&tempenc[0], &tempenc[1]); double dl = enc_to_dist(tempenc[0] - prevenc[0]); double dr = enc_to_dist(tempenc[1] - prevenc[1]); face_angle += ( dl - dr )/ width; if (abs_angle < to_rad(30)){ tempspeed = side * 1; set_motors(tempspeed, -tempspeed); } else if(angle_turned >= fabs(0.92*angle)){ tempspeed = (tempspeed < 2) ? side : side * speed * (1 - fabs(angle_turned/angle)); set_motors(tempspeed, -tempspeed); } else { set_motors(side*speed, side*-speed); } angle_turned = fabs(face_angle - initial_angle); prevenc[0] = tempenc[0]; prevenc[1] = tempenc[1]; //printf("Monitoring: angle: %f X: %f , Y: %f \n", to_degree(face_angle), curr_coord[0], curr_coord[1] ); } position_tracker(curr_coord); set_motors(0, 0); printf("(Spinning done ! %f \n", to_degree(face_angle) ); usleep(10000); }
//=============== void go_forward() { float angle_diff; uchar lspeed, rspeed, speed_flag, current_speed=0; #ifdef debug_printf printf("MOV: %f\n",target_distance); #endif if(target_distance>=0) { speed_flag=FLAG_FORWARD; } else { target_distance=-target_distance; speed_flag=FLAG_BACKWARD; } if(target_distance>INERTION) { while(moving_allowed&&(me.dist_from_start<(target_distance-INERTION))) { //acceleration if(current_speed<input_speed) { current_speed += ACCELERATION; if(current_speed>input_speed) current_speed = input_speed; lspeed=rspeed=current_speed; } else { lspeed=rspeed=input_speed; // speed-trajectory correction angle_diff = target_angle-me.angle; if(angle_diff>to_rad(ANGLE_DIFF)) { if(speed_flag==FLAG_FORWARD) { lspeed += 5; //lspeed += (2*ANGLE_CORRECTION*angle_diff)/M_PI; rspeed -= 15; //rspeed -= (2*ANGLE_CORRECTION*angle_diff)/M_PI; } if(speed_flag==FLAG_BACKWARD) { rspeed += 5; lspeed -= 15; } } if(angle_diff<-to_rad(ANGLE_DIFF)) { if(speed_flag==FLAG_FORWARD) { rspeed += 5; //rspeed += (2*ANGLE_CORRECTION*angle_diff)/M_PI; lspeed -= 15; //lspeed -= (2*ANGLE_CORRECTION*angle_diff)/M_PI; } if(speed_flag==FLAG_BACKWARD) { lspeed += 5; rspeed -= 15; } } } set_motors_speed_dir(lspeed, rspeed, speed_flag); usleep(MOTOR_SLEEP); } // decceleration while(moving_allowed&&(current_speed>0)) { current_speed -= ACCELERATION<<1; if(current_speed<0) current_speed = 0; lspeed = rspeed = current_speed; } stop_motors(); } }
shooting(main_window *w): window(*w), config(keyconfig::get_instance()), holder(std::make_shared<resource_holder>()), player_(holder) { register_resource(holder.get()); register_object(&dialog); dialog.set_button_text(0, L"はい"); dialog.set_button_handler(0, [this](){ this->dialog.hide(); on_hide(); on_show(); }); dialog.set_button_text(1, L"いいえ"); dialog.set_button_handler(1, [this](){ this->dialog.hide(); this->window.select_scene(main_window::scene::title); }); dialog.set_size(paint::size(300, 200)); dialog.set_position(paint::point(250, 200)); add_keyboard_handler([this](unsigned keycode, bool push){ config->set_keyboard_state(keycode, push); }, keycode_range(0, 255)); add_timer_handler([this](...){ if(pause) return; auto state = config->get_state(); vector v; const float velocity = 7.f; if(state.up){ v[1][0] -= velocity; } if(state.down){ v[1][0] += velocity; } if(state.left){ v[0][0] -= velocity; } if(state.right){ v[0][0] += velocity; } player_.set_vector(v); auto now = std::chrono::system_clock::now(); if(state.b && now - bullet_time >= std::chrono::milliseconds(192)){ auto it = std::find(bullets.begin(), bullets.end(), false); if(it != bullets.end()){ float r = it->get_radius(); it->set_position(player_.get_center()); it->set_vector(vector(0, -10)); it->set_active(); auto trans = transform::scale(1.f, 1.01f); if(state.a){ trans = transform::rotation(to_rad(-0.5)) * trans; } if(state.c){ trans = transform::rotation(to_rad(0.5)) * trans; } it->set_transform(trans); } bullet_time = now; } if(now - enemy_time >= enemy_duration){ enemy_duration = std::chrono::milliseconds(this->rand() % 750 + 250); enemy_time = now; enemies.push_back(new enemy1(holder)); enemies.back()->set_position(paint::point(static_cast<float>(this->rand() % 800), -50)); } move(); this->window.repaint(); }, timer_id); }
#include <y/test/test.h> namespace { using namespace y; using namespace y::math; y_test_func("Quaternion from_euler pitch") { auto q = Quaternion<>::from_euler(to_rad(90), 0, 0); y_test_assert(q({1.0f, 0.0f, 0.0f}).z() < -0.99f); y_test_assert(q({0.0f, 0.0f, 1.0f}).x() > 0.99f); y_test_assert(to_deg(q.pitch()) == 90.0f); } y_test_func("Quaternion from_euler yaw") { auto q = Quaternion<>::from_euler(0, to_rad(90), 0); y_test_assert(q({1.0f, 0.0f, 0.0f}).y() > 0.99f); y_test_assert(q({0.0f, 0.0f, 1.0f}).z() > 0.99f); y_test_assert(to_deg(q.yaw()) == 90.0f); } y_test_func("Quaternion from_euler roll") { auto q = Quaternion<>::from_euler(0, 0, to_rad(90)); y_test_assert(q({1.0f, 0.0f, 0.0f}).x() > 0.99f); y_test_assert(q({0.0f, 0.0f, 1.0f}).y() < -0.99f); y_test_assert(to_deg(q.roll()) == 90.0f); } y_test_func("Quaternion default values") {
void Game::draw_skybox(){ static GLuint index; if( !index ){ int s = 100; float offset = s/2.0f; index = glGenLists(1); glNewList(index,GL_COMPILE); glRotatef(90,0,1,0); glColor3f(1,1,1); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, tex[0]); glBegin(GL_QUADS); glTexCoord2d(0.0,0.0); glVertex3f(-s,s,s); glTexCoord2d(0.0,1.0); glVertex3f(-s,s,-s); glTexCoord2d(1.0,1.0); glVertex3f(s,s,-s); glTexCoord2d(1.0,0.0); glVertex3f(s,s,s); glEnd(); glBindTexture(GL_TEXTURE_2D, tex[1]); glBegin(GL_QUADS); glTexCoord2d(0.0,0.0); glVertex3f(-s,s,s); glTexCoord2d(0.0,1.0); glVertex3f(-s,-s,s); glTexCoord2d(1.0,1.0); glVertex3f(-s,-s,-s); glTexCoord2d(1.0,0.0); glVertex3f(-s,s,-s); glEnd(); glBindTexture(GL_TEXTURE_2D, tex[2]); glBegin(GL_QUADS); glTexCoord2d(0.0,0.0); glVertex3f(-s,s,-s); glTexCoord2d(0.0,1.0); glVertex3f(-s,-s,-s); glTexCoord2d(1.0,1.0); glVertex3f(s,-s,-s); glTexCoord2d(1.0,0.0); glVertex3f(s,s,-s); glEnd(); glBindTexture(GL_TEXTURE_2D, tex[3]); glBegin(GL_QUADS); glTexCoord2d(0.0,0.0); glVertex3f(s,s,-s); glTexCoord2d(0.0,1.0); glVertex3f(s,-s,-s); glTexCoord2d(1.0,1.0); glVertex3f(s,-s,s); glTexCoord2d(1.0,0.0); glVertex3f(s,s,s); glEnd(); glBindTexture(GL_TEXTURE_2D, tex[4]); glBegin(GL_QUADS); glTexCoord2d(0.0,0.0); glVertex3f(s,s,s); glTexCoord2d(0.0,1.0); glVertex3f(s,-s,s); glTexCoord2d(1.0,1.0); glVertex3f(-s,-s,s); glTexCoord2d(1.0,0.0); glVertex3f(-s,s,s); glEnd(); glBindTexture(GL_TEXTURE_2D, tex[5]); glBegin(GL_QUADS); glTexCoord2d(0.0,0.0); glVertex3f(-s,-s,-s); glTexCoord2d(0.0,1.0); glVertex3f(-s,-s,s); glTexCoord2d(1.0,1.0); glVertex3f(s,-s,s); glTexCoord2d(1.0,0.0); glVertex3f(s,-s,-s); glEnd(); glColor4f(1,1,1,0.8); glBindTexture(GL_TEXTURE_2D, tex[6]); glBegin(GL_QUADS); int rate = 10; int r = 80; for( int i = 0; i < 360; i += rate ){ vector3f p(r * cos(to_rad(i)), 22, r * sin(to_rad(i))); glTexCoord2d(0.0,0.0); glVertex3f(p.x,p.y,p.z); glTexCoord2d(0.0,1.0); glVertex3f(p.x,p.y-30,p.z); p.set(r * cos(to_rad(i+rate)), 22, r * sin(to_rad(i+rate))); glTexCoord2d(1.0,1.0); glVertex3f(p.x,p.y-30,p.z); glTexCoord2d(1.0,0.0); glVertex3f(p.x,p.y,p.z); } glEnd(); glDisable(GL_TEXTURE_2D); glEndList(); } glDisable(GL_FOG); glPushMatrix(); vector3f p = camera.get_pos(); glTranslatef(p.x,-10,p.z); glCallList(index); glPopMatrix(); glEnable(GL_FOG); }