Strange slowdown at odd multipes of PI/4
in
Programming Questions
•
3 years ago
This class represents an "endless" grid that is supposed to scroll under a player ship in a game. It rotates when the player turns. There's a strange thing that happens, though; processor usage shoots way up whenever the grid is at 45deg angles to the window edges! If I am drawing and throwing away the same number of lines per frame, why should the angle matter at all?
- class GridField{
float gridSpacing;
float hLinOrig;
float vLinOrig;
float xOffset;
float yOffset;
float directionRads;
float renderLen;
int index; float measure;
float raw;
GridField(float spacing, float xStart, float yStart){
gridSpacing = spacing; xOffset = xStart; yOffset = yStart;
directionRads = 0.0;
renderLen = max(WINWIDTH,WINHEIGHT) * 1.5;
calc_first_lines(xStart, yStart);
index = 0;
}
void calc_first_lines(float iX, float iY){
hLinOrig = - yOffset % gridSpacing;
vLinOrig = - xOffset % gridSpacing;
}
void move(float dX, float dY){
xOffset += dX; yOffset += dY;
}
void turn(float dir){
raw = directionRads + dir;
if(raw < 0){
directionRads = TWO_PI + raw;
}else if(raw > TWO_PI){
directionRads = raw - TWO_PI;
}else{directionRads = raw;}
//if(abs(directionRads) > HALF_PI && yOffset > 0){yOffset *= -1;}//else
//if(abs(directionRads) < HALF_PI && yOffset < 0){yOffset *= -1;}
}
void place(float iX, float iY, float iBearing){
xOffset = iX; yOffset = iY; directionRads = iBearing;
}
void update(){
pushMatrix();
//rotate(directionRads);
translate(wreckerXAnchor,wreckerYAnchor);
rotate(directionRads);
index = 1; measure = index * gridSpacing;
calc_first_lines(xOffset, yOffset);
line(-0.5 * renderLen, hLinOrig, 0.5 * renderLen, hLinOrig);
line(vLinOrig, -0.5 * renderLen, vLinOrig, 0.5 * renderLen);
while(measure < renderLen / 2){
// draw horizontal lines
line(-0.5 * renderLen, hLinOrig + measure, 0.5 * renderLen, hLinOrig + measure);
line(-0.5 * renderLen, hLinOrig - measure, 0.5 * renderLen, hLinOrig - measure);
// draw vertical lines
line(vLinOrig + measure, -0.5 * renderLen, vLinOrig + measure, 0.5 * renderLen);
line(vLinOrig - measure, -0.5 * renderLen, vLinOrig - measure, 0.5 * renderLen);
// increment for next set of lines
index++; measure = index * gridSpacing;
}
//println(vLinOrig); print(hLinOrig);
//println(directionRads);
popMatrix();
}
}
1