-
Notifications
You must be signed in to change notification settings - Fork 34
/
DemoMultithreading.java
206 lines (180 loc) · 6.98 KB
/
DemoMultithreading.java
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*************************************************************************
* *
* Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
* All rights reserved. Email: russ@q12.org Web: www.q12.org *
* Open Dynamics Engine 4J, Copyright (C) 2009-2014 Tilmann Zaeschke *
* All rights reserved. Email: ode4j@gmx.de Web: www.ode4j.org *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of EITHER: *
* (1) The GNU Lesser General Public License as published by the Free *
* Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. The text of the GNU Lesser *
* General Public License is included with this library in the *
* file LICENSE.TXT. *
* (2) The BSD-style license that is included with this library in *
* the file ODE-LICENSE-BSD.TXT and ODE4J-LICENSE-BSD.TXT. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
* LICENSE.TXT, ODE-LICENSE-BSD.TXT and ODE4J-LICENSE-BSD.TXT for more *
* details. *
* *
*************************************************************************/
package org.ode4j.demo.ragdoll;
import static org.ode4j.drawstuff.DrawStuff.dsDrawBox;
import static org.ode4j.drawstuff.DrawStuff.dsDrawCapsule;
import static org.ode4j.drawstuff.DrawStuff.dsSetColor;
import static org.ode4j.drawstuff.DrawStuff.dsSetViewpoint;
import static org.ode4j.drawstuff.DrawStuff.dsSimulationLoop;
import static org.ode4j.ode.OdeConstants.dContactBounce;
import static org.ode4j.ode.OdeConstants.dContactSoftCFM;
import static org.ode4j.ode.OdeHelper.areConnectedExcluding;
import org.ode4j.drawstuff.DrawStuff.dsFunctions;
import org.ode4j.math.DMatrix3;
import org.ode4j.math.DMatrix3C;
import org.ode4j.math.DQuaternion;
import org.ode4j.math.DVector3;
import org.ode4j.math.DVector3C;
import org.ode4j.ode.*;
import org.ode4j.ode.internal.DxQuickStep;
import org.ode4j.ode.ragdoll.DRagdoll;
import org.ode4j.ode.threading.task.MultiThreadTaskExecutor;
public class DemoMultithreading extends dsFunctions {
private static final int MAX_CONTACTS = 64; // maximum number of contact points per body
private static final int RAGDOLLS = 8;
private DWorld world;
private DSpace space;
private final DRagdoll[] ragdolls = new DRagdoll[RAGDOLLS * RAGDOLLS];
private static final double[] xyz = {0, -12.0614, 6.4300};
private static final double[] hpr = {90, -34.5, 0};
private DJointGroup contactgroup;
private boolean show_contacts = false; // show contact points?
@Override
public void start()
{
//dAllocateODEDataForThread(dAllocateMaskAll);
dsSetViewpoint (xyz,hpr);
System.out.println ("Press space to apply some force to the ragdoll");
world = OdeHelper.createWorld();
world.setGravity(0,0,-9.8);
world.setDamping(1e-4, 1e-5);
world.setTaskExecutor(new MultiThreadTaskExecutor(4));
world.setQuickStepNumIterations(200);
// dWorldSetERP(world, 1);
space = OdeHelper.createSimpleSpace();
contactgroup = OdeHelper.createJointGroup ();
OdeHelper.createPlane( space, 0, 0, 1, 0 );
for (int y = 0; y < RAGDOLLS; y++) {
for (int x = 0; x < RAGDOLLS; x++) {
int n = x + y * RAGDOLLS;
ragdolls[n] = OdeHelper.createRagdoll(world, space, new DxDefaultHumanRagdollConfig());
ragdolls[n].setAngularDamping(0.1);
DQuaternion q = new DQuaternion(1, 0, 0, 0);
DRotation.dQFromAxisAndAngle(q, new DVector3(1, 0, 0), -0.5 * Math.PI);
for (DRagdoll.DRagdollBody bone : ragdolls[n].getBoneIter()) {
DGeom g = OdeHelper.createCapsule(space, bone.getRadius(), bone.getLength());
DBody body = bone.getBody();
DQuaternion qq = new DQuaternion();
OdeMath.dQMultiply1(qq, q, body.getQuaternion());
body.setQuaternion(qq);
DMatrix3 R = new DMatrix3();
OdeMath.dRfromQ(R, q);
DVector3 v = new DVector3();
OdeMath.dMultiply0_133(v, body.getPosition(), R);
v.add((x - RAGDOLLS / 2) * 2, (y - RAGDOLLS / 2) * 2, 0);
body.setPosition(v.get0(), v.get1(), v.get2());
g.setBody(body);
}
}
}
// initial camera position
dsSetViewpoint (xyz,hpr);
}
@Override
public void stop()
{
for (int x = 0; x < RAGDOLLS * RAGDOLLS; x++) {
ragdolls[x].destroy();
}
contactgroup.destroy();
space.destroy();
world.destroy();
}
private void drawGeom(DGeom g)
{
//int gclass = dGeomGetClass(g);
if (g instanceof DCapsule) {
DVector3C pos = g.getPosition();
DMatrix3C rot = g.getRotation();
DCapsule cap = (DCapsule) g;
dsDrawCapsule (pos, rot, cap.getLength(), cap.getRadius());
}
}
private void nearCallback (Object data, DGeom o1, DGeom o2) {
int i;
// if (o1->body && o2->body) return;
// exit without doing anything if the two bodies are connected by a joint
DBody b1 = o1.getBody();
DBody b2 = o2.getBody();
if (b1!=null && b2!=null && areConnectedExcluding (b1,b2,DContactJoint.class)) {
return;
}
DContactBuffer contacts = new DContactBuffer(MAX_CONTACTS); // up to MAX_CONTACTS contacts per box-box
for (i=0; i<MAX_CONTACTS; i++) {
DContact contact = contacts.get(i);
contact.surface.mode = dContactBounce | dContactSoftCFM;
contact.surface.mu = 100;
contact.surface.mu2 = 0;
contact.surface.bounce = 0.01;
contact.surface.bounce_vel = 0.01;
contact.surface.soft_cfm = 0.0001;
}
int numc = OdeHelper.collide (o1,o2,MAX_CONTACTS,contacts.getGeomBuffer());
if (numc!=0) {
DMatrix3 RI = new DMatrix3();
RI.setIdentity();
final DVector3 ss = new DVector3(0.02,0.02,0.02);
for (i=0; i<numc; i++) {
DJoint c = OdeHelper.createContactJoint (world,contactgroup,contacts.get(i));
c.attach (b1,b2);
if (show_contacts) {
dsSetColor(0, 0, 1);
dsDrawBox (contacts.get(i).geom.pos,RI,ss);
}
}
}
}
@Override
public void step(boolean pause)
{
space.collide (null, this::nearCallback);
DxQuickStep.mtIterations.set(0);
if (!pause) {
final double step = 0.005;
final int nsteps = 4;
for (int i=0; i<nsteps; ++i) {
world.quickStep(step);
}
}
contactgroup.empty();
// now we draw everything
for (DGeom g : space.getGeoms()) {
drawGeom(g);
}
}
public static void main(String[] args) {
new DemoMultithreading().demo(args);
}
private void demo(String[] args) {
// create world
OdeHelper.initODE();
// run demo
dsSimulationLoop (args, 800, 600, this);
OdeHelper.closeODE();
}
@Override
public void command(char cmd) {
}
}