예제 #1
0
void physics_system::destruct(const const_entity_handle handle) {
	if (is_constructed_rigid_body(handle)) {
		auto& cache = get_rigid_body_cache(handle);
		
		for (const auto& colliders_cache_id : cache.correspondent_colliders_caches)
			colliders_caches[colliders_cache_id] = colliders_cache();

		b2world->DestroyBody(cache.body);

		cache = rigid_body_cache();
	}
	
	if (is_constructed_colliders(handle)) {
		const auto this_cache_id = handle.get_id().pool.indirection_index;
		auto& cache = colliders_caches[this_cache_id];

		ensure(cache.correspondent_rigid_body_cache != -1);

		auto& owner_body_cache = rigid_body_caches[cache.correspondent_rigid_body_cache];

		for (const auto& f_per_c : cache.fixtures_per_collider)
			for (const auto f : f_per_c)
				owner_body_cache.body->DestroyFixture(f);

		remove_element(owner_body_cache.correspondent_colliders_caches, this_cache_id);
		cache = colliders_cache();
	}
}
std::optional<transformr> interpolation_system::find_interpolated(const const_entity_handle handle) const {
	const auto id = handle.get_id();

	auto result = [&]() -> std::optional<transformr> {
		if (enabled) {
			if (const auto cache = mapped_or_nullptr(per_entity_cache, id)) {
				return cache->interpolated_transform;
			}
		}

		return handle.find_logic_transform();
	}();

	/*
		Here, we integerize the transform of the viewed entity, (and later possibly of the vehicle that it drives)
		so that the rendered player does not shake when exactly followed by the camera,
		which is also integerized for pixel-perfect rendering.

		Ideally we shouldn't do it here because it introduces more state, but that's about the simplest solution
		that doesn't make a mess out of rendering scripts for this special case.	

		Additionally, if someone does not use interpolation (there should be no need to disable it, really)
		they will still suffer from the problem of shaky controlled player. We don't have time for now to handle it better.
	*/

	if (id == id_to_integerize) {
		if (result) {
			result->pos.discard_fract();
		}
	}

	return result;
}
예제 #3
0
simulation_receiver::misprediction_candidate_entry simulation_receiver::acquire_potential_misprediction(const const_entity_handle& e) const {
	misprediction_candidate_entry candidate;
	
	if(e.has_logic_transform())
		candidate.transform = e.logic_transform();

	candidate.id = e.get_id();

	return candidate;
}
void interpolation_system::set_updated_interpolated_transform(
	const const_entity_handle subject,
	const transformr updated_value
) {
	auto& cache = per_entity_cache[subject];
	const auto& info = subject.get<components::interpolation>();
	
	cache.recorded_place_of_birth = info.place_of_birth;
	cache.interpolated_transform = updated_value;
	cache.recorded_version = subject.get_id().raw.version;
}
예제 #5
0
std::pair<size_t, size_t> physics_system::map_fixture_pointer_to_indices(const b2Fixture* const f, const const_entity_handle& handle) {
	const auto this_cache_id = handle.get_id().pool.indirection_index;
	const auto& cache = colliders_caches[this_cache_id];

	for (size_t collider_index = 0; collider_index < cache.fixtures_per_collider.size(); ++collider_index) {
		for (size_t convex_index = 0; convex_index < cache.fixtures_per_collider[collider_index].size(); ++convex_index) {
			if (cache.fixtures_per_collider[collider_index][convex_index] == f) {
				return std::make_pair(collider_index, convex_index);
			}
		}
	}

	ensure(false);
	return{};
}
예제 #6
0
void physics_system::construct(const const_entity_handle handle) {
	//ensure(!is_constructed_rigid_body(handle));
	if (is_constructed_rigid_body(handle))
		return;

	fixtures_construct(handle);

	if (handle.has<components::physics>()) {
		const auto& physics = handle.get<components::physics>();
		const auto& fixture_entities = physics.get_fixture_entities();

		if (physics.is_activated() && fixture_entities.size() > 0) {
			const auto& physics_data = physics.get_data();
			auto& cache = get_rigid_body_cache(handle);

			b2BodyDef def;

			switch (physics_data.body_type) {
			case components::physics::type::DYNAMIC: def.type = b2BodyType::b2_dynamicBody; break;
			case components::physics::type::STATIC: def.type = b2BodyType::b2_staticBody; break;
			case components::physics::type::KINEMATIC: def.type = b2BodyType::b2_kinematicBody; break;
			default:ensure(false) break;
			}

			def.userData = handle.get_id();
			def.bullet = physics_data.bullet;
			def.transform = physics_data.transform;
			def.sweep = physics_data.sweep;
			def.angularDamping = physics_data.angular_damping;
			def.linearDamping = physics_data.linear_damping;
			def.fixedRotation = physics_data.fixed_rotation;
			def.gravityScale = physics_data.gravity_scale;
			def.active = true;
			def.linearVelocity = physics_data.velocity;
			def.angularVelocity = physics_data.angular_velocity;

			cache.body = b2world->CreateBody(&def);
			cache.body->SetAngledDampingEnabled(physics_data.angled_damping);
			
			/* notice that all fixtures must be unconstructed at this moment since we assert that the rigid body itself is not */
			for (const auto& f : fixture_entities)
				fixtures_construct(f);
		}
	}
}
예제 #7
0
void physics_system::fixtures_construct(const const_entity_handle handle) {
	//ensure(!is_constructed_colliders(handle));
	if (is_constructed_colliders(handle))
		return;

	if (handle.has<components::fixtures>()) {
		const auto colliders = handle.get<components::fixtures>();

		if (colliders.is_activated() && is_constructed_rigid_body(colliders.get_owner_body())) {
			const auto& colliders_data = colliders.get_data();
			auto& cache = get_colliders_cache(handle);

			const auto owner_body_entity = colliders.get_owner_body();
			ensure(owner_body_entity.alive());
			auto& owner_cache = get_rigid_body_cache(owner_body_entity);

			const auto this_cache_id = handle.get_id().pool.indirection_index;
			const auto owner_cache_id = owner_body_entity.get_id().pool.indirection_index;

			owner_cache.correspondent_colliders_caches.push_back(this_cache_id);
			cache.correspondent_rigid_body_cache = owner_cache_id;

			for (size_t ci = 0; ci < colliders_data.colliders.size(); ++ci) {
				const auto& c = colliders_data.colliders[ci];

				b2PolygonShape shape;

				b2FixtureDef fixdef;
				fixdef.density = c.density;
				fixdef.friction = c.friction;
				fixdef.isSensor = c.sensor;
				fixdef.filter = c.filter;
				fixdef.restitution = c.restitution;
				fixdef.shape = &shape;
				fixdef.userData = handle.get_id();

				auto transformed_shape = c.shape;
				transformed_shape.offset_vertices(colliders.get_total_offset());

				std::vector<b2Fixture*> partitioned_collider;

				for (const auto convex : transformed_shape.convex_polys) {
					std::vector<b2Vec2> b2verts(convex.vertices.begin(), convex.vertices.end());

					for (auto& v : b2verts)
						v *= PIXELS_TO_METERSf;

					shape.Set(b2verts.data(), b2verts.size());

					b2Fixture* const new_fix = owner_cache.body->CreateFixture(&fixdef);
					
					ensure(static_cast<short>(ci) < std::numeric_limits<short>::max());
					new_fix->collider_index = static_cast<short>(ci);

					partitioned_collider.push_back(new_fix);
				}

				cache.fixtures_per_collider.push_back(partitioned_collider);
			}
		}
	}
}