/*
* File name: ClothAgent.java (package eas.users.fabian.cloth)
* Author(s): Fabian
* Java version: 6.0
* Generation date: 15.02.2012 (16:31:52)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.users.students.fabian.cloth;
import org.ode4j.math.DVector3;
import org.ode4j.ode.DBallJoint;
import org.ode4j.ode.DBody;
import org.ode4j.ode.DGeom;
import org.ode4j.ode.DJoint;
import org.ode4j.ode.DMass;
import org.ode4j.ode.OdeHelper;
import eas.plugins.standard.visualization.visualization3D.VideoPluginLWJGL;
import eas.plugins.standard.visualization.visualization3D.VideoPluginLWJGL.DS_TEXTURE_NUMBER;
import eas.simulation.spatial.sim3D.physicalSimulation.standardAgents.AbstractAgentODE3D;
import eas.startSetup.ParCollection;
/**
* @author Fabian
*
*/
public class ClothAgent extends AbstractAgentODE3D<ClothEnv> {
/**
*
*/
private static final long serialVersionUID = 4924268600074543405L;
private int width = 25;
private int height = 25;
private double radius = .02;
double distance = radius * 5;
private final DVector3 initialPos = new DVector3(0, 0, 6);
private final double cfmAng1 = .000001;
private final double erpAng1 = .000001;
private final double fmaxAng1 = 100000;
private DBody[][] bodyMatrix;
private DGeom[][] geomMatrix;
/**
* @param id
* @param env
* @param params
*/
public ClothAgent(int id, ClothEnv env, ParCollection params) {
super(id, env, params);
double offset = width / 2 * (distance + radius);
// bodies = new DBody[width * height];
// int bodycounter = 0;
// geoms = new DGeom[width * height];
// masses = new DMass[width * height];
bodyMatrix = new DBody[width][height];
geomMatrix = new DGeom[width][height];
int numberOfJoints = (width)*(height-1) + (width-1)*(height) + (width-1)*(height-1)*2;
DBallJoint[] joints = new DBallJoint[numberOfJoints];
// place spheres
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bodyMatrix[x][y] = OdeHelper.createBody(env.getWorld());
bodyMatrix[x][y].setPosition(initialPos.get0() - (x+1) * distance
+ offset, initialPos.get1() - (y+1) * distance + offset,
initialPos.get2());
DMass mass = OdeHelper.createMass();
mass.setSphereTotal(0.000001, radius);
bodyMatrix[x][y].setMass(mass);
bodies.add(bodyMatrix[x][y]);
geomMatrix[x][y] = OdeHelper.createSphere(env.getSpace(),
radius);
geoms.add(geomMatrix[x][y]);
geomMatrix[x][y].setBody(bodyMatrix[x][y]);
}
}
bodyMatrix[0][0].setKinematic();
bodyMatrix[width-1][0].setKinematic();
bodyMatrix[0][height-1].setKinematic();
bodyMatrix[width-1][height-1].setKinematic();
int jointCounter = 0;
// attach vertical constraints
for (int x = 0; x < width; x++) {
for (int y = 0; y < height - 1; y++) {
DBallJoint joint = joints[jointCounter];
joint = OdeHelper.createBallJoint(env.getWorld());
joint.attach(bodyMatrix[x][y],bodyMatrix[x][y + 1]);
joint.setParam(DJoint.PARAM_N.dParamCFM1, cfmAng1);
joint.setParam(DJoint.PARAM_N.dParamERP1, erpAng1);
joint.setParam(DJoint.PARAM_N.dParamFMax1, fmaxAng1);
jointCounter++;
}
}
//
// attach horizontal constraints
for (int x = 0; x < width - 1; x++) {
for (int y = 0; y < height; y++) {
DBallJoint joint = joints[jointCounter];
joint = OdeHelper.createBallJoint(env.getWorld());
joint.attach(bodyMatrix[x][y],bodyMatrix[x + 1][y]);
joint.setParam(DJoint.PARAM_N.dParamCFM1, cfmAng1);
joint.setParam(DJoint.PARAM_N.dParamERP1, erpAng1);
joint.setParam(DJoint.PARAM_N.dParamFMax1, fmaxAng1);
jointCounter++;
}
}
// attach diagonal constraints
for (int x = 0; x < width - 1; x++) {
for (int y = 0; y < height - 1; y++) {
DBallJoint joint = joints[jointCounter];
joint = OdeHelper.createBallJoint(env.getWorld());
joint.attach(bodyMatrix[x][y],bodyMatrix[x + 1][y + 1]);
joint.setParam(DJoint.PARAM_N.dParamCFM1, cfmAng1);
joint.setParam(DJoint.PARAM_N.dParamERP1, erpAng1);
joint.setParam(DJoint.PARAM_N.dParamFMax1, fmaxAng1);
jointCounter++;
}
}
////
// attach diagonal constraints
for (int x = 0; x < width-1; x++) {
for (int y = 0; y < height - 1; y++) {
DBallJoint joint = joints[jointCounter];
joint = OdeHelper.createBallJoint(env.getWorld());
joint.attach(bodyMatrix[x+1][y],bodyMatrix[x][y + 1]);
joint.setParam(DJoint.PARAM_N.dParamCFM1, cfmAng1);
joint.setParam(DJoint.PARAM_N.dParamERP1, erpAng1);
joint.setParam(DJoint.PARAM_N.dParamFMax1, fmaxAng1);
jointCounter++;
}
}
}
/*
* (non-Javadoc)
*
* @see eas.simulation.spatial.sim3D.physicalSimulation.standardAgents.
* AbstractAgentLWJGLdrawable#isDrawableInLWJGL()
*/
@Override
public boolean isDrawableInLWJGL() {
return true;
}
@Override
public void drawInLWJGL() {
VideoPluginLWJGL.dsSetColor(.5f, .5f, .5f);
VideoPluginLWJGL.setTexture(DS_TEXTURE_NUMBER.DS_GROUND);
VideoPluginLWJGL.drawCloth(bodyMatrix);
// VideoPluginLWJGL.drawClothAsPatches(bodyMatrix);
// super.drawInLWJGL() ;
}
}