diff options
Diffstat (limited to 'servers/physics_2d/shape_2d_sw.cpp')
-rw-r--r-- | servers/physics_2d/shape_2d_sw.cpp | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/servers/physics_2d/shape_2d_sw.cpp b/servers/physics_2d/shape_2d_sw.cpp index 012c4ed404..336eec73b5 100644 --- a/servers/physics_2d/shape_2d_sw.cpp +++ b/servers/physics_2d/shape_2d_sw.cpp @@ -131,7 +131,7 @@ bool LineShape2DSW::intersect_segment(const Vector2& p_begin,const Vector2& p_en return true; } -real_t LineShape2DSW::get_moment_of_inertia(float p_mass) const { +real_t LineShape2DSW::get_moment_of_inertia(float p_mass, const Vector2 &p_scale) const { return 0; } @@ -180,7 +180,7 @@ bool RayShape2DSW::intersect_segment(const Vector2& p_begin,const Vector2& p_end } -real_t RayShape2DSW::get_moment_of_inertia(float p_mass) const { +real_t RayShape2DSW::get_moment_of_inertia(float p_mass, const Vector2 &p_scale) const { return 0; //rays are mass-less } @@ -237,10 +237,12 @@ bool SegmentShape2DSW::intersect_segment(const Vector2& p_begin,const Vector2& p return true; } -real_t SegmentShape2DSW::get_moment_of_inertia(float p_mass) const { +real_t SegmentShape2DSW::get_moment_of_inertia(float p_mass, const Vector2 &p_scale) const { - real_t l = b.distance_to(a); - Vector2 ofs = (a+b)*0.5; + Vector2 s[2]={a*p_scale,b*p_scale}; + + real_t l = s[1].distance_to(s[0]); + Vector2 ofs = (s[0]+s[1])*0.5; return p_mass*(l*l/12.0f + ofs.length_squared()); } @@ -312,9 +314,10 @@ bool CircleShape2DSW::intersect_segment(const Vector2& p_begin,const Vector2& p_ return true; } -real_t CircleShape2DSW::get_moment_of_inertia(float p_mass) const { +real_t CircleShape2DSW::get_moment_of_inertia(float p_mass, const Vector2 &p_scale) const { + + return (radius*radius)*(p_scale.x*0.5+p_scale.y*0.5); - return radius*radius; } void CircleShape2DSW::set_data(const Variant& p_data) { @@ -377,9 +380,9 @@ bool RectangleShape2DSW::intersect_segment(const Vector2& p_begin,const Vector2& return get_aabb().intersects_segment(p_begin,p_end,&r_point,&r_normal); } -real_t RectangleShape2DSW::get_moment_of_inertia(float p_mass) const { +real_t RectangleShape2DSW::get_moment_of_inertia(float p_mass,const Vector2& p_scale) const { - Vector2 he2=half_extents*2; + Vector2 he2=half_extents*2*p_scale; return p_mass*he2.dot(he2)/12.0f; } @@ -499,9 +502,9 @@ bool CapsuleShape2DSW::intersect_segment(const Vector2& p_begin,const Vector2& p return collided; //todo } -real_t CapsuleShape2DSW::get_moment_of_inertia(float p_mass) const { +real_t CapsuleShape2DSW::get_moment_of_inertia(float p_mass, const Vector2 &p_scale) const { - Vector2 he2(radius*2,height+radius*2); + Vector2 he2=Vector2(radius*2,height+radius*2)*p_scale; return p_mass*he2.dot(he2)/12.0f; } @@ -610,16 +613,16 @@ bool ConvexPolygonShape2DSW::intersect_segment(const Vector2& p_begin,const Vect return inters; //todo } -real_t ConvexPolygonShape2DSW::get_moment_of_inertia(float p_mass) const { +real_t ConvexPolygonShape2DSW::get_moment_of_inertia(float p_mass,const Vector2& p_scale) const { Rect2 aabb; - aabb.pos=points[0].pos; + aabb.pos=points[0].pos*p_scale; for(int i=0;i<point_count;i++) { - aabb.expand_to(points[i].pos); + aabb.expand_to(points[i].pos*p_scale); } - return p_mass*aabb.size.dot(aabb.size)/12.0f; + return p_mass*aabb.size.dot(aabb.size)/12.0f + p_mass * (aabb.pos+aabb.size*0.5).length_squared(); } void ConvexPolygonShape2DSW::set_data(const Variant& p_data) { |