mirror of
https://github.com/godotengine/godot.git
synced 2024-11-23 12:43:43 +00:00
Fix polygon inertia calculation
This commit is contained in:
parent
5efd124ca1
commit
46dec43257
@ -560,13 +560,21 @@ bool GodotConvexPolygonShape2D::intersect_segment(const Vector2 &p_begin, const
|
||||
|
||||
real_t GodotConvexPolygonShape2D::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
|
||||
ERR_FAIL_COND_V_MSG(point_count == 0, 0, "Convex polygon shape has no points.");
|
||||
Rect2 aabb_new;
|
||||
aabb_new.position = points[0].pos * p_scale;
|
||||
Vector2 pos[point_count+1];
|
||||
for (int i = 0; i < point_count; i++) {
|
||||
aabb_new.expand_to(points[i].pos * p_scale);
|
||||
pos[i] = points[i].pos * p_scale;
|
||||
}
|
||||
pos[point_count] = points[0].pos * p_scale;
|
||||
|
||||
real_t inertia = 0.;
|
||||
real_t area = 0.;
|
||||
for (int i = 0; i < point_count; i++) {
|
||||
real_t a = pos[i].x * pos[i+1].y - pos[i+1].x * pos[i].y;
|
||||
inertia += a * (pos[i].dot(pos[i]) + pos[i].dot(pos[i+1]) + pos[i+1].dot(pos[i+1]));
|
||||
area += a / 2.;
|
||||
}
|
||||
|
||||
return p_mass * aabb_new.size.dot(aabb_new.size) / 12.0;
|
||||
return p_mass * inertia / (12. * area);
|
||||
}
|
||||
|
||||
void GodotConvexPolygonShape2D::set_data(const Variant &p_data) {
|
||||
|
Loading…
Reference in New Issue
Block a user