Switching physics for traffic
This commit is contained in:
@@ -10,7 +10,9 @@
|
||||
#include "traffic.h"
|
||||
|
||||
Traffic::Traffic(): max_spawn_distance(120.0f),
|
||||
min_spawn_distance(80), delete_distance(Vector3(100.0f, -60.0f, 180.0f)),
|
||||
min_spawn_distance(80),
|
||||
delete_distance(Vector3(100.0f, -60.0f, 180.0f)),
|
||||
physics_distance(Vector3(30.0f, -20.0f, 60.0f)),
|
||||
list_cooldown(0.0), debug(false)
|
||||
{
|
||||
state = 0;
|
||||
@@ -259,13 +261,52 @@ void Traffic::control_traffic()
|
||||
Spatial *sp = Object::cast_to<Spatial>(node);
|
||||
Transform xform = sp->get_global_transform();
|
||||
Vector3 check = cam_xform.xform_inv(xform.origin);
|
||||
/* TODO: use AABB instead */
|
||||
if (fabsf(check.x) > delete_distance.x ||
|
||||
check.z > delete_distance.z ||
|
||||
check.z < delete_distance.y)
|
||||
sp->queue_delete();
|
||||
else if (fabsf(check.x) > physics_distance.x + 1.0f ||
|
||||
check.z > physics_distance.z + 1.0f ||
|
||||
check.z < physics_distance.y - 1) {
|
||||
if (!sp->has_meta("space"))
|
||||
vehicle_disable_physics(sp);
|
||||
} else if (fabsf(check.x) < physics_distance.x - 1.0f ||
|
||||
check.z < physics_distance.z - 1.0f ||
|
||||
check.z > physics_distance.y + 1) {
|
||||
if (sp->has_meta("space"))
|
||||
vehicle_enable_physics(sp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Traffic::vehicle_enable_physics(Spatial *obj)
|
||||
{
|
||||
RigidBody *body = Object::cast_to<RigidBody>(obj);
|
||||
if (!body)
|
||||
return;
|
||||
|
||||
RID space = obj->get_meta("space");
|
||||
PhysicsServer::get_singleton()->body_set_space(body->get_rid(), space);
|
||||
obj->remove_meta("space");
|
||||
if (obj->has_meta("velocity")) {
|
||||
Vector3 velocity = obj->get_meta("velocity");
|
||||
body->set_linear_velocity(velocity);
|
||||
}
|
||||
}
|
||||
|
||||
void Traffic::vehicle_disable_physics(Spatial *obj)
|
||||
{
|
||||
RigidBody *body = Object::cast_to<RigidBody>(obj);
|
||||
if (!body)
|
||||
return;
|
||||
RID space = PhysicsServer::get_singleton()->body_get_space(body->get_rid());
|
||||
Vector3 velocity = body->get_linear_velocity();
|
||||
obj->set_meta("space", space);
|
||||
obj->set_meta("velocity", velocity);
|
||||
PhysicsServer::get_singleton()->body_set_space(body->get_rid(), RID());
|
||||
}
|
||||
|
||||
static Traffic *g_traffic_data = NULL;
|
||||
Traffic *Traffic::get_singleton()
|
||||
{
|
||||
|
||||
@@ -9,7 +9,8 @@ protected:
|
||||
List<Node *>::Element *spawner_e;
|
||||
float max_spawn_distance;
|
||||
float min_spawn_distance;
|
||||
Vector3 delete_distance;
|
||||
Vector3 delete_distance,
|
||||
physics_distance;
|
||||
Ref<RandomNumberGenerator> rnd;
|
||||
float list_cooldown, spawn_cooldown;
|
||||
RID immediate, instance;
|
||||
@@ -18,6 +19,8 @@ protected:
|
||||
|
||||
void debug_traffic();
|
||||
void control_traffic();
|
||||
void vehicle_enable_physics(Spatial *obj);
|
||||
void vehicle_disable_physics(Spatial *obj);
|
||||
public:
|
||||
Traffic();
|
||||
~Traffic();
|
||||
|
||||
Reference in New Issue
Block a user