-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.go
89 lines (71 loc) · 1.92 KB
/
player.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
package objects
import (
"github.com/dayaftereh/discover/server/game/engine/physics"
"github.com/dayaftereh/discover/server/mathf"
)
var GameObjectPlayer GameObjectType = "planet"
type Player struct {
id int64
radius float64
rigidbody *physics.RigidBody
// movement
move *mathf.Vec3
rotation *mathf.Vec3
// force
linearForce *mathf.Vec3
angularForce *mathf.Vec3
}
func NewPlayer(id int64, position *mathf.Vec3) *Player {
rigidbody := physics.NewRigidBody()
rigidbody.Position = position
rigidbody.LinearDamping = 0.5
rigidbody.AngularDamping = 0.5
radius := 1.0
rigidbody.Inertia = physics.CalculateSphereInertia(radius, rigidbody.Mass)
rigidbody.UpdateInertiaWorld(true)
return &Player{
id: id,
radius: radius,
rigidbody: rigidbody,
move: nil,
rotation: nil,
linearForce: mathf.NewVec3(100.0, 100.0, 100.0),
angularForce: mathf.NewVec3(1.0, 1.0, 1.0),
}
}
func (player *Player) ID() int64 {
return player.id
}
func (player *Player) RigidBody() *physics.RigidBody {
return player.rigidbody
}
func (player *Player) Update(delta float64) {
if player.move != nil {
move := mathf.ClampVec3(player.move, -1.0, 1.0)
// calculate the linear force
force := player.linearForce.MultiplyVec(move)
// apply move force
player.rigidbody.ApplyLocalForce(force, mathf.NewZeroVec3())
}
if player.rotation != nil {
torque := mathf.ClampVec3(player.rotation, -1.0, 1.0)
// calculate the angular force
force := player.angularForce.MultiplyVec(torque)
// apply the rotaion
player.rigidbody.AddLocalTorque(force)
}
// update the rigidbody
player.rigidbody.Update(delta)
}
func (player *Player) UpdateMovement(move *mathf.Vec3, rotation *mathf.Vec3) {
player.move = move
player.rotation = rotation
}
func (player *Player) Type() GameObjectType {
return GameObjectPlayer
}
func (player *Player) Radius() float64 {
return 1.0
}
func (player *Player) Destroy() {
}