Bullet::Bullet(Game *game, Level *level, Player *player) : BaseEntity(game, level) { entity_type = TYPE_BULLET; collision_check = true; collision_radius = 1; rotation = player->rotation; // Calculate velocity depending on player rotation and velocity velocity.x = fast_cos(rotation) * DEFAULT_VELOCITY + player->velocity.x; velocity.y = fast_sin(rotation) * DEFAULT_VELOCITY + player->velocity.y; position.x = player->position.x + fast_cos(rotation) * 25; position.y = player->position.y + fast_sin(rotation) * 25; }
/* * euler2quat - Convert Pitch, Roll and Yaw to quaternion * @param[in] roll Roll [rad] * @param[in] pitch Pitch [rad] * @param[in] yaw Yaw [rad] * @param[out] q Quaternion */ void euler2quat(float roll, float pitch, float yaw, quaternion_t *q) { /* Prepare the angles for conversion to quaternions */ pitch *= 0.5f; roll *= 0.5f; yaw *= 0.5f; q->q0 = fast_cos(roll) * fast_cos(pitch) * fast_cos(yaw) + fast_sin(roll) * fast_sin(pitch) * fast_sin(yaw); q->q1 = fast_sin(roll) * fast_cos(pitch) * fast_cos(yaw) - fast_cos(roll) * fast_sin(pitch) * fast_sin(yaw); q->q2 = fast_cos(roll) * fast_sin(pitch) * fast_cos(yaw) + fast_sin(roll) * fast_cos(pitch) * fast_sin(yaw); q->q3 = fast_cos(roll) * fast_cos(pitch) * fast_sin(yaw) - fast_sin(roll) * fast_sin(pitch) * fast_cos(yaw); }
void Player::update(const float& delta) { if (game_->nunchuck->button_z() && !dead_) { // add relative velocity so you get a drag effect velocity.y += fast_sin(rotation) * acceleration_ * delta; velocity.x += fast_cos(rotation) * acceleration_ * delta; } else { velocity += -velocity * deceleration_ * delta; } // Check if cooldown is active if (firing_cooldown_ > 0) { firing_cooldown_ -= delta; } // If cooldown isn't active, fire a bullet if (game_->nunchuck->button_c() && firing_cooldown_ <= 0 && !dead_) { level_->addEntity(new Bullet(game_, level_, this)); firing_cooldown_ = FIRING_COOLDOWN; } // if speed is faster than max speed, clamp it if (velocity.length_sq() > square(max_speed_)) { velocity = velocity.normalized() * max_speed_; } // add rotations if(!dead_){ rotation += game_->nunchuck->joystick().x * rotation_speed_ * delta; } position = position + velocity * delta; }
/** * Draw ball animation */ void draw_balls(SDL_Surface* screen, int time) { SDL_Rect spritepos; // Clear screen SDL_FillRect(screen, NULL, 0x00000000); // Draw balls for(int i = 0; i < NUM_BALLS; i++) { // Advance ball balls[i].x += balls[i].speedx; balls[i].angle += balls[i].dangle; balls[i].amplitude *= 0.995; spritepos.x = balls[i].x; spritepos.y = HEIGHT - 32 - abs(balls[i].amplitude * fast_sin((balls[i].angle / 180.0) * MATH_PI)); SDL_BlitSurface(ball_surface[balls[i].color], NULL, screen, &spritepos); // Reset ball if needed if(balls[i].x > WIDTH) reset_ball(i); } }
/** * Create a ball sprite */ SDL_Surface* sprite_create_ball(float rparam, float gparam, float bparam) { SDL_Surface* temp = SDL_CreateRGBSurface(SDL_SWSURFACE, 32, 32, 32, rmask, gmask, bmask, amask); for(int y = 0; y < 32; y++) for(int x = 0; x < 32; x++) sdl_putpixel(temp, x, y, rparam * 240 * fast_sin((float) (x / 32.0) * MATH_PI) * fast_sin((float) (y / 32.0) * MATH_PI), gparam * 240 * fast_sin((float) (x / 32.0) * MATH_PI) * fast_sin((float) (y / 32.0) * MATH_PI), bparam * 240 * fast_sin((float) (x / 32.0) * MATH_PI) * fast_sin((float) (y / 32.0) * MATH_PI), 240 * fast_sin((float) (x / 32.0) * MATH_PI) * fast_sin((float) (y / 32.0) * MATH_PI)); return temp; }
void nglRotateZ(const GLFix a) { MATRIX rot; const GLFix sina = fast_sin(a), cosa = fast_cos(a); M(rot, 0, 0) = cosa; M(rot, 0, 1) = -sina; M(rot, 1, 0) = sina; M(rot, 1, 1) = cosa; M(rot, 2, 2) = 1; M(rot, 3, 3) = 1; nglMultMatMat(transformation, &rot); }
END_TEST START_TEST(Maximum_absolute_error_is_small) { static const double small = 0.0011; static const int32_t test_count = 1048577; for (int32_t i = 0; i < test_count; ++i) { const double x = 2.0 * PI * (double)i / (double)(test_count - 1); const double result = fast_sin(x); const double std_sin = sin(x); const double abs_error = fabs(result - std_sin); fail_unless(abs_error <= small, "fast_sin(2 * pi * %" PRId32 " / %" PRId32 ") = fast_sin(%.17f)" " yields %.17f, which is %.17f from %.17f", i, (test_count - 1), x, result, abs_error, std_sin); } }
int16_t fast_cos(int16_t deg) { return fast_sin(90+deg); }
void WorldTask::logic() { GLFix dx = 0, dz = 0; if(keyPressed(KEY_NSPIRE_8)) //Forward { GLFix dx1, dz1; getForward(&dx1, &dz1); dx += dx1; dz += dz1; } else if(keyPressed(KEY_NSPIRE_2)) //Backward { GLFix dx1, dz1; getForward(&dx1, &dz1); dx -= dx1; dz -= dz1; } if(keyPressed(KEY_NSPIRE_4)) //Left { GLFix dx1, dz1; getRight(&dx1, &dz1); dx -= dx1; dz -= dz1; } else if(keyPressed(KEY_NSPIRE_6)) //Right { GLFix dx1, dz1; getRight(&dx1, &dz1); dx += dx1; dz += dz1; } if(!world.intersect(aabb)) { AABB aabb_moved = aabb; aabb_moved.low_x += dx; aabb_moved.high_x += dx; if(!world.intersect(aabb_moved)) { x += dx; aabb = aabb_moved; } aabb_moved = aabb; aabb_moved.low_z += dz; aabb_moved.high_z += dz; if(!world.intersect(aabb_moved)) { z += dz; aabb = aabb_moved; } aabb_moved = aabb; aabb_moved.low_y += vy; aabb_moved.high_y += vy; can_jump = world.intersect(aabb_moved); if(!can_jump) { y += vy; aabb = aabb_moved; } else if(vy > GLFix(0)) { can_jump = false; vy = 0; } else vy = 0; vy -= 5; in_water = getBLOCK(world.getBlock((x / BLOCK_SIZE).floor(), ((y + eye_pos) / BLOCK_SIZE).floor(), (z / BLOCK_SIZE).floor())) == BLOCK_WATER; if(in_water) can_jump = true; } if(keyPressed(KEY_NSPIRE_5) && can_jump) //Jump { vy = 50; can_jump = false; } if(has_touchpad) { touchpad_report_t touchpad; touchpad_scan(&touchpad); if(touchpad.pressed) { switch(touchpad.arrow) { case TPAD_ARROW_DOWN: xr += speed()/3; break; case TPAD_ARROW_UP: xr -= speed()/3; break; case TPAD_ARROW_LEFT: yr -= speed()/3; break; case TPAD_ARROW_RIGHT: yr += speed()/3; break; case TPAD_ARROW_RIGHTDOWN: xr += speed()/3; yr += speed()/3; break; case TPAD_ARROW_UPRIGHT: xr -= speed()/3; yr += speed()/3; break; case TPAD_ARROW_DOWNLEFT: xr += speed()/3; yr -= speed()/3; break; case TPAD_ARROW_LEFTUP: xr -= speed()/3; yr -= speed()/3; break; } } else if(tp_had_contact && touchpad.contact) { yr += (touchpad.x - tp_last_x) / 50; xr -= (touchpad.y - tp_last_y) / 50; } tp_had_contact = touchpad.contact; tp_last_x = touchpad.x; tp_last_y = touchpad.y; } else { if(keyPressed(KEY_NSPIRE_UP)) xr -= speed()/3; else if(keyPressed(KEY_NSPIRE_DOWN)) xr += speed()/3; if(keyPressed(KEY_NSPIRE_LEFT)) yr -= speed()/3; else if(keyPressed(KEY_NSPIRE_RIGHT)) yr += speed()/3; } //Normalisation required for rotation with nglRotate yr.normaliseAngle(); xr.normaliseAngle(); //xr and yr are normalised, so we can't test for negative values if(xr > GLFix(180)) if(xr <= GLFix(270)) xr = 269; if(xr < GLFix(180)) if(xr >= GLFix(90)) xr = 89; //Do test only on every second frame, it's expensive if(do_test) { GLFix dx = fast_sin(yr)*fast_cos(xr), dy = -fast_sin(xr), dz = fast_cos(yr)*fast_cos(xr); GLFix dist; if(!world.intersectsRay(x, y + eye_pos, z, dx, dy, dz, selection_pos, selection_side, dist, in_water)) selection_side = AABB::NONE; else selection_pos_abs = {x + dx * dist, y + eye_pos + dy * dist, z + dz * dist}; } world.setPosition(x, y, z); do_test = !do_test; if(key_held_down) key_held_down = keyPressed(KEY_NSPIRE_ESC) || keyPressed(KEY_NSPIRE_7) || keyPressed(KEY_NSPIRE_9) || keyPressed(KEY_NSPIRE_1) || keyPressed(KEY_NSPIRE_3) || keyPressed(KEY_NSPIRE_PERIOD) || keyPressed(KEY_NSPIRE_MINUS) || keyPressed(KEY_NSPIRE_PLUS) || keyPressed(KEY_NSPIRE_MENU); else if(keyPressed(KEY_NSPIRE_ESC)) //Save & Exit { save(); Task::running = false; return; } else if(keyPressed(KEY_NSPIRE_7)) //Put block down { if(selection_side != AABB::NONE) { if(!world.blockAction(selection_pos.x, selection_pos.y, selection_pos.z)) { VECTOR3 pos = selection_pos; switch(selection_side) { case AABB::BACK: ++pos.z; break; case AABB::FRONT: --pos.z; break; case AABB::LEFT: --pos.x; break; case AABB::RIGHT: ++pos.x; break; case AABB::BOTTOM: --pos.y; break; case AABB::TOP: ++pos.y; break; default: puts("This can't normally happen #1"); break; } if(!world.intersect(aabb)) { //Only set the block if there's air const BLOCK_WDATA current_block = world.getBlock(pos.x, pos.y, pos.z); if(current_block == BLOCK_AIR || (in_water && getBLOCK(current_block) == BLOCK_WATER)) { if(!global_block_renderer.isOriented(current_inventory.currentSlot())) world.changeBlock(pos.x, pos.y, pos.z, current_inventory.currentSlot()); else { AABB::SIDE side = selection_side; //If the block is not fully oriented and has been placed on top or bottom of another block, determine the orientation by yr if(!global_block_renderer.isFullyOriented(current_inventory.currentSlot()) && (side == AABB::TOP || side == AABB::BOTTOM)) side = yr < GLFix(45) ? AABB::FRONT : yr < GLFix(135) ? AABB::LEFT : yr < GLFix(225) ? AABB::BACK : yr < GLFix(315) ? AABB::RIGHT : AABB::FRONT; world.changeBlock(pos.x, pos.y, pos.z, getBLOCKWDATA(current_inventory.currentSlot(), side)); //AABB::SIDE is compatible to BLOCK_SIDE } //If the player is stuck now, it's because of the block change, so remove it again if(world.intersect(aabb)) world.changeBlock(pos.x, pos.y, pos.z, BLOCK_AIR); } } } } key_held_down = true; } else if(keyPressed(KEY_NSPIRE_9)) //Remove block { if(selection_side != AABB::NONE && world.getBlock(selection_pos.x, selection_pos.y, selection_pos.z) != BLOCK_BEDROCK) world.changeBlock(selection_pos.x, selection_pos.y, selection_pos.z, BLOCK_AIR); key_held_down = true; } else if(keyPressed(KEY_NSPIRE_1)) //Switch inventory slot { current_inventory.previousSlot(); key_held_down = true; } else if(keyPressed(KEY_NSPIRE_3)) { current_inventory.nextSlot(); key_held_down = true; } else if(keyPressed(KEY_NSPIRE_PERIOD)) //Open list of blocks (or take screenshot with Ctrl + .) { if(keyPressed(KEY_NSPIRE_CTRL)) { //Find a filename that doesn't exist char buf[45]; unsigned int i; for(i = 0; i <= 999; ++i) { sprintf(buf, "/documents/ndless/screenshot_%d.ppm.tns", i); struct stat stat_buf; if(stat(buf, &stat_buf) != 0) break; } if(i > 999 || !saveTextureToFile(*screen, buf)) setMessage("Screenshot failed!"); else { sprintf(message, "Screenshot taken (%d)!", i); message_timeout = 20; } } else block_list_task.makeCurrent(); key_held_down = true; } else if(keyPressed(KEY_NSPIRE_MINUS)) //Decrease max view distance { int fov = world.fieldOfView() - 1; world.setFieldOfView(fov < 1 ? 1 : fov); key_held_down = true; } else if(keyPressed(KEY_NSPIRE_PLUS)) //Increase max view distance { world.setFieldOfView(world.fieldOfView() + 1); key_held_down = true; } else if(keyPressed(KEY_NSPIRE_MENU)) { menu_task.makeCurrent(); key_held_down = true; } }
void WorldTask::getRight(GLFix *x, GLFix *z) { *x = GLFix(fast_sin((yr + 90).normaliseAngle())) * speed(); *z = GLFix(fast_cos((yr + 90).normaliseAngle())) * speed(); }
void WorldTask::getForward(GLFix *x, GLFix *z) { *x = GLFix(fast_sin(yr)) * speed(); *z = GLFix(fast_cos(yr)) * speed(); }
static PyObject * py_noise2(PyObject *self, PyObject *args, PyObject *kwargs) { float x, y; int octaves = 1; float persistence = 0.5f; float lacunarity = 2.0f; float repeatx = FLT_MAX; float repeaty = FLT_MAX; float z = 0.0f; static char *kwlist[] = {"x", "y", "octaves", "persistence", "lacunarity", "repeatx", "repeaty", "base", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ff|ifffff:snoise2", kwlist, &x, &y, &octaves, &persistence, &lacunarity, &repeatx, &repeaty, &z)) { return NULL; } if (octaves <= 0) { PyErr_SetString(PyExc_ValueError, "Expected octaves value > 0"); return NULL; } if (repeatx == FLT_MAX && repeaty == FLT_MAX) { // Flat noise, no tiling float freq = 1.0f; float amp = 1.0f; float max = 1.0f; float total = noise2(x + z, y + z); int i; for (i = 1; i < octaves; i++) { freq *= lacunarity; amp *= persistence; max += amp; total += noise2(x * freq + z, y * freq + z) * amp; } return (PyObject *) PyFloat_FromDouble((double) (total / max)); } else { // Tiled noise float w = z; if (repeaty != FLT_MAX) { float yf = y * 2.0 / repeaty; float yr = repeaty * M_1_PI * 0.5; float vy = fast_sin(yf); float vyz = fast_cos(yf); y = vy * yr; w += vyz * yr; if (repeatx == FLT_MAX) { return (PyObject *) PyFloat_FromDouble( (double) fbm_noise3(x, y, w, octaves, persistence, lacunarity)); } } if (repeatx != FLT_MAX) { float xf = x * 2.0 / repeatx; float xr = repeatx * M_1_PI * 0.5; float vx = fast_sin(xf); float vxz = fast_cos(xf); x = vx * xr; z += vxz * xr; if (repeaty == FLT_MAX) { return (PyObject *) PyFloat_FromDouble( (double) fbm_noise3(x, y, z, octaves, persistence, lacunarity)); } } return (PyObject *) PyFloat_FromDouble( (double) fbm_noise4(x, y, z, w, octaves, persistence, lacunarity)); } }
inline float fast_cos(float x) { return fast_sin(x + anglef::pi2); }
void magicmirror_apply( VJFrame *frame, int w, int h, int vx, int vy, int d, int n ) { double c1 = (double)vx; double c2 = (double)vy; int motion = 0; if( motionmap_active()) { motionmap_scale_to( 100,100,0,0, &d, &n, &n__, &N__ ); motion = 1; } else { n__ = 0; N__ = 0; } double c3 = (double)d * 0.001; unsigned int dx,dy,x,y,p,q,len=w*h; double c4 = (double)n * 0.001; int changed = 0; uint8_t *Y = frame->data[0]; uint8_t *Cb= frame->data[1]; uint8_t *Cr= frame->data[2]; int interpolate = 1; if( n__ == N__ || n__ == 0) interpolate = 0; if( d != last[1] ) { changed = 1; last[1] =d; } if( n != last[0] ) { changed = 1; last[0] = n; } if(changed==1) { // degrees x or y changed, need new sin for(x=0; x < w ; x++) { double res; fast_sin(res,(double)(c3*x)); funhouse_x[x] = res; //funhouse_x[x] = sin(c3 * x); } for(y=0; y < h; y++) { double res; fast_sin(res,(double)(c4*y)); funhouse_y[y] = res; //funhouse_y[y] = sin(c4 * y); } } int strides[4] = { len,len,len, 0 }; vj_frame_copy( frame->data, magicmirrorbuf, strides ); for(x=0; x < w; x++) { dx = x + funhouse_x[x] * c1; if(dx < 0) dx += w; if(dx < 0) dx = 0; else if (dx >= w) dx = w-1; cache_x[x] = dx; } for(y=0; y < h; y++) { dy = y + funhouse_y[y] * c2; if(dy < 0) dy += h; if(dy < 0) dy = 0; else if (dy >= h) dy = h-1; cache_y[y] = dy; } for(y=1; y < h-1; y++) { for(x=1; x < w-1; x++) { p = cache_y[y] * w + cache_x[x]; q = y * w + x; Y[q] = magicmirrorbuf[0][p]; Cb[q] = magicmirrorbuf[1][p]; Cr[q] = magicmirrorbuf[2][p]; } } if( interpolate ) { motionmap_interpolate_frame( frame, N__, n__ ); } if( motion ) { motionmap_store_frame(frame); } }
inline float fast_cos(float x) { return fast_sin(x + 0.5f); }
float fast_cos(float x, bool lessPrecision) { return fast_sin(x + 3.1415f / 2, lessPrecision); }