OLD | NEW |
1 /******************************************************************************* | 1 /******************************************************************************* |
2 * Copyright (c) 2015, Daniel Murphy, Google | 2 * Copyright (c) 2015, Daniel Murphy, Google |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without modificati
on, | 5 * Redistribution and use in source and binary forms, with or without modificati
on, |
6 * are permitted provided that the following conditions are met: | 6 * are permitted provided that the following conditions are met: |
7 * * Redistributions of source code must retain the above copyright notice, | 7 * * Redistributions of source code must retain the above copyright notice, |
8 * this list of conditions and the following disclaimer. | 8 * this list of conditions and the following disclaimer. |
9 * * Redistributions in binary form must reproduce the above copyright notice, | 9 * * Redistributions in binary form must reproduce the above copyright notice, |
10 * this list of conditions and the following disclaimer in the documentation | 10 * this list of conditions and the following disclaimer in the documentation |
(...skipping 23 matching lines...) Expand all Loading... |
34 | 34 |
35 /** | 35 /** |
36 * Local position of the shape centroid in parent body frame. | 36 * Local position of the shape centroid in parent body frame. |
37 */ | 37 */ |
38 final Vector2 centroid = new Vector2.zero(); | 38 final Vector2 centroid = new Vector2.zero(); |
39 | 39 |
40 /** | 40 /** |
41 * The vertices of the shape. Note: use getVertexCount(), not _vertices.length
, to get number of | 41 * The vertices of the shape. Note: use getVertexCount(), not _vertices.length
, to get number of |
42 * active vertices. | 42 * active vertices. |
43 */ | 43 */ |
44 final List<Vector2> vertices = | 44 final List<Vector2> vertices = new List<Vector2>(Settings.maxPolygonVertices); |
45 new List<Vector2>(Settings.maxPolygonVertices); | |
46 | 45 |
47 /** | 46 /** |
48 * The normals of the shape. Note: use getVertexCount(), not _normals.length,
to get number of | 47 * The normals of the shape. Note: use getVertexCount(), not _normals.length,
to get number of |
49 * active normals. | 48 * active normals. |
50 */ | 49 */ |
51 final List<Vector2> normals = | 50 final List<Vector2> normals = new List<Vector2>(Settings.maxPolygonVertices); |
52 new List<Vector2>(Settings.maxPolygonVertices); | |
53 | 51 |
54 /** | 52 /** |
55 * Number of active vertices in the shape. | 53 * Number of active vertices in the shape. |
56 */ | 54 */ |
57 int count = 0; | 55 int count = 0; |
58 | 56 |
59 // pooling | 57 // pooling |
60 final Vector2 _pool1 = new Vector2.zero(); | 58 final Vector2 _pool1 = new Vector2.zero(); |
61 final Vector2 _pool2 = new Vector2.zero(); | 59 final Vector2 _pool2 = new Vector2.zero(); |
62 final Vector2 _pool3 = new Vector2.zero(); | 60 final Vector2 _pool3 = new Vector2.zero(); |
(...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 s.scale(1.0 / count.toDouble()); | 587 s.scale(1.0 / count.toDouble()); |
590 | 588 |
591 final double k_inv3 = 1.0 / 3.0; | 589 final double k_inv3 = 1.0 / 3.0; |
592 | 590 |
593 final Vector2 e1 = _pool3; | 591 final Vector2 e1 = _pool3; |
594 final Vector2 e2 = _pool4; | 592 final Vector2 e2 = _pool4; |
595 | 593 |
596 for (int i = 0; i < count; ++i) { | 594 for (int i = 0; i < count; ++i) { |
597 // Triangle vertices. | 595 // Triangle vertices. |
598 e1.setFrom(vertices[i]).sub(s); | 596 e1.setFrom(vertices[i]).sub(s); |
599 e2 | 597 e2.setFrom(s).negate().add(i + 1 < count ? vertices[i + 1] : vertices[0]); |
600 .setFrom(s) | |
601 .negate() | |
602 .add(i + 1 < count ? vertices[i + 1] : vertices[0]); | |
603 | 598 |
604 final double D = e1.cross(e2); | 599 final double D = e1.cross(e2); |
605 | 600 |
606 final double triangleArea = 0.5 * D; | 601 final double triangleArea = 0.5 * D; |
607 area += triangleArea; | 602 area += triangleArea; |
608 | 603 |
609 // Area weighted centroid | 604 // Area weighted centroid |
610 center.x += triangleArea * k_inv3 * (e1.x + e2.x); | 605 center.x += triangleArea * k_inv3 * (e1.x + e2.x); |
611 center.y += triangleArea * k_inv3 * (e1.y + e2.y); | 606 center.y += triangleArea * k_inv3 * (e1.y + e2.y); |
612 | 607 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
668 Vector2 applyToCentroid(final Transform xf) { | 663 Vector2 applyToCentroid(final Transform xf) { |
669 return Transform.mulVec2(xf, centroid); | 664 return Transform.mulVec2(xf, centroid); |
670 } | 665 } |
671 | 666 |
672 /** Get the centroid and apply the supplied transform. */ | 667 /** Get the centroid and apply the supplied transform. */ |
673 Vector2 centroidToOut(final Transform xf, final Vector2 out) { | 668 Vector2 centroidToOut(final Transform xf, final Vector2 out) { |
674 Transform.mulToOutUnsafeVec2(xf, centroid, out); | 669 Transform.mulToOutUnsafeVec2(xf, centroid, out); |
675 return out; | 670 return out; |
676 } | 671 } |
677 } | 672 } |
OLD | NEW |