Problems with trimesh

Hi all,



I'm having a problem with collision. Mi class generate a rectangle using the trimesh but when an object collide with the rectangle the car goes crazy. I don`t know what it happen. Somebody could help me??  here is the code than I'm using:




package util;

import java.nio.FloatBuffer;

import com.jme.math.Vector3f;
import com.jme.scene.TriMesh;
import com.jme.scene.batch.TriangleBatch;
import com.jme.util.geom.BufferUtils;

/**
 * <code>Quad</code> defines a four sided, two dimensional shape. The local
 * height of the <code>Quad</code> defines it's size about the y-axis, while
 * the width defines the x-axis. The z-axis will always be 0.
 *
 * @author Mark Powell
 * @version $Id: Quad.java,v 1.14 2008/04/23 22:44:41 renanse Exp $
 */
public class Recta extends TriMesh implements Tramo{
   public int coorx1;
   public int coory1;
   public int coorz1;
   public int coorx2;
   public int coory2;
   public int coorz2;
   public int largo;
   public int largo2;
   public float posXini;
   public float posYini;
   public float posZini;
   public float posXini2;
   public float posYini2;
   public float posZini2;
   public float posXfin;
   public float posYfin;
   public float posZfin;
   public float posXfin2;
   public float posYfin2;
   public float posZfin2;
   public String terreno;
   public String nombre;

   
   
    public Recta() {
       
    }
   
   /**
    * Constructor creates a new <code>Quad</code> object. That data for the
    * <code>Quad</code> is not set until a call to <code>initialize</code>
    * is made.
    *
    * @param name
    *            the name of this <code>Quad</code>.
    */
   public Recta(String name) {
      this(name, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, name, name);
   }

   /**
    * Constructor creates a new <code>Quade</code> object with the provided
    * width and height.
    *
    * @param name
    *            the name of the <code>Quad</code>.
    * @param width
    *            the width of the <code>Quad</code>.
    * @param height
    *            the height of the <code>Quad</code>.
    */
   public Recta(String name, int coorx1, int coory1, int coorz1, int coorx2,
         int coory2, int coorz2, int largo, int largo2, float posXini,
         float posYini, float posZini, float posXini2, float posYini2,
         float posZini2, String terreno, String nombre) {
      super(name);
      initialize(coorx1, coory1, coorz1, coorx2,
            coory2, coorz2, largo, largo2, posXini,
            posYini, posZini, posXini2, posYini2,
            posZini2, terreno, nombre);
   }

   /**
    * <code>resize</code> changes the width and height of the given quad by
    * altering its vertices.
    *
    * @param width
    *            the new width of the <code>Quad</code>.
    * @param height
    *            the new height of the <code>Quad</code>.
    */
   public void resize(float width, float height) {
        TriangleBatch batch = getBatch(0);
      batch.getVertexBuffer().clear();
      batch.getVertexBuffer().put(-width / 2f).put(height / 2f).put(0);
      batch.getVertexBuffer().put(-width / 2f).put(-height / 2f).put(0);
      batch.getVertexBuffer().put(width / 2f).put(-height / 2f).put(0);
      batch.getVertexBuffer().put(width / 2f).put(height / 2f).put(0);
   }

   /**
    *
    * <code>initialize</code> builds the data for the <code>Quad</code>
    * object.
    * @param nombre
    *
    *
    * @param width
    *            the width of the <code>Quad</code>.
    * @param height
    *            the height of the <code>Quad</code>.
    */
   public void initialize(int coorx1, int coory1, int coorz1, int coorx2,
         int coory2, int coorz2, int largo, int largo2, float posXini,
         float posYini, float posZini, float posXini2, float posYini2,
         float posZini2, String terreno, String nombre) {
      this.coorx1=coorx1;
      this.coory1=coory1;
      this.coorz1=coorz1;
      this.coorx2=coorx2;
      this.coory2=coory2;
      this.coorz2=coorz2;
      this.largo=largo;
      this.largo2=largo2;
      ////tramo/////
      this.posXini=posXini;
      this.posYini=posYini;
      this.posZini=posZini;
      this.posXini2=posXini2;
      this.posYini2=posYini2;
      this.posZini2=posZini2;
      this.terreno=terreno;
      this.nombre = nombre;
      
      int s1x, s1y, s1z, s2x, s2y, s2z;
      if (coorx1 < 0) {
         s1x = coorx1 * -1;
      } else {
         s1x = coorx1;
      }
      if (coory1 < 0) {
         s1y = coory1 * -1;
      } else {
         s1y = coory1;
      }
      if (coorz1 < 0) {
         s1z = coorz1 * -1;
      } else {
         s1z = coorz1;
      }
      int s1 = s1x + s1y + s1z;
      float coorx1b = (float) coorx1 / (float) s1;
      float coory1b = (float) coory1 / (float) s1;
      float coorz1b = (float) coorz1 / (float) s1;

      if (coorx2 < 0) {
         s2x = coorx2 * -1;
      } else {
         s2x = coorx2;
      }
      if (coory2 < 0) {
         s2y = coory2 * -1;
      } else {
         s2y = coory2;
      }
      if (coorz2 < 0) {
         s2z = coorz2 * -1;
      } else {
         s2z = coorz2;
      }
      int s2 = s2x + s2y + s2z;
      float coorx2b = (float) coorx2 / (float) s2;
      float coory2b = (float) coory2 / (float) s2;
      float coorz2b = (float) coorz2 / (float) s2;

      if (posXini2 == 0 && posYini2 == 0 && posZini2 == 0) {
         posYini2 = -15;
      }

      
        TriangleBatch batch = getBatch(0);
      batch.setVertexCount(4);
      batch.setVertexBuffer(BufferUtils.createVector3Buffer(batch.getVertexCount()));
      batch.setNormalBuffer(BufferUtils.createVector3Buffer(batch.getVertexCount()));
        FloatBuffer tbuf = BufferUtils.createVector2Buffer(batch.getVertexCount());
        setTextureBuffer(0,tbuf);
       batch.setTriangleQuantity(2);
       batch.setIndexBuffer(BufferUtils.createIntBuffer(batch.getTriangleCount() * 3));
      
      if(nombre.equals("fin")){
         batch.getVertexBuffer().put(posXini).put(posZini).put(posYini);

         batch.getVertexBuffer().put(posXini2).put(posZini2).put(posYini2);

         batch.getVertexBuffer().put(0).put(0).put(0);

         batch.getVertexBuffer().put(0).put(0).put(-15);   
      }else{

      batch.getVertexBuffer().put(posXini).put(posZini).put(posYini);

      batch.getVertexBuffer().put(posXini2).put(posZini2).put(posYini2);

      batch.getVertexBuffer().put(posXini+(coorx1b*largo)).put(posZini+(coorz1b*largo)).put(posYini+(coory1b*largo));

      batch.getVertexBuffer().put(posXini2+(coorx2b*largo2)).put(posZini2+(coorz2b*largo2)).put(posYini2+(coory2b*largo2));
      }
      //valor de los 2 ultimos puntos de la recta
      //donde debe comenzar la siguiente
      this.posXfin2=posXini+(coorx1b*largo);
      this.posYfin2=posYini+(coory1b*largo);
      this.posZfin2=posZini+(coorz1b*largo);
      this.posXfin=posXini2+(coorx2b*largo2);
      this.posYfin=posYini2+(coory2b*largo2);
      this.posZfin=posZini2+(coorz2b*largo2);
      
      
      batch.getNormalBuffer().put(0).put(0).put(1);
      batch.getNormalBuffer().put(0).put(0).put(1);
      batch.getNormalBuffer().put(0).put(0).put(1);
      batch.getNormalBuffer().put(0).put(0).put(1);

       
      tbuf.put(0).put(1);
        tbuf.put(0).put(0);
        tbuf.put(1).put(0);
        tbuf.put(1).put(1);

       batch.getIndexBuffer().put(0);
       batch.getIndexBuffer().put(1);
       batch.getIndexBuffer().put(2);
       batch.getIndexBuffer().put(1);
       batch.getIndexBuffer().put(3);
       batch.getIndexBuffer().put(2);

      
   }

   /**
    * <code>getCenter</code> returns the center of the <code>Quad</code>.
    *
    * @return Vector3f the center of the <code>Quad</code>.
    */
   public Vector3f getCenter() {
      return worldTranslation;
   }

   public String getNombre() {
      return nombre;
   }

   public void setNombre(String nombre) {
      this.nombre = nombre;
   }

   public String getTerreno() {
      return terreno;
   }

   
   
   

}





Thanks 4 all.
seldom said:

Hi all,

I'm having a problem with collision. Mi class generate a rectangle using the trimesh but when an object collide with the rectangle the car goes crazy. I don`t know what it happen. Somebody could help me??  here is the code than I'm using:


package util;

import java.nio.FloatBuffer;

import com.jme.math.Vector3f;
import com.jme.scene.TriMesh;
import com.jme.scene.batch.TriangleBatch;
import com.jme.util.geom.BufferUtils;

/**
 * <code>Quad</code> defines a four sided, two dimensional shape. The local
 * height of the <code>Quad</code> defines it's size about the y-axis, while
 * the width defines the x-axis. The z-axis will always be 0.
 *
 * @author Mark Powell
 * @version $Id: Quad.java,v 1.14 2008/04/23 22:44:41 renanse Exp $
 */
public class Recta extends TriMesh implements Tramo{
   public int coorx1;
   public int coory1;
   public int coorz1;
   public int coorx2;
   public int coory2;
   public int coorz2;
   public int largo;
   public int largo2;
   public float posXini;
   public float posYini;
   public float posZini;
   public float posXini2;
   public float posYini2;
   public float posZini2;
   public float posXfin;
   public float posYfin;
   public float posZfin;
   public float posXfin2;
   public float posYfin2;
   public float posZfin2;
   public String terreno;
   public String nombre;

   
   
    public Recta() {
       
    }
   
   /**
    * Constructor creates a new <code>Quad</code> object. That data for the
    * <code>Quad</code> is not set until a call to <code>initialize</code>
    * is made.
    *
    * @param name
    *            the name of this <code>Quad</code>.
    */
   public Recta(String name) {
      this(name, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, name, name);
   }

   /**
    * Constructor creates a new <code>Quade</code> object with the provided
    * width and height.
    *
    * @param name
    *            the name of the <code>Quad</code>.
    * @param width
    *            the width of the <code>Quad</code>.
    * @param height
    *            the height of the <code>Quad</code>.
    */
   public Recta(String name, int coorx1, int coory1, int coorz1, int coorx2,
         int coory2, int coorz2, int largo, int largo2, float posXini,
         float posYini, float posZini, float posXini2, float posYini2,
         float posZini2, String terreno, String nombre) {
      super(name);
      initialize(coorx1, coory1, coorz1, coorx2,
            coory2, coorz2, largo, largo2, posXini,
            posYini, posZini, posXini2, posYini2,
            posZini2, terreno, nombre);
   }

   /**
    * <code>resize</code> changes the width and height of the given quad by
    * altering its vertices.
    *
    * @param width
    *            the new width of the <code>Quad</code>.
    * @param height
    *            the new height of the <code>Quad</code>.
    */
   public void resize(float width, float height) {
        TriangleBatch batch = getBatch(0);
      batch.getVertexBuffer().clear();
      batch.getVertexBuffer().put(-width / 2f).put(height / 2f).put(0);
      batch.getVertexBuffer().put(-width / 2f).put(-height / 2f).put(0);
      batch.getVertexBuffer().put(width / 2f).put(-height / 2f).put(0);
      batch.getVertexBuffer().put(width / 2f).put(height / 2f).put(0);
   }

   /**
    *
    * <code>initialize</code> builds the data for the <code>Quad</code>
    * object.
    * @param nombre
    *
    *
    * @param width
    *            the width of the <code>Quad</code>.
    * @param height
    *            the height of the <code>Quad</code>.
    */
   public void initialize(int coorx1, int coory1, int coorz1, int coorx2,
         int coory2, int coorz2, int largo, int largo2, float posXini,
         float posYini, float posZini, float posXini2, float posYini2,
         float posZini2, String terreno, String nombre) {
      this.coorx1=coorx1;
      this.coory1=coory1;
      this.coorz1=coorz1;
      this.coorx2=coorx2;
      this.coory2=coory2;
      this.coorz2=coorz2;
      this.largo=largo;
      this.largo2=largo2;
      ////tramo/////
      this.posXini=posXini;
      this.posYini=posYini;
      this.posZini=posZini;
      this.posXini2=posXini2;
      this.posYini2=posYini2;
      this.posZini2=posZini2;
      this.terreno=terreno;
      this.nombre = nombre;
      
      int s1x, s1y, s1z, s2x, s2y, s2z;
      if (coorx1 < 0) {
         s1x = coorx1 * -1;
      } else {
         s1x = coorx1;
      }
      if (coory1 < 0) {
         s1y = coory1 * -1;
      } else {
         s1y = coory1;
      }
      if (coorz1 < 0) {
         s1z = coorz1 * -1;
      } else {
         s1z = coorz1;
      }
      int s1 = s1x + s1y + s1z;
      float coorx1b = (float) coorx1 / (float) s1;
      float coory1b = (float) coory1 / (float) s1;
      float coorz1b = (float) coorz1 / (float) s1;

      if (coorx2 < 0) {
         s2x = coorx2 * -1;
      } else {
         s2x = coorx2;
      }
      if (coory2 < 0) {
         s2y = coory2 * -1;
      } else {
         s2y = coory2;
      }
      if (coorz2 < 0) {
         s2z = coorz2 * -1;
      } else {
         s2z = coorz2;
      }
      int s2 = s2x + s2y + s2z;
      float coorx2b = (float) coorx2 / (float) s2;
      float coory2b = (float) coory2 / (float) s2;
      float coorz2b = (float) coorz2 / (float) s2;

      if (posXini2 == 0 && posYini2 == 0 && posZini2 == 0) {
         posYini2 = -15;
      }

      
        TriangleBatch batch = getBatch(0);
      batch.setVertexCount(4);
      batch.setVertexBuffer(BufferUtils.createVector3Buffer(batch.getVertexCount()));
      batch.setNormalBuffer(BufferUtils.createVector3Buffer(batch.getVertexCount()));
        FloatBuffer tbuf = BufferUtils.createVector2Buffer(batch.getVertexCount());
        setTextureBuffer(0,tbuf);
       batch.setTriangleQuantity(2);
       batch.setIndexBuffer(BufferUtils.createIntBuffer(batch.getTriangleCount() * 3));
      
      if(nombre.equals("fin")){
         batch.getVertexBuffer().put(posXini).put(posZini).put(posYini);

         batch.getVertexBuffer().put(posXini2).put(posZini2).put(posYini2);

         batch.getVertexBuffer().put(0).put(0).put(0);

         batch.getVertexBuffer().put(0).put(0).put(-15);   
      }else{

      batch.getVertexBuffer().put(posXini).put(posZini).put(posYini);

      batch.getVertexBuffer().put(posXini2).put(posZini2).put(posYini2);

      batch.getVertexBuffer().put(posXini+(coorx1b*largo)).put(posZini+(coorz1b*largo)).put(posYini+(coory1b*largo));

      batch.getVertexBuffer().put(posXini2+(coorx2b*largo2)).put(posZini2+(coorz2b*largo2)).put(posYini2+(coory2b*largo2));
      }
      //valor de los 2 ultimos puntos de la recta
      //donde debe comenzar la siguiente
      this.posXfin2=posXini+(coorx1b*largo);
      this.posYfin2=posYini+(coory1b*largo);
      this.posZfin2=posZini+(coorz1b*largo);
      this.posXfin=posXini2+(coorx2b*largo2);
      this.posYfin=posYini2+(coory2b*largo2);
      this.posZfin=posZini2+(coorz2b*largo2);
      
      
      batch.getNormalBuffer().put(0).put(0).put(1);
      batch.getNormalBuffer().put(0).put(0).put(1);
      batch.getNormalBuffer().put(0).put(0).put(1);
      batch.getNormalBuffer().put(0).put(0).put(1);

       
      tbuf.put(0).put(1);
        tbuf.put(0).put(0);
        tbuf.put(1).put(0);
        tbuf.put(1).put(1);

       batch.getIndexBuffer().put(0);
       batch.getIndexBuffer().put(1);
       batch.getIndexBuffer().put(2);
       batch.getIndexBuffer().put(1);
       batch.getIndexBuffer().put(3);
       batch.getIndexBuffer().put(2);

      
   }

   /**
    * <code>getCenter</code> returns the center of the <code>Quad</code>.
    *
    * @return Vector3f the center of the <code>Quad</code>.
    */
   public Vector3f getCenter() {
      return worldTranslation;
   }

   public String getNombre() {
      return nombre;
   }

   public void setNombre(String nombre) {
      this.nombre = nombre;
   }

   public String getTerreno() {
      return terreno;
   }

   
   
   

}





Thanks 4 all.