Package eas.users.students.fabian.cloth

Source Code of eas.users.students.fabian.cloth.ClothAgent2

/*
* 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.DBody;
import org.ode4j.ode.DGeom;
import org.ode4j.ode.DJoint;
import org.ode4j.ode.DLMotorJoint;
import org.ode4j.ode.DMass;
import org.ode4j.ode.OdeConstants;
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 ClothAgent2 extends AbstractAgentODE3D<ClothEnv> {

  /**
     *
     */
    private static final long serialVersionUID = -7321013402054130316L;
    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 cfm = 0;
//  private final double sphereMass = 0.000000001;
//  private final double erp = .000001;
//  private final double fmax = 100000;
 
  private DBody[][] bodyMatrix;
  private DGeom[][] geomMatrix;

  /**
   * @param id
   * @param env
   * @param params
   */
  public ClothAgent2(int id, ClothEnv env, ParCollection params) {
    super(id, env, params);
   
    double offset = width / 2 * (distance + radius);

//    int bodycounter = 0;

    bodyMatrix = new DBody[width][height];
    geomMatrix = new DGeom[width][height];
   
    int numberOfJoints = (width)*(height-1) + (width-1)*(height) + (width-1)*(height-1)*2;
    DLMotorJoint[] joints2 = new DLMotorJoint[numberOfJoints];
   
   

//    bodies = new DBody[width * height];
//    geoms = new DGeom[width * height];
//    masses = new DMass[width * height];

    bodyMatrix = new DBody[width][height];
    geomMatrix = new DGeom[width][height];
   
//    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++) {
        joints2[jointCounter] = OdeHelper.createLMotorJoint(env.getWorld());
        joints2[jointCounter].attach(bodyMatrix[x][y],bodyMatrix[x][y + 1]);

        joints2[jointCounter].setNumAxes(3);
        joints2[jointCounter].setAxis(0,1,1,0,0);
        joints2[jointCounter].setAxis(1,1,0,1,0);
        joints2[jointCounter].setAxis(2,1,0,0,1);
       
        jointCounter++;
      }
    }
    //
     // attach horizontal constraints
    for (int x = 0; x < width - 1; x++) {
      for (int y = 0; y < height; y++) {
        joints2[jointCounter] = OdeHelper.createLMotorJoint(env.getWorld());
        joints2[jointCounter].attach(bodyMatrix[x][y],bodyMatrix[x+1][y]);

        joints2[jointCounter].setNumAxes(3);
        joints2[jointCounter].setAxis(0,1,0,1,0);
        joints2[jointCounter].setAxis(1,1,1,0,0);
        joints2[jointCounter].setAxis(2,1,0,0,1);
       
       
        jointCounter++;
      }
    }
    // attach diagonal constraints
    for (int x = 0; x < width - 1; x++) {
      for (int y = 0; y < height - 1; y++) {
        joints2[jointCounter] = OdeHelper.createLMotorJoint(env.getWorld());
        joints2[jointCounter].attach(bodyMatrix[x][y],bodyMatrix[x + 1][y + 1]);

        joints2[jointCounter].setNumAxes(3);
        joints2[jointCounter].setAxis(0,1,1,1,0);
        joints2[jointCounter].setAxis(1,1,-1,1,0);
        joints2[jointCounter].setAxis(2,1,0,0,1);
       
       
        jointCounter++;
      }
    }
////
    // attach diagonal constraints
    for (int x = 0; x < width-1; x++) {
      for (int y = 0; y < height - 1; y++) {
        joints2[jointCounter] = OdeHelper.createLMotorJoint(env.getWorld());
        joints2[jointCounter].attach(bodyMatrix[x+1][y],bodyMatrix[x][y + 1]);

        joints2[jointCounter].setNumAxes(3);
        joints2[jointCounter].setAxis(0,1,1,-1,0);
        joints2[jointCounter].setAxis(1,1,1,1,0);
        joints2[jointCounter].setAxis(2,1,0,0,1);
       
       
        jointCounter++;
      }
    }
   
    // setup joints
    for (int i = 0; i < joints2.length; i++) {
      if (joints2[i] == null) {
        System.out.println(i);
        break;
      }
     
      joints2[i].setParamFMax(OdeConstants.dInfinity);
      joints2[i].setParamFMax2(OdeConstants.dInfinity);
      joints2[i].setParamFMax3(OdeConstants.dInfinity);
     
      joints2[i].setParam(DJoint.PARAM_N.dParamHiStop1, 0.0001);
      joints2[i].setParam(DJoint.PARAM_N.dParamHiStop2, 0.0001);
      joints2[i].setParam(DJoint.PARAM_N.dParamHiStop3, 0.0001);
     
      joints2[i].setParam(DJoint.PARAM_N.dParamLoStop1, -0.0001);
      joints2[i].setParam(DJoint.PARAM_N.dParamLoStop2, -0.0001);
      joints2[i].setParam(DJoint.PARAM_N.dParamLoStop3, -0.0001);

      joints2[i].setParam(DJoint.PARAM_N.dParamCFM1,.001);
      joints2[i].setParam(DJoint.PARAM_N.dParamCFM2,.001);
      joints2[i].setParam(DJoint.PARAM_N.dParamCFM3,.001);
     
      joints2[i].setParam(DJoint.PARAM_N.dParamStopERP1,.99);
      joints2[i].setParam(DJoint.PARAM_N.dParamStopERP2,.99);
      joints2[i].setParam(DJoint.PARAM_N.dParamStopERP3,.99);
     
//      joints2[i].enable();
    }
  }

  /*
   * (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() ;
  }
}
TOP

Related Classes of eas.users.students.fabian.cloth.ClothAgent2

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.