forked from g3n/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distance.go
49 lines (35 loc) · 1.28 KB
/
distance.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
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package constraint
import (
"github.com/adamlenda/engine/experimental/physics/equation"
)
// Distance is a distance constraint.
// Constrains two bodies to be at a constant distance from each others center of mass.
type Distance struct {
Constraint
distance float32 // Distance
equation *equation.Contact
}
// NewDistance creates and returns a pointer to a new Distance constraint object.
func NewDistance(bodyA, bodyB IBody, distance, maxForce float32) *Distance {
dc := new(Distance)
dc.initialize(bodyA, bodyB, true, true)
// Default distance should be: bodyA.position.distanceTo(bodyB.position)
// Default maxForce should be: 1e6
dc.distance = distance
dc.equation = equation.NewContact(bodyA, bodyB, -maxForce, maxForce) // Make it bidirectional
dc.AddEquation(dc.equation)
return dc
}
// Update updates the equation with data.
func (dc *Distance) Update() {
halfDist := dc.distance * 0.5
posA := dc.bodyA.Position()
posB := dc.bodyB.Position()
normal := posB.Sub(&posA)
normal.Normalize()
dc.equation.SetRA(normal.Clone().MultiplyScalar(halfDist))
dc.equation.SetRB(normal.Clone().MultiplyScalar(-halfDist))
}