|
@@ -47,8 +47,10 @@ void Body2DSW::update_inertias() {
|
|
|
|
|
|
case Physics2DServer::BODY_MODE_RIGID: {
|
|
case Physics2DServer::BODY_MODE_RIGID: {
|
|
|
|
|
|
- if (user_inertia) break;
|
|
|
|
-
|
|
|
|
|
|
+ if (user_inertia) {
|
|
|
|
+ _inv_inertia = inertia > 0 ? (1.0 / inertia) : 0;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
//update tensor for allshapes, not the best way but should be somehow OK. (inspired from bullet)
|
|
//update tensor for allshapes, not the best way but should be somehow OK. (inspired from bullet)
|
|
real_t total_area = 0;
|
|
real_t total_area = 0;
|
|
|
|
|
|
@@ -57,7 +59,7 @@ void Body2DSW::update_inertias() {
|
|
total_area += get_shape_aabb(i).get_area();
|
|
total_area += get_shape_aabb(i).get_area();
|
|
}
|
|
}
|
|
|
|
|
|
- real_t _inertia = 0;
|
|
|
|
|
|
+ inertia = 0;
|
|
|
|
|
|
for (int i = 0; i < get_shape_count(); i++) {
|
|
for (int i = 0; i < get_shape_count(); i++) {
|
|
|
|
|
|
@@ -73,15 +75,10 @@ void Body2DSW::update_inertias() {
|
|
|
|
|
|
Transform2D mtx = get_shape_transform(i);
|
|
Transform2D mtx = get_shape_transform(i);
|
|
Vector2 scale = mtx.get_scale();
|
|
Vector2 scale = mtx.get_scale();
|
|
- _inertia += shape->get_moment_of_inertia(mass, scale) + mass * mtx.get_origin().length_squared();
|
|
|
|
- //Rect2 ab = get_shape_aabb(i);
|
|
|
|
- //_inertia+=mass*ab.size.dot(ab.size)/12.0f;
|
|
|
|
|
|
+ inertia += shape->get_moment_of_inertia(mass, scale) + mass * mtx.get_origin().length_squared();
|
|
}
|
|
}
|
|
|
|
|
|
- if (_inertia != 0)
|
|
|
|
- _inv_inertia = 1.0 / _inertia;
|
|
|
|
- else
|
|
|
|
- _inv_inertia = 0.0; //wathever
|
|
|
|
|
|
+ _inv_inertia = inertia > 0 ? (1.0 / inertia) : 0;
|
|
|
|
|
|
if (mass)
|
|
if (mass)
|
|
_inv_mass = 1.0 / mass;
|
|
_inv_mass = 1.0 / mass;
|
|
@@ -160,6 +157,7 @@ void Body2DSW::set_param(Physics2DServer::BodyParameter p_param, real_t p_value)
|
|
_update_inertia();
|
|
_update_inertia();
|
|
} else {
|
|
} else {
|
|
user_inertia = true;
|
|
user_inertia = true;
|
|
|
|
+ inertia = p_value;
|
|
_inv_inertia = 1.0 / p_value;
|
|
_inv_inertia = 1.0 / p_value;
|
|
}
|
|
}
|
|
} break;
|
|
} break;
|
|
@@ -194,7 +192,7 @@ real_t Body2DSW::get_param(Physics2DServer::BodyParameter p_param) const {
|
|
return mass;
|
|
return mass;
|
|
}
|
|
}
|
|
case Physics2DServer::BODY_PARAM_INERTIA: {
|
|
case Physics2DServer::BODY_PARAM_INERTIA: {
|
|
- return _inv_inertia == 0 ? 0 : 1.0 / _inv_inertia;
|
|
|
|
|
|
+ return inertia;
|
|
}
|
|
}
|
|
case Physics2DServer::BODY_PARAM_GRAVITY_SCALE: {
|
|
case Physics2DServer::BODY_PARAM_GRAVITY_SCALE: {
|
|
return gravity_scale;
|
|
return gravity_scale;
|
|
@@ -226,6 +224,7 @@ void Body2DSW::set_mode(Physics2DServer::BodyMode p_mode) {
|
|
|
|
|
|
_set_inv_transform(get_transform().affine_inverse());
|
|
_set_inv_transform(get_transform().affine_inverse());
|
|
_inv_mass = 0;
|
|
_inv_mass = 0;
|
|
|
|
+ _inv_inertia = 0;
|
|
_set_static(p_mode == Physics2DServer::BODY_MODE_STATIC);
|
|
_set_static(p_mode == Physics2DServer::BODY_MODE_STATIC);
|
|
set_active(p_mode == Physics2DServer::BODY_MODE_KINEMATIC && contacts.size());
|
|
set_active(p_mode == Physics2DServer::BODY_MODE_KINEMATIC && contacts.size());
|
|
linear_velocity = Vector2();
|
|
linear_velocity = Vector2();
|
|
@@ -237,17 +236,21 @@ void Body2DSW::set_mode(Physics2DServer::BodyMode p_mode) {
|
|
case Physics2DServer::BODY_MODE_RIGID: {
|
|
case Physics2DServer::BODY_MODE_RIGID: {
|
|
|
|
|
|
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
|
|
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
|
|
|
|
+ _inv_inertia = inertia > 0 ? (1.0 / inertia) : 0;
|
|
_set_static(false);
|
|
_set_static(false);
|
|
|
|
|
|
} break;
|
|
} break;
|
|
case Physics2DServer::BODY_MODE_CHARACTER: {
|
|
case Physics2DServer::BODY_MODE_CHARACTER: {
|
|
|
|
|
|
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
|
|
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
|
|
|
|
+ _inv_inertia = 0;
|
|
_set_static(false);
|
|
_set_static(false);
|
|
|
|
+ angular_velocity = 0;
|
|
} break;
|
|
} break;
|
|
}
|
|
}
|
|
-
|
|
|
|
- _update_inertia();
|
|
|
|
|
|
+ if (p_mode == Physics2DServer::BODY_MODE_RIGID && _inv_inertia == 0) {
|
|
|
|
+ _update_inertia();
|
|
|
|
+ }
|
|
/*
|
|
/*
|
|
if (get_space())
|
|
if (get_space())
|
|
_update_queries();
|
|
_update_queries();
|