Loading...
Logo
Processing Forum
Hello , Im having problems witch my sketch . I have used that class definition in eclipse :

package misketch2;

import processing.opengl.*;
import processing.core.*;

public  class Ficha {
   
    PApplet parent;
    final int ancho_casilla = 200/8;
   
    public Ficha(PApplet p){
        parent=p;
    }
   
    public void mover2d( int posx , int posy){
        parent.pushMatrix();
        Cilindro((ancho_casilla-4), 40, 20);
        parent.translate(posx,posy);
        parent.popMatrix();
    }
   
    public void mover3d (int posx, int posy, int posz){
        parent.pushMatrix();         
        parent.translate(posx,posy,posz);
        parent.popMatrix();
    }
    public void dibujar(){
        Cilindro((ancho_casilla-4), 40, 20);       
    }
    public void rotar(){
        parent.pushMatrix();
        parent.rotateX(4.7F);
        parent.popMatrix();
    }


And then I do something like that in my draw() function on the main class:

      Ficha ficha1 = new Ficha(this);
      ficha1.dibujar();
      ficha1.rotar();
      rotateX(rotX + distY);
      ficha1.mover2d(width/2,height/2);

The thing that I want to know is why all works fine except all the callings to the pushMatrix(), translate senteces from inside the class methods,etc.
Does anyone know why it happens that?

Replies(4)

The methods rotar and mover3d will have no effect because the pushMatrix will save a copy of the current matrix, then the translate/rotate will transform the current matrix and finally the popMatrix will restore the original matrix. This effectively cancels out the translate/rotate operation.

 
Thanks quarks but I think that's not the problem because I have removed de pushMatrix() and popMatrix() sentences which are inside the method rotar() and it does not make nothing as before.

I didn't say that was the complete solution since I can't see all your code but I will make some comment.

You state that this statement is in the draw
Ficha ficha1 = new Ficha(this);

which is not a good idea since you are creating a new object every draw cycle - thats approximately 60 times a second.

I am not sure what the class Ficha represents but I assume you will create many objects from this class. In that case I would recommend that you add attributes/fields to the class to store its current position and rotation. Then create methods which allow you to change these values. Finally a draw /render method that

pushes the matrix
translate according to the position attributes
rotate according to the rotation attributes
pop the matrix

this gives a clear seperation between the setting of an objects position/rotation and its display. Having all the graphic commands in just one method will simplify debugging your program.

Hello quarks, I have realized that is not a good idea to do the new inside the draw function ( thank you  ) , the purpose of my application it´s to create a 3d checkers game and the class Ficha it would be the checker class , and i want to rotate , translate them across the checkerboard.

And the thing that i wanted to mean is that although I removed the pushMatrix() and the popMatrix() from inside the method rotar and then all continues like it nothing happened.

Anyway thanks in advance and I will put the complete code of the application:

File Ficha.java

package misketch2;

import processing.opengl.*;
import processing.core.*;


public  class Ficha {
   
    PApplet parent;
    final int ancho_casilla = 200/8;
//    String color = new String() ;
//   
//    public Ficha ( String color ){
//        this.color = color;
//    }
   
    public Ficha(PApplet p){
        parent=p;
    }
   
    public void mover2d( int posx , int posy){
       
        //Cilindro((ancho_casilla-4), 40, 20);
        parent.translate(posx,posy);
       
    }
   
    public void mover3d (int posx, int posy, int posz){
        parent.pushMatrix();         
        parent.translate(posx,posy,posz);
        parent.popMatrix();
    }
    public void dibujar(){
        Cilindro((ancho_casilla-4), 40, 20);       
    }
    public void rotar(){
       
        parent.rotateX(4.7F);
       
    }
   
    public void Cilindro (float w, float h, int sides){

        float angle;
        float[] x = new float[sides+1];
        float[] z = new float[sides+1];
     
        //get the x and z position on a circle for all the sides
        for (int i=0; i < x.length; i++) {
          angle = parent.TWO_PI / (sides) * i;
          x[i] = parent.sin(angle) * w;
          z[i] = parent.cos(angle) * w;
        }
     
        //draw the top of the cylinder
        parent.beginShape(parent.TRIANGLE_FAN);
     
        parent.vertex(0, -h/2, 0);
       
        for (int i=0; i < x.length; i++) {
          parent.vertex(x[i], -h/2, z[i]);
        }
     
        parent.endShape();
     
        //draw the center of the cylinder
        parent.beginShape(parent.QUAD_STRIP);
     
        for (int i=0; i < x.length; i++) {
         
          parent.vertex(x[i], -h/2, z[i]);
          parent.vertex(x[i], h/2, z[i]);
        }
     
        parent.endShape();
     
        //draw the bottom of the cylinder
        parent.beginShape(parent.TRIANGLE_FAN);
     
        parent.vertex(0, h/2, 0);
     
        for (int i=0; i < x.length; i++) {
          parent.vertex(x[i], h/2, z[i]);
        }
     
        parent.endShape();
     }

     
}  

And file Damas.java

public class Damas extends PApplet {
   
    static int escala=200;

    static  float rotX = 0.0F, rotY =  0.0F;
    static int lastX, lastY;
    static float distX = 0.0F;
    static float distY = 0.0F;

    //Textura
    static PImage foto;   

   
      public  void setup() {
          size(800, 480, OPENGL);
          noStroke();
          foto = loadImage("texturadamas.png");
          // Vamos a referirnos a las coordenadas de la
          // textura de (0,0) a (1,1)
          textureMode(NORMAL);
         


          //iniciar_tablero();
          //inicializar_fichas();

      }

      public void draw() {
              background(0);
              lights();


              dibujar_tablero();
              dibujar_fichas();

      }

   
   
   
   
    public void mousePressed()
    {
        lastX = mouseX;
        lastY = mouseY;
    }
    public void mouseDragged()
    {
        distX = radians(mouseX - lastX);
        distY = radians(lastY - mouseY);
    }
    public void mouseReleased()
    {
        rotX += distY;
        rotY += distX;
        distX = distY = 0.0F;
    }
void dibujar_tablero(){
       
        pushMatrix();
        translate(width/2, height/2); 
        rotateX(rotX + distY);
       // rotateX(-4.8);  
        scale(escala, escala,5 );
        beginShape(QUADS);
        texture(foto);
        //
        // Proporcionamos los vértices
        // 3D de cada cara del cubo.
        // Los dos valores finales
        // son las coordenadas de la
        // textura que corresponde
        // al vértice en cuestión
        // +Z "front" face
     
        vertex(-1, -1, 1, 0, 0);
        vertex( 1, -1, 1, 1, 0);
        vertex( 1, 1, 1, 1, 1);
        vertex(-1, 1, 1, 0, 1);
        // -Z "back" face
     
        vertex( 1, -1, -1, 0, 0);
        vertex(-1, -1, -1, 1, 0);
        vertex(-1, 1, -1, 1, 1);
        vertex( 1, 1, -1, 0, 1);
        // +Y "bottom" face
        vertex(-1, 1, 1, 0, 0);
        vertex( 1, 1, 1, 1, 0);
        vertex( 1, 1, -1, 1, 1);
        vertex(-1, 1, -1, 0, 1);
        // -Y "top" face
        vertex(-1, -1, -1, 0, 0);
        vertex( 1, -1, -1, 1, 0);
        vertex( 1, -1, 1, 1, 1);
        vertex(-1, -1, 1, 0, 1);
        // +X "right" face
        vertex( 1, -1, 1, 0, 0);
        vertex( 1, -1, -1, 1, 0);
        vertex( 1, 1, -1, 1, 1);
        vertex( 1, 1, 1, 0, 1);
     
        // -X "left" face
        vertex(-1, -1, -1, 0, 0);
        vertex(-1, -1, 1, 1, 0);
        vertex(-1, 1, 1, 1, 1);
        vertex(-1, 1, -1, 0, 1);
     
        endShape(); 
        popMatrix();
    }

void dibujar_fichas (){
      Ficha ficha1 = new Ficha(this);   
      rotateX(rotX + distY);
      ficha1.dibujar();
      ficha1.rotar();     
      ficha1.mover2d(width/2,height/2);
     


}