Skip to content

Commit

Permalink
added physics settings panel
Browse files Browse the repository at this point in the history
  • Loading branch information
i-make-robots committed May 14, 2024
1 parent 6745447 commit febc60a
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/marginallyclever/ro3/AllPanels.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.formdev.flatlaf.FlatLightLaf;
import com.marginallyclever.ro3.node.nodes.pose.poses.*;
import com.marginallyclever.ro3.physics.ODEPhysicsPanel;
import com.marginallyclever.ro3.apps.ode4j.ODEPhysicsSettingsPanel;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -107,7 +107,7 @@ private Collection<Class<? extends JPanel>> getHandmadeList() {
com.marginallyclever.ro3.node.nodes.limbplanner.LimbPlannerPanel.class,
com.marginallyclever.ro3.node.nodes.limbsolver.LimbSolverPanel.class,
com.marginallyclever.ro3.node.nodes.marlinrobotarm.MarlinRobotArmPanel.class,
ODEPhysicsPanel.class,
ODEPhysicsSettingsPanel.class,
com.marginallyclever.ro3.node.nodes.pose.PosePanel.class,
AttachmentPointPanel.class,
CameraPanel.class,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/marginallyclever/ro3/apps/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.awt.*;

/**
* All apps extend from App for Reflection.
* All applications in the environment extend from {@link App} for Reflection.
*/
public abstract class App extends JPanel {
public App() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public AppSettingsDialog(List<App> apps) {
ViewProvider<App> viewProvider = (ViewProvider<App>) clazz.getConstructor().newInstance();
viewProvider.setViewSubject(appInstance);
JPanel viewPanel = (JPanel)viewProvider;
tabbedPane.addTab(appClass.getSimpleName(), viewPanel);
JPanel container = new JPanel(new BorderLayout());
container.add(viewPanel, BorderLayout.NORTH);
tabbedPane.addTab(((JPanel) viewProvider).getName(), container);
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
logger.error("Error creating view for app: " + appInstance.getClass().getSimpleName(), e);
}
Expand All @@ -49,6 +51,7 @@ public AppSettingsDialog(List<App> apps) {
}

public void run(Component parent) {
// show message dialog is the reason the dialog cannot be resized.
JOptionPane.showMessageDialog(parent, this, "Settings", JOptionPane.PLAIN_MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.marginallyclever.ro3.apps.ode4j;

import com.marginallyclever.convenience.swing.NumberFormatHelper;
import com.marginallyclever.ro3.PanelHelper;
import com.marginallyclever.ro3.Registry;
import com.marginallyclever.ro3.physics.ODEPhysics;
import com.marginallyclever.ro3.view.View;
import com.marginallyclever.ro3.view.ViewProvider;

import javax.swing.*;
import javax.swing.text.NumberFormatter;
import java.awt.*;

/**
* {@link ODEPhysicsSettingsPanel} is a {@link View} for {@link ODEPhysics}.
*/
@View(of= ODE4JPanel.class)
public class ODEPhysicsSettingsPanel extends JPanel implements ViewProvider<ODE4JPanel> {
private ODE4JPanel subject;
private final NumberFormatter formatter = NumberFormatHelper.getNumberFormatter();
private final JFormattedTextField cfm = new JFormattedTextField(formatter);
private final JFormattedTextField erp = new JFormattedTextField(formatter);
private final JFormattedTextField gravity = new JFormattedTextField(formatter);

public ODEPhysicsSettingsPanel() {
super(new GridBagLayout());
setName("Physics");

GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridx=0;
gbc.gridy=0;
gbc.fill = GridBagConstraints.BOTH;

var physics = Registry.getPhysics();
// cfm
cfm.setValue(physics.getCFM());
PanelHelper.addLabelAndComponent(this, "CFM",cfm,gbc);
cfm.addPropertyChangeListener("value", evt -> setCFM((Double) evt.getNewValue()));

// erp
gbc.gridy++;
erp.setValue(physics.getERP());
PanelHelper.addLabelAndComponent(this, "ERP",erp,gbc);
erp.addPropertyChangeListener("value", evt -> setERP((Double) evt.getNewValue()));

// gravity
gbc.gridy++;
gravity.setValue(physics.getGravity());
PanelHelper.addLabelAndComponent(this, "Gravity",gravity,gbc);
gravity.addPropertyChangeListener("value", evt ->setGravity((Double) evt.getNewValue()));
}

public void setCFM(double cfm) {
var physics = Registry.getPhysics();
physics.setCFM(cfm);
}

public void setERP(double erp) {
var physics = Registry.getPhysics();
physics.setERP(erp);
}

public void setGravity(double gravity) {
var physics = Registry.getPhysics();
physics.setGravity(gravity);
}

@Override
public void setViewSubject(ODE4JPanel subject) {
this.subject = subject;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ViewportSettingsPanel extends JPanel implements ViewProvider<Viewpo

public ViewportSettingsPanel() {
super(new GridBagLayout());
setName("Viewport Settings");
setName("Viewport");

GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
Expand Down Expand Up @@ -188,6 +188,8 @@ public void setViewSubject(Viewport subject) {
setVerticalSync(subject.isVerticalSync());
setFSAASamples(subject.getFsaaSamples());

// this only allows parameters from one render pass.
// TODO: add other passes?
DrawMeshes meshes = getDrawMeshes();
if(meshes!=null) {
setSunColor(meshes.getSunlightColor());
Expand Down
48 changes: 42 additions & 6 deletions src/main/java/com/marginallyclever/ro3/physics/ODEPhysics.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.marginallyclever.ro3.physics;

import org.ode4j.math.DVector3;
import org.ode4j.ode.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.event.EventListenerList;

import static org.ode4j.ode.OdeConstants.*;
import static org.ode4j.ode.OdeHelper.createSapSpace;
import static org.ode4j.ode.OdeHelper.createWorld;

/**
Expand All @@ -16,12 +18,14 @@
public class ODEPhysics {
private static final Logger logger = LoggerFactory.getLogger(ODEPhysics.class);

public double WORLD_CFM = 1e-5;
public double WORLD_ERP = 0.8;
public double WORLD_GRAVITY = -9.81;
private double WORLD_CFM = 1e-5;
private double WORLD_ERP = 0.8;
private double WORLD_GRAVITY = -9.81;
private final int ITERS = 20;
private final int CONTACT_BUFFER_SIZE = 4;

private int spaceType = 0;

private DWorld world;
private DSpace space;
private DContactBuffer contacts;
Expand Down Expand Up @@ -54,10 +58,14 @@ private void startPhysics() {
world.setQuickStepNumIterations(ITERS);
}

// setup a space in the world
// create a space in the world
if(space == null) {
space = OdeHelper.createSapSpace(null, DSapSpace.AXES.XYZ);
//space = OdeHelper.createSimpleSpace();
space = switch(spaceType) {
case 1 -> OdeHelper.createHashSpace(null);
case 2 -> OdeHelper.createSapSpace(null, DSapSpace.AXES.XYZ);
//case 3 -> OdeHelper.createQuadTreeSpace(null, new DVector3(0, 0, 0), new DVector3(100, 100, 100), 0);
default -> OdeHelper.createSimpleSpace();
};
}

if(contacts == null) {
Expand Down Expand Up @@ -174,4 +182,32 @@ public boolean isPaused() {
public void setPaused(boolean state) {
isPaused = state;
}

public double getCFM() {
return WORLD_CFM;
}

public void setCFM(double WORLD_CFM) {
this.WORLD_CFM = WORLD_CFM;
if(world!=null) world.setCFM(WORLD_CFM);
}

public double getERP() {
return WORLD_ERP;
}

public void setERP(double WORLD_ERP) {
this.WORLD_ERP = WORLD_ERP;
if(world!=null) world.setERP(WORLD_ERP);
}

public double getGravity() {
return WORLD_GRAVITY;
}

public void setGravity(double WORLD_GRAVITY) {
this.WORLD_GRAVITY = WORLD_GRAVITY;
if(world!=null) world.setGravity(0, 0, WORLD_GRAVITY);
}

}

This file was deleted.

0 comments on commit febc60a

Please sign in to comment.