forked from g3n/engine
/
constraint.go
95 lines (74 loc) · 2.1 KB
/
constraint.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
84
85
86
87
88
89
90
91
92
93
94
95
// 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 implements physics constraints.
package constraint
import (
"github.com/g3n/engine/experimental/physics/equation"
"github.com/g3n/engine/math32"
)
type IBody interface {
equation.IBody
WakeUp()
VectorToWorld(*math32.Vector3) math32.Vector3
PointToLocal(*math32.Vector3) math32.Vector3
VectorToLocal(*math32.Vector3) math32.Vector3
Quaternion() *math32.Quaternion
}
type IConstraint interface {
Update() // Update all the equations with data.
Equations() []equation.IEquation
CollideConnected() bool
BodyA() IBody
BodyB() IBody
}
// Constraint base struct.
type Constraint struct {
equations []equation.IEquation // Equations to be solved in this constraint
bodyA IBody
bodyB IBody
colConn bool // Set to true if you want the bodies to collide when they are connected.
}
// NewConstraint creates and returns a pointer to a new Constraint object.
//func NewConstraint(bodyA, bodyB IBody, colConn, wakeUpBodies bool) *Constraint {
//
// c := new(Constraint)
// c.initialize(bodyA, bodyB, colConn, wakeUpBodies)
// return c
//}
func (c *Constraint) initialize(bodyA, bodyB IBody, colConn, wakeUpBodies bool) {
c.bodyA = bodyA
c.bodyB = bodyB
c.colConn = colConn // true
if wakeUpBodies { // true
if bodyA != nil {
bodyA.WakeUp()
}
if bodyB != nil {
bodyB.WakeUp()
}
}
}
// AddEquation adds an equation to the constraint.
func (c *Constraint) AddEquation(eq equation.IEquation) {
c.equations = append(c.equations, eq)
}
// Equations returns the constraint's equations.
func (c *Constraint) Equations() []equation.IEquation {
return c.equations
}
func (c *Constraint) CollideConnected() bool {
return c.colConn
}
func (c *Constraint) BodyA() IBody {
return c.bodyA
}
func (c *Constraint) BodyB() IBody {
return c.bodyB
}
// SetEnable sets the enabled flag of the constraint equations.
func (c *Constraint) SetEnabled(state bool) {
for i := range c.equations {
c.equations[i].SetEnabled(state)
}
}