Skip to content

Commit 0655489

Browse files
committed
Started adding a super simple physics engine.
1 parent 484e911 commit 0655489

File tree

7 files changed

+268
-2
lines changed

7 files changed

+268
-2
lines changed

sim-eth-basic/src/main/java/example/Main.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ public void simpleInitApp() {
112112
if( inputManager.hasMapping(INPUT_MAPPING_EXIT) ) {
113113
inputManager.deleteMapping(INPUT_MAPPING_EXIT);
114114
}
115+
116+
/*
117+
Spatial test = assetManager.loadModel("Models/fighter.j3o");
118+
Texture texture = assetManager.loadTexture("Textures/ship1.png");
119+
Material mat = globals.createMaterial(texture, false).getMaterial();
120+
test.setMaterial(mat);
121+
rootNode.attachChild(test);*/
115122

116123
}
117124

sim-eth-basic/src/main/java/example/net/client/GameSessionClientService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ public GameSessionClientService() {
7272

7373
@Override
7474
public void move( Quaternion dir, Vector3f thrust ) {
75-
log.info("move(" + dir + ", " + thrust + ")");
75+
if( log.isTraceEnabled() ) {
76+
log.trace("move(" + dir + ", " + thrust + ")");
77+
}
7678
getDelegate().move(dir, thrust);
7779
}
7880

sim-eth-basic/src/main/java/example/net/server/GameServer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.simsilica.sim.GameSystemManager;
5151

5252
import example.GameConstants;
53+
import example.sim.*;
5354

5455
/**
5556
* The main GameServer that manages the back end game services, hosts
@@ -86,6 +87,7 @@ public GameServer( int port, String description ) throws IOException {
8687
this.loop = new GameLoop(systems);
8788

8889
// Add the various game services to the GameSystemManager
90+
systems.addSystem(new SimplePhysics());
8991

9092
// Add any hosted services that require those systems to already
9193
// exist

sim-eth-basic/src/main/java/example/net/server/GameSessionHostedService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ protected GameSessionListener getCallback() {
199199

200200
@Override
201201
public void move( Quaternion rotation, Vector3f thrust ) {
202-
log.info("move(" + rotation + ", " + thrust + ")");
202+
if( log.isTraceEnabled() ) {
203+
log.trace("move(" + rotation + ", " + thrust + ")");
204+
}
203205

204206
// Need to forward this to the game world
205207
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* $Id$
3+
*
4+
* Copyright (c) 2016, Simsilica, LLC
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
*
14+
* 2. Redistributions in binary form must reproduce the above copyright
15+
* notice, this list of conditions and the following disclaimer in
16+
* the documentation and/or other materials provided with the
17+
* distribution.
18+
*
19+
* 3. Neither the name of the copyright holder nor the names of its
20+
* contributors may be used to endorse or promote products derived
21+
* from this software without specific prior written permission.
22+
*
23+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34+
* OF THE POSSIBILITY OF SUCH DAMAGE.
35+
*/
36+
37+
package example.sim;
38+
39+
import java.util.concurrent.atomic.AtomicInteger;
40+
41+
import com.simsilica.mathd.*;
42+
43+
/**
44+
* A physical body in space. These are modeled as a "point mass"
45+
* in the sense that their orientation does not integrate, nor does
46+
* it affect integration. ie: there is no rotational acceleration
47+
* or velocity. Rotation is kept because it's convenient.
48+
*
49+
* @author Paul Speed
50+
*/
51+
public class Body {
52+
53+
private static AtomicInteger nextBodyId = new AtomicInteger(42);
54+
55+
public int bodyId = nextBodyId.getAndIncrement();
56+
57+
public Quatd rotation = new Quatd();
58+
public Vec3d pos = new Vec3d();
59+
public Vec3d velocity = new Vec3d();
60+
public Vec3d acceleration = new Vec3d();
61+
public double radius = 1;
62+
63+
public volatile ControlDriver driver;
64+
65+
public Body() {
66+
}
67+
68+
public Body( double x, double y, double z ) {
69+
pos.set(x, y, z);
70+
}
71+
72+
public void integrate( double stepTime ) {
73+
// Integrate velocity
74+
velocity.addScaledVectorLocal(acceleration, stepTime);
75+
76+
// Integrate position
77+
pos.addScaledVectorLocal(velocity, stepTime);
78+
79+
// That's it. That's a physics engine.
80+
}
81+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* $Id$
3+
*
4+
* Copyright (c) 2016, Simsilica, LLC
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
*
14+
* 2. Redistributions in binary form must reproduce the above copyright
15+
* notice, this list of conditions and the following disclaimer in
16+
* the documentation and/or other materials provided with the
17+
* distribution.
18+
*
19+
* 3. Neither the name of the copyright holder nor the names of its
20+
* contributors may be used to endorse or promote products derived
21+
* from this software without specific prior written permission.
22+
*
23+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34+
* OF THE POSSIBILITY OF SUCH DAMAGE.
35+
*/
36+
37+
package example.sim;
38+
39+
40+
/**
41+
*
42+
*
43+
* @author Paul Speed
44+
*/
45+
public interface ControlDriver {
46+
47+
public void update( double stepTime, Body body );
48+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* $Id$
3+
*
4+
* Copyright (c) 2016, Simsilica, LLC
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
*
14+
* 2. Redistributions in binary form must reproduce the above copyright
15+
* notice, this list of conditions and the following disclaimer in
16+
* the documentation and/or other materials provided with the
17+
* distribution.
18+
*
19+
* 3. Neither the name of the copyright holder nor the names of its
20+
* contributors may be used to endorse or promote products derived
21+
* from this software without specific prior written permission.
22+
*
23+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34+
* OF THE POSSIBILITY OF SUCH DAMAGE.
35+
*/
36+
37+
package example.sim;
38+
39+
import java.util.*;
40+
import java.util.concurrent.*;
41+
42+
import com.jme3.util.SafeArrayList;
43+
44+
import com.simsilica.sim.*;
45+
46+
/**
47+
* Just a basic physics simulation that integrates acceleration,
48+
* velocity, and position on "point masses".
49+
*
50+
* @author Paul Speed
51+
*/
52+
public class SimplePhysics extends AbstractGameSystem {
53+
54+
// Single threaded.... we'll have to take care when adding/removing
55+
// items.
56+
private SafeArrayList<Body> bodies = new SafeArrayList<>(Body.class);
57+
private Map<Integer, Body> index = new ConcurrentHashMap<>();
58+
59+
private ConcurrentLinkedQueue<Body> toAdd = new ConcurrentLinkedQueue<>();
60+
private ConcurrentLinkedQueue<Body> toRemove = new ConcurrentLinkedQueue<>();
61+
62+
public SimplePhysics() {
63+
}
64+
65+
public int createBody() {
66+
Body result = new Body();
67+
toAdd.add(result);
68+
index.put(result.bodyId, result);
69+
return result.bodyId;
70+
}
71+
72+
public boolean removeBody( int id ) {
73+
Body body = index.remove(id);
74+
if( body == null ) {
75+
return false;
76+
}
77+
toRemove.add(body);
78+
return true;
79+
}
80+
81+
protected void initialize() {
82+
}
83+
84+
protected void terminate() {
85+
}
86+
87+
private void updateBodyList() {
88+
if( !toAdd.isEmpty() ) {
89+
Body body = null;
90+
while( (body = toAdd.poll()) != null ) {
91+
bodies.add(body);
92+
}
93+
}
94+
if( !toRemove.isEmpty() ) {
95+
Body body = null;
96+
while( (body = toRemove.poll()) != null ) {
97+
bodies.remove(body);
98+
}
99+
}
100+
}
101+
102+
@Override
103+
public void update( SimTime time ) {
104+
105+
updateBodyList();
106+
107+
double tpf = time.getTpf();
108+
109+
// Apply control driver changes
110+
for( Body b : bodies.getArray() ) {
111+
if( b.driver != null ) {
112+
b.driver.update(tpf, b);
113+
}
114+
}
115+
116+
// Integrate
117+
for( Body b : bodies.getArray() ) {
118+
b.integrate(tpf);
119+
}
120+
121+
// Publish the results
122+
}
123+
124+
}

0 commit comments

Comments
 (0)