Package eas.users.students.fabian.cloth

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

/*
* 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.DMass;
import org.ode4j.ode.DUniversalJoint;
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 ClothAgent3 extends AbstractAgentODE3D<ClothEnv> {


  /**
     *
     */
    private static final long serialVersionUID = 2253584057363925644L;

    private double radius = .1;
 
  double distance;
 

//  private final double cfm = 0;
  private final double sphereMass = 0.000001;
//  private final double erp = .000001;
//  private final double fmax = 100000;
 
  private DBody[][] bodyMatrix;
  private DGeom[][] geomMatrix;

  /**
   * @param id
   * @param env
   * @param params
   */
  public ClothAgent3(int id, ClothEnv env, ParCollection params, int gridSize, double distancefactor) {
    super(id, env, params);
   
    this.distance = radius * distancefactor;
   
    double initialOffset = -1.*gridSize/2. * distance;
    DVector3 initialPos = new DVector3(initialOffset, initialOffset, 2.5);
   
   
//    int bodycounter = 0;

    bodyMatrix = new DBody[gridSize][gridSize];
    geomMatrix = new DGeom[gridSize][gridSize];
       
    int numberOfJoints = (gridSize)*(gridSize-1) + (gridSize-1)*(gridSize);// + (width-1)*(height-1)*2;
    DUniversalJoint[] joints2 = new DUniversalJoint[numberOfJoints];
   
    // place spheres
    for (int x = 0; x < gridSize; x++) {
      for (int y = 0; y < gridSize; y++) {
        bodyMatrix[x][y] = OdeHelper.createBody(env.getWorld());
        bodies.add(bodyMatrix[x][y]);
        bodyMatrix[x][y].setPosition(initialPos.get0()+x*distance,
            initialPos.get1() + y * distance,
            initialPos.get2());
        DMass mass = OdeHelper.createMass();
        mass.setSphereTotal(sphereMass, radius);
        masses.add(mass);
        bodyMatrix[x][y].setMass(mass);
        geomMatrix[x][y] = OdeHelper.createSphere(env.getSpace(),
            radius);
        geoms.add(geomMatrix[x][y]);
        geomMatrix[x][y].setBody(bodyMatrix[x][y]);
      }
    }

    bodyMatrix[1][1].setKinematic();
    bodyMatrix[gridSize-2][1].setKinematic();
    bodyMatrix[1][gridSize-2].setKinematic();
    bodyMatrix[gridSize-2][gridSize-2].setKinematic();
   
    int jointCounter = 0;
//    attach vertical constraints
    for (int x = 0; x < gridSize; x++) {
      for (int y = 0; y < gridSize - 1; y++) {
        joints2[jointCounter] = OdeHelper.createUniversalJoint(env.getWorld());
        joints2[jointCounter].attach(bodyMatrix[x][y],bodyMatrix[x][y + 1]);
       
        jointCounter++;
      }
    }
    //
     // attach horizontal constraints
    for (int x = 0; x < gridSize - 1; x++) {
      for (int y = 0; y < gridSize; y++) {
        joints2[jointCounter] = OdeHelper.createUniversalJoint(env.getWorld());
        joints2[jointCounter].attach(bodyMatrix[x][y],bodyMatrix[x+1][y]);

        jointCounter++;
      }
    }
    // attach diagonal constraints
//    for (int x = 0; x < width - 1; x++) {
//      for (int y = 0; y < height - 1; y++) {
//        joints2[jointCounter] = OdeHelper.createUniversalJoint(env.getWorld());
//        joints2[jointCounter].attach(bodyMatrix[x][y],bodyMatrix[x + 1][y + 1]);
//        DVector3 ax1 = null;
//        joints2[jointCounter].getAxis1(ax1);
//      }
//    }

    // attach diagonal constraints
//    for (int x = 0; x < width-1; x++) {
//      for (int y = 0; y < height - 1; y++) {
//        joints2[jointCounter] = OdeHelper.createUniversalJoint(env.getWorld());
//        joints2[jointCounter].attach(bodyMatrix[x+1][y],bodyMatrix[x][y + 1]);
//
//       
//       
//        jointCounter++;
//      }
//    }
   
    // setup joints
    for (int i = 0; i < joints2.length; i++) {
      if (joints2[i] == null) {
        System.out.println(i);
        break;
      }
      joints2[i].setParam(DJoint.PARAM_N.dParamBounce1, 0);
      joints2[i].setParam(DJoint.PARAM_N.dParamBounce2, 0);
    }
  }

  /*
   * (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() ;
  }
 
  public DBody[][] getBodyMatrix() {
    return this.bodyMatrix;
  }
}
TOP

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

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.