Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/physics.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ addSlider(&dampStr, &damp, 1000)

This simulation allows interactive control over the parameters of a `Prismatic` joint, which sets the linear position of a body along a given axis, in this case along the horizontal (`X`) axis.

Click the `Step 10000` button and then start moving the sliders to see the effects interactively. Here's what you should observe:
Click the `Run` button and then start moving the sliders to see the effects interactively (press `Stop` to stop when done). Here's what you should observe:

* `Stiff` (stiffness) determines how quickly the joint responds to the position changes. You can make this variable even stronger in practice (e.g., 10,000).

Expand Down
43 changes: 43 additions & 0 deletions physics/phyxyz/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,41 @@ func (pe *Editor) Step(n int) {
pe.AsyncUnlock()
}

// Run runs the physics until stopped
func (pe *Editor) Run() {
if pe.isRunning {
return
}
pe.isRunning = true
pe.Model.SetAsCurrent()
pe.toolbar.AsyncLock()
pe.toolbar.UpdateRender()
pe.toolbar.AsyncUnlock()
for {
if pe.ControlFunc != nil {
pe.ControlFunc(physics.StepsToMsec(pe.TimeStep))
}
pe.Model.Step()
pe.TimeStep++
pe.Scene.Update()
pe.editor.AsyncLock()
pe.editor.NeedsRender()
pe.editor.AsyncUnlock()
if !pe.Model.GPU {
time.Sleep(time.Nanosecond) // this is essential for web (wasm) running to actually update
// if running in GPU mode, it works, but otherwise the thread never yields and it never updates.
}
if pe.stop {
pe.stop = false
break
}
}
pe.isRunning = false
pe.AsyncLock()
pe.Update()
pe.AsyncUnlock()
}

func (pe *Editor) MakeToolbar(p *tree.Plan) {
stepNButton := func(n int) {
nm := fmt.Sprintf("Step %d", n)
Expand Down Expand Up @@ -253,6 +288,14 @@ func (pe *Editor) MakeToolbar(p *tree.Plan) {
})
tree.Add(p, func(w *core.Separator) {})

tree.Add(p, func(w *core.Button) {
w.SetText("Run").SetIcon(icons.PlayArrow).
SetTooltip("Run physics until Stop is pressed.").
OnClick(func(e events.Event) {
go pe.Run()
})
w.FirstStyler(func(s *styles.Style) { s.SetEnabled(!pe.isRunning) })
})
stepNButton(1)
stepNButton(10)
stepNButton(100)
Expand Down
8 changes: 6 additions & 2 deletions physics/step_joint.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion physics/step_joint.goal
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ func StepJointForces(i uint32) { //gosl:kernel
// child world transform
poseCR := DynamicPos(jCi, params.Cur)
poseCQ := DynamicQuat(jCi, params.Cur)
jCR := JointCPos(ji)
jCQ := JointCQuat(ji)
xwCR := jCR
xwCQ := jCQ
slmath.MulSpatialTransforms(poseCR, poseCQ, jCR, jCQ, &xwCR, &xwCQ)
// https://github.com/newton-physics/newton/issues/1261
comC := BodyCom(jCbi)
dC := poseCR.Sub(slmath.MulSpatialPoint(poseCR, poseCQ, comC)) // child moment arm
dC := xwCR.Sub(slmath.MulSpatialPoint(poseCR, poseCQ, comC)) // child moment arm

var f, t math32.Vector3
switch jt {
Expand Down