edited August 2016 in Library Questions

Can someone explain this framecount*i,and how it is used in the sketch.Because when i put out i,from the equation the vertices dont move independently.Thanks in advance

import peasy.*;
import saito.objloader.*; 

OBJModel model ;
OBJModel tmpmodel ;

PeasyCam cam;
float easing = 0.005;

int VertCount;
PVector[] Verts;

void setup()
  size(800, 600, P3D);

  model = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);

  tmpmodel = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);

  cam = new PeasyCam(this, width/2, height/2, 0, 500);

void draw()

  int VertCount = model.getVertexCount ();
  PVector[] Verts = new PVector[VertCount];


  translate(width/2, height/2, 0);

  for (int i = 0; i < VertCount; i++) {
    //PVector orgv = model.getVertex(i);

    Verts[i]= model.getVertex(i);

    //PVector tmpv = new PVector();
    if (frameCount> 100) {

      float randX = random(-0.05, 0.05);
      float randY = random(0, 0.05);
      float randZ = random(0, 0.05);


      float norX = abs(cos((frameCount+i))) * randX;
      float norY = abs(cos((frameCount+i)) * randY);
      float norZ = abs(cos((frameCount+i)) * randZ);


      if (Verts[i].x > width/2|| Verts[i].x < -width/2) {
      if (Verts[i].y > height/2|| Verts[i].y < -height/2) {

      if (Verts[i].z < -600/2 || Verts[i].z > 600/2) {  //note that Zaxis goes 'into' the screen

    PVector locas = new PVector(Verts[i].x, Verts[i].y, Verts[i].z);
    tmpmodel.setVertex(i, locas.x, locas.y, locas.z);


  translate(width/2, height/2, 0);
  box(width, height, 600);


  • for (int i = 0; i < VertCount; i++) {

    i is 0 for the first vertex, 1 for the second etc. so each vertex is rotated further than the one before.

    framecount changes with time starting at 1, so the first time through the first vertex is at 0 + 1 = 1, the second time through it's 0 + 2 = 2. so it moves with time. this is what causes the animation.

  • ok thank you,i have an other problem,i want every vertex move as a particle,i tried for( : ) loop but it gives null pointer exception,with for( ; ; ;) loop the move goes to the whole object,not individual vertex.Is any way to get the vertices and add them individual move?

  • Sorry for the push,but i have to make it for my degree until September,and i am in hurry.

  • edited August 2016

    I have tidied up your code getting rid of all that vertical whitespace so it is clearer.

    Just to start it is not frameCount*1it is frameCount+1.

    Looking at your code you are using the unchanged model vertices for the first 100 frames then in lines 58 - 78 you are modifying the individual vertex position. Do you understand what is actually happening in this code? Did you write it?

    Unfortunately it doesn't make much sense mathematically. The use of the cos functione seems to indicate that you want some cyclic function e.g. maybe to produce some vibration.

    Consider the statement float norX = abs(cos((frameCount+i))) * randX; you have an extra pair of parenthesis which we can remove to get

    float norX = abs(cos(frameCount+i)) * randX;

    Take the inner bit first cos(frameCount+i) this will calculate the cosine of frameCount + 1 radians. The cosine of an angle is always in the range -1 to +1 so abs(cos(frameCount+i)) will always return a value of 0 to +1. The distribution of the cosine means that approximately 30% will be in the range 0-0.5 and the remaining 70% in the range 0.5-1.0

    So you have a value in the range 0.0-1.0 which you multiple by a random number (randX) in the range -0.05 and 0.05 so the result will still be a random number in the range -0.05 and 0.05 with 30% in the range -0.05 and 70% in the range 0-0.05

    The point is that if you multiple a number by a random number you still get a random number

    I suggest that you replace the lines

    float randX = random(-0.05, 0.05);
    float norX = abs(cos((frameCount+i))) * randX;

    with the single line

    float norX = cos(frameCount+i) * 0.05;

    This will give you a cyclic number in the range - 0.05 to 0.05

    The only other observation is that the increment inside the cosine is rather course (1 radian ~ 57.3 degrees) so you might apply adjust it to get a smoother change in norX with

    float norX = cos((frameCount+i)*0.1) * 0.05;

  • yeah i know what is happening in that code,some parts i copy paste them,and then modified them.The parenthesis was a relic from some previous experimentation with cos, sin, abs and noise.To see what is happening,i am somehow new in the concepts of sin and cos and how they are combined .I will see what are you suggesting.I want to ask you,what if i use an for( : ) loop,can i add random move to individual vertices?and not to the whole object?I tried for (PVector v : Verts){ do that }.and it gave me null pointer.Maybe i should copy the array and the do the for each loop? Thanks alot

Sign In or Register to comment.