This repository has been archived by the owner on Oct 29, 2018. It is now read-only.
forked from vova616/chipmunk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segmentShape.go
83 lines (67 loc) · 1.81 KB
/
segmentShape.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package chipmunk
import (
. "github.com/oniproject/chipmunk/algebra"
)
//If Settings.AutoUpdateShapes is not set, call Update on the parent shape for changes to the A, B and Radius to take effect.
type SegmentShape struct {
Shape *Shape
//start/end points of the segment.
A, B Vect
//radius of the segment.
Radius Float
//local normal. Do not touch!
N Vect
//transformed normal. Do not touch!
Tn Vect
//transformed start/end points. Do not touch!
Ta, Tb Vect
//tangents at the start/end when chained with other segments. Do not touch!
A_tangent, B_tangent Vect
}
// Creates a new SegmentShape with the given points and radius.
func NewSegment(a, b Vect, r Float) *Shape {
shape := newShape()
seg := &SegmentShape{
A: a,
B: b,
Radius: r,
Shape: shape,
}
shape.ShapeClass = seg
return shape
}
// Returns ShapeType_Segment. Needed to implemet the ShapeClass interface.
func (segment *SegmentShape) ShapeType() ShapeType {
return ShapeType_Segment
}
func (segment *SegmentShape) Moment(mass float32) Float {
offset := Mult(Add(segment.A, segment.B), 0.5)
return Float(mass) * (DistSqr(segment.B, segment.A)/12.0 + LengthSqr(offset))
}
//Called to update N, Tn, Ta, Tb and the the bounding box.
func (segment *SegmentShape) update(xf Transform) AABB {
a := xf.TransformVect(segment.A)
b := xf.TransformVect(segment.B)
segment.Ta = a
segment.Tb = b
segment.N = Perp(Normalize(Sub(segment.B, segment.A)))
segment.Tn = xf.RotateVect(segment.N)
rv := Vect{segment.Radius, segment.Radius}
min := Min(a, b)
min.Sub(rv)
max := Max(a, b)
max.Add(rv)
return AABB{
min,
max,
}
}
func (segment *SegmentShape) Clone(s *Shape) ShapeClass {
clone := *segment
clone.Shape = s
return &clone
}
// Only returns false for now.
func (segment *SegmentShape) TestPoint(point Vect) bool {
return false
}