X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=examples%2Fbassteroids%2Fsource%2Fphysics.cpp;h=67cdf6fe596bbf29199b7af96174c92eecaefc92;hb=e7223a520b12663127b21afe8a68965898f34ded;hp=a8d49ec4d0331cb98437c07a24264a6c724b88a8;hpb=1819b186d60376a546722d99edd686e876b81d9f;p=libs%2Fgame.git diff --git a/examples/bassteroids/source/physics.cpp b/examples/bassteroids/source/physics.cpp index a8d49ec..67cdf6f 100644 --- a/examples/bassteroids/source/physics.cpp +++ b/examples/bassteroids/source/physics.cpp @@ -70,12 +70,17 @@ void Physics::copy_in(SimulatedEntity &entity) entity.rotation = Geometry::atan2(2*(r.a*r.d+r.b*r.c), 1-2*(r.c*r.c+r.d*r.d)); if constexpr(is_fixture) + { entity.inverse_mass = 0.0f; + entity.inverse_momi = 0.0f; + } else { Game::Handle body = entity.entity->get_body(); entity.inverse_mass = 1.0f/body->get_mass(); - entity.moment_of_inertia = body->get_moment_of_inertia(); + entity.inverse_momi = 1.0f/body->get_moment_of_inertia(); + entity.external_force = body->get_force(); + entity.external_torque = body->get_torque(); entity.velocity = body->get_velocity(); entity.angular_velocity = body->get_angular_velocity(); } @@ -93,6 +98,7 @@ void Physics::copy_out(SimulatedEntity &entity) Game::Handle body = entity.entity->get_body(); body->set_velocity(entity.velocity); body->set_angular_velocity(entity.angular_velocity); + body->clear_forces(); } } @@ -102,11 +108,11 @@ void Physics::step(float dt_secs) { SimulatedEntity &entity = entities[i]; - LinAl::Vector new_velocity = entity.velocity+entity.external_force*dt_secs*entity.inverse_mass; + LinAl::Vector new_velocity = entity.velocity+entity.external_force*(dt_secs*entity.inverse_mass); entity.position += (entity.velocity+new_velocity)*(dt_secs/2); entity.velocity = new_velocity; - Geometry::Angle new_angular_velocity = entity.angular_velocity+Geometry::Angle::from_radians(entity.external_torque*(dt_secs/entity.moment_of_inertia)); + Geometry::Angle new_angular_velocity = entity.angular_velocity+Geometry::Angle::from_radians(entity.external_torque*dt_secs*entity.inverse_momi); entity.rotation = wrap_positive(entity.rotation+(entity.angular_velocity+new_angular_velocity)*(dt_secs/2)); entity.angular_velocity = new_angular_velocity; } @@ -175,12 +181,24 @@ void Physics::apply_impulses() { SimulatedEntity &entity1 = entities[c.body1]; SimulatedEntity &entity2 = entities[c.body2]; - LinAl::Vector v_rel = entity2.velocity-entity1.velocity; + LinAl::Vector r1 = c.point-entity1.position; + LinAl::Vector r2 = c.point-entity2.position; + LinAl::Vector v_p1 = entity1.velocity+LinAl::Vector(-r1.y, r1.x)*entity1.angular_velocity.radians(); + LinAl::Vector v_p2 = entity2.velocity+LinAl::Vector(-r2.y, r2.x)*entity2.angular_velocity.radians(); + LinAl::Vector v_rel = v_p2-v_p1; + LinAl::Vector tangent = v_rel-c.normal*inner_product(v_rel, c.normal); + float v_tan = tangent.norm(); + tangent = (v_tan>1e-5 ? normalize(tangent) : LinAl::Vector(-c.normal.y, c.normal.x)); float restitution = 1.0f; + float friction_coeff = 0.1f; float inv_mass_sum = entity1.inverse_mass+entity2.inverse_mass; - float impulse = (1+restitution)*inner_product(v_rel, c.normal)/inv_mass_sum; - entity1.velocity += c.normal*(impulse*entity1.inverse_mass); - entity2.velocity -= c.normal*(impulse*entity2.inverse_mass); + float reaction = (1+restitution)*inner_product(v_rel, c.normal)/inv_mass_sum; + float friction = min(reaction*friction_coeff, v_tan/inv_mass_sum); + LinAl::Vector impulse = c.normal*reaction+tangent*friction; + entity1.velocity += impulse*entity1.inverse_mass; + entity2.velocity -= impulse*entity2.inverse_mass; + entity1.angular_velocity += Geometry::Angle::from_radians(entity1.inverse_momi*(r1.x*impulse.y-r1.y*impulse.x)); + entity2.angular_velocity -= Geometry::Angle::from_radians(entity2.inverse_momi*(r2.x*impulse.y-r2.y*impulse.x)); } } @@ -233,11 +251,29 @@ void Physics::collide_circle_box(unsigned i, unsigned j) LinAl::Vector local_closest(clamp(local_delta.x, -half_size.x, half_size.x), clamp(local_delta.y, -half_size.y, half_size.y)); LinAl::Vector local_cdelta = local_delta-local_closest; float d_sq = inner_product(local_cdelta, local_cdelta); + if(d_sq(c*local_cdelta.x-s*local_cdelta.y, c*local_cdelta.y+s*local_cdelta.x)); - collision.depth = radius-sqrt(d_sq); + if(d_sq>1e-10) + { + collision.normal = normalize(LinAl::Vector(c*local_cdelta.x-s*local_cdelta.y, c*local_cdelta.y+s*local_cdelta.x)); + collision.depth = radius-sqrt(d_sq); + } + else + { + LinAl::Vector inside_dist(half_size.x-abs(local_delta.x), half_size.y-abs(local_delta.y)); + if(inside_dist.x(c, s) * (local_delta.x<0 ? -1.0f : 1.0f); + collision.depth = radius+inside_dist.x; + } + else + { + collision.normal = LinAl::Vector(-s, c) * (local_delta.y<0 ? -1.0f : 1.0f); + collision.depth = radius+inside_dist.y; + } + } collision.point = pos1-collision.normal*(radius-collision.depth/2); if(collision.body1!=i) collision.normal = -collision.normal;