void Body2DSW::update_inertias() { //update shapes and motions switch(mode) { case Physics2DServer::BODY_MODE_RIGID: { //update tensor for allshapes, not the best way but should be somehow OK. (inspired from bullet) float total_area=0; for (int i=0;i<get_shape_count();i++) { total_area+=get_shape_aabb(i).get_area(); } real_t _inertia=0; for (int i=0;i<get_shape_count();i++) { const Shape2DSW* shape=get_shape(i); float area=get_shape_aabb(i).get_area(); float mass = area * this->mass / total_area; Matrix32 mtx = get_shape_transform(i); 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; } if (_inertia!=0) _inv_inertia=1.0/_inertia; else _inv_inertia=0.0; //wathever if (mass) _inv_mass=1.0/mass; else _inv_mass=0; } break; case Physics2DServer::BODY_MODE_KINEMATIC: case Physics2DServer::BODY_MODE_STATIC: { _inv_inertia=0; _inv_mass=0; } break; case Physics2DServer::BODY_MODE_CHARACTER: { _inv_inertia=0; _inv_mass=1.0/mass; } break; } //_update_inertia_tensor(); //_update_shapes(); }
void BodySW::update_inertias() { //update shapes and motions switch(mode) { case PhysicsServer::BODY_MODE_RIGID: { //update tensor for allshapes, not the best way but should be somehow OK. (inspired from bullet) float total_area=0; for (int i=0;i<get_shape_count();i++) { total_area+=get_shape_aabb(i).get_area(); } Vector3 _inertia; for (int i=0;i<get_shape_count();i++) { const ShapeSW* shape=get_shape(i); float area=get_shape_aabb(i).get_area(); float mass = area * this->mass / total_area; _inertia += shape->get_moment_of_inertia(mass) + mass * get_shape_transform(i).get_origin(); } if (_inertia!=Vector3()) _inv_inertia=_inertia.inverse(); else _inv_inertia=Vector3(); if (mass) _inv_mass=1.0/mass; else _inv_mass=0; } break; case PhysicsServer::BODY_MODE_KINEMATIC: case PhysicsServer::BODY_MODE_STATIC: { _inv_inertia=Vector3(); _inv_mass=0; } break; case PhysicsServer::BODY_MODE_CHARACTER: { _inv_inertia=Vector3(); _inv_mass=1.0/mass; } break; } _update_inertia_tensor(); //_update_shapes(); }