void collideBallWithPaddle(Gem& ball, const Paddle& paddle) { SpriteMatrix matrix = paddle.getSpriteMatrix(); // Left sphere vec2 left = {-24, 0}; // Right sphere vec2 right = {24, 0}; matrix.transform(&left.x, &left.y); matrix.transform(&right.x, &right.y); static const int PADDLE_RADIUS = 8; fixed24_8 rel_ball_x = ball.pos_x - paddle.pos_x; fixed24_8 rel_ball_y = ball.pos_y - paddle.pos_y; vec2 rel_ball = {rel_ball_x.toFloat(), rel_ball_y.toFloat()}; vec2 nearest_point = pointLineSegmentNearestPoint(rel_ball, left, right); vec2 penetration = rel_ball - nearest_point; float d_sqr = length_sqr(penetration); float r = PADDLE_RADIUS + Gem::RADIUS; if (d_sqr < r*r) { vec2 vel = {ball.vel_x.toFloat(), ball.vel_y.toFloat()}; int score_addition = static_cast<int>(ball.score_value * (ball.vel_y.toFloat() / 128.f)); ball.score_value = std::min(ball.score_value + std::max(score_addition, 0), Gem::MAX_VALUE); float d = std::sqrt(d_sqr); float sz = r - d; vec2 normal = penetration / d; fixed24_8 push_back_x(sz * normal.x); fixed24_8 push_back_y(sz * normal.y); ball.pos_x += push_back_x; ball.pos_y += push_back_y; vec2 par, perp; splitVector(vel, normal, &par, &perp); vel = perp - par; ball.vel_x = fixed16_16(vel.x); ball.vel_y = fixed16_16(vel.y); } }
void Drone::draw(SpriteBuffer& sprite_buffer, const Camera& camera) const { Sprite drone_spr; drone_spr.setImg(34, 1, 24, 24); drone_spr.setPos(camera.transform(rb.pos)); SpriteMatrix matrix; matrix.loadIdentity().rotate(angle); sprite_buffer.append(drone_spr, matrix); if (anim_flags.test(AnimationFlags::YELLOW_STROBE)) { drone_spr.img_y = 1+1*25; sprite_buffer.append(drone_spr, matrix); } if (anim_flags.test(AnimationFlags::RED_STROBE)) { drone_spr.img_y = 1+2*25; sprite_buffer.append(drone_spr, matrix); } }
void Ship::draw(SpriteBuffer& sprite_buffer, const Camera& camera) const { Sprite ship_spr; ship_spr.img = img_ship_body; ship_spr.setPos(camera.transform(rb.pos)); SpriteMatrix matrix; matrix.loadIdentity().rotate(rb.orientation); sprite_buffer.append(ship_spr, matrix); if (anim_flags.test(AnimationFlags::THRUST_FORWARD)) { ship_spr.img = img_ship_thrust; sprite_buffer.append(ship_spr, matrix); } if (anim_flags.test(AnimationFlags::INERTIAL_BRAKE)) { ship_spr.img = img_ship_brake; sprite_buffer.append(ship_spr, matrix); } shield.draw(sprite_buffer, ship_spr.x, ship_spr.y); }