We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Framerate / redraw problem
Page Index Toggle Pages: 1
Framerate / redraw problem (Read 788 times)
Framerate / redraw problem
Feb 8th, 2006, 5:10pm
 
Hi, I'm only new to processing and have written a simple automated car(rectangle) which steers around the screen.  I'm not redrawing the background atm because I'm playing with the idea of drawing the path it travels.  It's works, but my problem is that I want it to draw faster but move the same amount each frame.  I have tried wrapping a for loop around the function calls in the draw() structure, but it doesn't work as expected.  Does anyone know hwo to make the car(rectangle) draw faster lines all over the place but still move at the same increment / space each frame?

Here's the code:

||----------------------->

float xVelo, yVelo, x, y, angle, speed, rotation, t;

int screenWidth = 500;
int screenHeight = 500;
int i = 0;

Car car = new Car(screenWidth/2,screenHeight/2,4,2);

void setup() {
 //frame.setUndecorated(true);
 size(screenWidth,screenHeight);
 noStroke();
 background(0);
 framerate(1000);
}

void draw() {
   //background(0);
   car.Move();
   car.Render();
   smooth();
}

class Car {
 float x,y,a,b,speed,steerRate;

 //Main constructor, setup basic parameters
 Car(float ix, float iy, int ia, int ib) {
   x = ix;
   y = iy;
   a = ia;
   b = ib;
   
   speed = 1;
   steerRate = 0.05;
 }
 
 //Method called to move a car
 void Move() {
   float xpos = x;
   float ypos = y;
   
   float rand = random(50);
   if (rand > 48) {
     steerRate = steerRate * -1;
   }    
   
   //Calculate rotation and speed
   t += steerRate;
   angle = cos(t);
   
   //Simple screen edge limitations
   if (x > screenWidth) {
     x = screenWidth;
     speed = speed * -1;
   } else if (x<0) {
     x = 0;
     speed = speed * -1;
   }
   
   if (y > screenHeight) {
     y = screenHeight;
     speed = speed * -1;
   } else if (y<0) {
     y = 0;
     speed = speed * -1;
   }
   
   //Calculate movement
   xVelo = cos(angle) * speed;
   yVelo = sin(angle) * speed;
 
   //Apply movement calculations
   x += xVelo;
   y += yVelo;
 }
 
 //Method called to render a car
 void Render() {
   translate(x,y);
   rotate(angle);
   rectMode(CENTER);
   rect(0,0,a,b);
 }
}

||----------------->
Re: Framerate / redraw problem
Reply #1 - Feb 8th, 2006, 5:24pm
 
I should probably mention that I want to ultimately have this running fast at 1280*1024 with frame.setUndecorated(true)
Re: Framerate / redraw problem
Reply #2 - Feb 8th, 2006, 5:43pm
 
Here's a suggestion. Instead of essentially implementing a point plot, I'd record 2 point calcs at a time in an array and then use a line call-something like:
line(x[i-1], y[i-1], x[i], y[i])

I'd keep the framerate between 30-60 (preferably nearer 30) and increase the speed variable. The point separation you'll get will be patched by the line segments. Obviously if the separation becomes too great, you'll get some stepping, but you could always then try the curve() call.

My sense looking at your code is that you may be new to Processing, but you're not new to coding, so I suspect you can implement this. Obviously, your current implementation, utilizing iterated translations, will need to be changed, but that shouldn't present a problem.
Does this make sense?

ira
Re: Framerate / redraw problem
Reply #3 - Feb 9th, 2006, 3:19am
 
Thx for the reply, your right I'm not new to programming but I'm not an experienced programmer either.  I'll try to implement your suggestions, I should be able to figure it out.

Ultimately I'm trying to make something similar to this:

http://seltar.wliia.org/sketch.php?pid=17

I'm not trying to replicate it but I would like to have the same redraw speed.  The example has many lines which draw out faster than my single line.  One of the problems with my code is that it draws one rectangle per draw() loop but I want it to do many eg 100 - 1000.  Whenever I implement a for loop wrapping the contents inside the draw() loop it doesn't behave as I expected and draw many rectangles in a seamless line, rather it seems to jump wildly around the screen far ahead of the previous point.
Re: Framerate / redraw problem
Reply #4 - Feb 9th, 2006, 4:04am
 
selter's piece is nice. If you can't get the implementation to work, let me know and I'll give it a try.

ira
Page Index Toggle Pages: 1