Ian Jones
YaBB Newbies
Offline
Posts: 6
QLD, Australia
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); } } ||----------------->