Exemplo n.º 1
0
void
Canvas::drawMesh(const Mesh &mesh) {
    static const auto cameraPosition = Vector(0, 0, -10);
    static const auto cameraTarget = Vector(0, 0, 0);
    static const auto cameraUp = Vector(0, 1, 0);

    static auto view = Matrix::lookAtLH(cameraPosition, cameraTarget, cameraUp);
    static auto projection = Matrix::perspectiveFovLH(radiansFromDegrees(45), _width / _height, 0.1, 1);
    auto rotation = Matrix::rotation(mesh.rotation);
    auto translation = Matrix::translation(mesh.position);
    auto scale = Matrix::scale(mesh.scale);

    auto world = scale * rotation * translation;
    auto transform = world * view * projection;
    
    for(unsigned int i = 0; i < mesh.indices.size(); i += 3) {
        const Vertex &a = mesh.vertices[mesh.indices[i]];
        const Vertex &b = mesh.vertices[mesh.indices[i+1]];
        const Vertex &c = mesh.vertices[mesh.indices[i+2]];
        
        auto v1 = project(a, transform);
        auto v2 = project(b, transform);
        auto v3 = project(c, transform);

        drawTriangle(v1, v2, v3, mesh.texture);
    }
}
Exemplo n.º 2
0
void Ship::update(InputButtons::Bitset& input, GameState& game_state) {
	static const float TURNING_SPEED = radiansFromDegrees(5.0f);
	static const vec2 turning_vel = complex_from_angle(TURNING_SPEED);

	anim_flags.reset();

	if (input.test(InputButtons::LEFT)) {
		rb.angular_vel = complex_conjugate(turning_vel);
		anim_flags.set(AnimationFlags::THRUST_CCW);
	}

	if (input.test(InputButtons::RIGHT)) {
		rb.angular_vel = turning_vel;
		anim_flags.set(AnimationFlags::THRUST_CW);
	}

	if (input.test(InputButtons::LEFT) == input.test(InputButtons::RIGHT)) {
		rb.angular_vel = complex_1;
		Position mouse_pos = game_state.camera.inverse_transform(
			mvec2(static_cast<float>(game_state.mouse_x), static_cast<float>(game_state.mouse_y)));
		vec2 mouse_orientation = normalized(mouse_pos - rb.pos);
		rb.orientation = rotateTowards(rb.orientation, mouse_orientation, TURNING_SPEED);
	}

	if (input.test(InputButtons::BRAKE)) {
		rb.vel = 0.96f * rb.vel;
		anim_flags.set(AnimationFlags::INERTIAL_BRAKE);
	} else if (input.test(InputButtons::THRUST)) {
		vec2 accel = 0.05f * rb.orientation;
		rb.vel = rb.vel + accel;
		anim_flags.set(AnimationFlags::THRUST_FORWARD);
	}

	if (input.test(InputButtons::SHOOT) && shoot_cooldown == 0) {
		Bullet bullet;
		bullet.physp.pos = rb.pos;
		bullet.orientation = rb.orientation;
		bullet.physp.vel = rb.vel + 4.0f * rb.orientation;
		bullet.img = img_bullet;

		game_state.bullets.push_back(bullet);
		shoot_cooldown = 5;
	}
	if (shoot_cooldown > 0) {
		--shoot_cooldown;
	}

	shield.update();
	rb.update();
}