-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
1,770 additions
and
1,028 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package acceleration | ||
|
||
import ( | ||
"github.com/mokiat/gomath/dprec" | ||
"github.com/mokiat/lacking/game/physics/solver" | ||
) | ||
|
||
// NewGravityDirection creates a new GravityDirection acceleration | ||
// that applies a constant force in a specific direction. | ||
func NewGravityDirection() *GravityDirection { | ||
return &GravityDirection{ | ||
direction: dprec.NewVec3(0.0, -1.0, 0.0), | ||
acceleration: 9.8, | ||
} | ||
} | ||
|
||
var _ solver.Acceleration = (*GravityDirection)(nil) | ||
|
||
// GravityDirection represents the solution for an acceleration that | ||
// applies a constant force in a specific direction. | ||
type GravityDirection struct { | ||
direction dprec.Vec3 | ||
acceleration float64 | ||
} | ||
|
||
// Direction returns the direction of the gravity force. | ||
func (d *GravityDirection) Direction() dprec.Vec3 { | ||
return d.direction | ||
} | ||
|
||
// SetDirection changes the direction of the gravity force. | ||
func (d *GravityDirection) SetDirection(direction dprec.Vec3) *GravityDirection { | ||
d.direction = direction | ||
return d | ||
} | ||
|
||
// Acceleration returns the acceleration of the gravity force. | ||
func (d *GravityDirection) Acceleration() float64 { | ||
return d.acceleration | ||
} | ||
|
||
// SetAcceleration changes the acceleration of the gravity force. | ||
func (d *GravityDirection) SetAcceleration(acceleration float64) *GravityDirection { | ||
d.acceleration = acceleration | ||
return d | ||
} | ||
|
||
func (d *GravityDirection) ApplyAcceleration(ctx solver.AccelerationContext) { | ||
ctx.Target.AddLinearAcceleration( | ||
dprec.Vec3Prod(d.direction, d.acceleration), | ||
) | ||
} | ||
|
||
// NewGravityPosition creates a new GravityPosition acceleration | ||
// that applies a constant force towards a specific position. | ||
func NewGravityPosition() *GravityPosition { | ||
return &GravityPosition{ | ||
position: dprec.ZeroVec3(), | ||
} | ||
} | ||
|
||
var _ solver.Acceleration = (*GravityPosition)(nil) | ||
|
||
// GravityPosition represents the solution for an acceleration that | ||
// applies a constant force towards a specific position. | ||
type GravityPosition struct { | ||
position dprec.Vec3 | ||
acceleration float64 | ||
|
||
// TODO: Add ability to control distance falloff. | ||
} | ||
|
||
// Position returns the position of the gravity force. | ||
func (d *GravityPosition) Position() dprec.Vec3 { | ||
return d.position | ||
} | ||
|
||
// SetPosition changes the position of the gravity force. | ||
func (d *GravityPosition) SetPosition(position dprec.Vec3) *GravityPosition { | ||
d.position = position | ||
return d | ||
} | ||
|
||
// Acceleration returns the acceleration of the gravity force. | ||
func (d *GravityPosition) Acceleration() float64 { | ||
return d.acceleration | ||
} | ||
|
||
// SetAcceleration changes the acceleration of the gravity force. | ||
func (d *GravityPosition) SetAcceleration(acceleration float64) *GravityPosition { | ||
d.acceleration = acceleration | ||
return d | ||
} | ||
|
||
func (d *GravityPosition) ApplyAcceleration(ctx solver.AccelerationContext) { | ||
delta := dprec.Vec3Diff(d.position, ctx.Target.Position()) | ||
if distance := delta.Length(); distance > solver.Epsilon { | ||
ctx.Target.AddLinearAcceleration( | ||
dprec.ResizedVec3(delta, d.acceleration), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,82 @@ | ||
package physics | ||
|
||
type Accelerator struct { | ||
import "github.com/mokiat/lacking/game/physics/solver" | ||
|
||
var invalidGlobalAcceleratorState = &globalAcceleratorState{} | ||
|
||
// GlobalAccelerator represents a force that is applied to all | ||
// bodies in the scene. | ||
type GlobalAccelerator struct { | ||
scene *Scene | ||
reference indexReference | ||
} | ||
|
||
// Logic returns the acceleration solver that will be used to | ||
// apply this global accelerator. | ||
func (a GlobalAccelerator) Logic() solver.Acceleration { | ||
state := a.state() | ||
return state.logic | ||
} | ||
|
||
// Enabled returns whether this global accelerator will be applied. | ||
func (a GlobalAccelerator) Enabled() bool { | ||
state := a.state() | ||
return state.enabled | ||
} | ||
|
||
// SetEnabled changes whether this global accelerator will be applied. | ||
func (a GlobalAccelerator) SetEnabled(enabled bool) { | ||
state := a.state() | ||
state.enabled = enabled | ||
} | ||
|
||
// Delete removes this global accelerator. | ||
func (a GlobalAccelerator) Delete() { | ||
deleteGlobalAccelerator(a.scene, a.reference) | ||
} | ||
|
||
func (a GlobalAccelerator) state() *globalAcceleratorState { | ||
index := a.reference.Index | ||
state := &a.scene.globalAccelerators[index] | ||
if state.reference != a.reference { | ||
return invalidGlobalAcceleratorState | ||
} | ||
return state | ||
} | ||
|
||
type globalAcceleratorState struct { | ||
reference indexReference | ||
logic solver.Acceleration | ||
enabled bool | ||
} | ||
|
||
func createGlobalAccelerator(scene *Scene, logic solver.Acceleration) GlobalAccelerator { | ||
var freeIndex uint32 | ||
if scene.freeGlobalAcceleratorIndices.IsEmpty() { | ||
freeIndex = uint32(len(scene.globalAccelerators)) | ||
scene.globalAccelerators = append(scene.globalAccelerators, globalAcceleratorState{}) | ||
} else { | ||
freeIndex = scene.freeGlobalAcceleratorIndices.Pop() | ||
} | ||
|
||
reference := newIndexReference(freeIndex, scene.nextRevision()) | ||
scene.globalAccelerators[freeIndex] = globalAcceleratorState{ | ||
reference: reference, | ||
logic: logic, | ||
enabled: true, | ||
} | ||
return GlobalAccelerator{ | ||
scene: scene, | ||
reference: reference, | ||
} | ||
} | ||
|
||
type AccelerationContext struct { | ||
func deleteGlobalAccelerator(scene *Scene, reference indexReference) { | ||
index := reference.Index | ||
state := &scene.globalAccelerators[index] | ||
if state.reference == reference { | ||
state.reference = newIndexReference(index, 0) | ||
state.logic = nil | ||
scene.freeGlobalAcceleratorIndices.Push(index) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.