[Solved] Major FPS drop with bullets

Hi

In my project, the user is able to run around, and shoot and kill entities that are chasing it. However, when the user starts to shoot while moving (or even standing still sometimes), the FPS drops from 200 to barely 60. Now, there are still tons of features to be added, so 60 fps now isn’t good at all. Here is how I render by bullets (and check if another one should be ‘shot’):


   for(Bullet bullet : bullets){
			bullet.update(this);
			bullet.render();
		}
		// Check for the user shooting
		Mouse.poll();
		if(Mouse.isButtonDown(0) && System.currentTimeMillis() - lastShot > delay){
			lastShot = System.currentTimeMillis();
			Vector2f player = screen.getPosition(); // Get the player's location
			Vector2f mouse = screen.getRealMouse(); // Get the mouse's location
			float dx = mouse.getX() - player.getX();
			float dy = mouse.getY() - player.getY();
			double angle = angleFromDirection(dx, dy);
			bullets.add(new Bullet(Color.RED, player.x, player.y, 2.0f, angle));
		}

And here is my bullet class:


package warlord.ingame.world;

import static org.lwjgl.opengl.GL11.*;

import java.awt.Color;
import java.awt.Rectangle;

import warlord.opengl.MathHelper;

public class Bullet {
	
	// Constants
	public static final int SIZE = 10;
	
	private Color color;
	private float x, y;
//	private ArrayList<float[]> oldCoordinates;
	private float velocityX, velocityY;
//	private long ticks;
	private Rectangle region;
	private boolean disposed;
	
	public Bullet(Color color, float x, float y, float velocity, double angle){
		this.color = color;
		this.x = x;
		this.y = y;
//		ticks = 0L;
//		oldCoordinates = new ArrayList<float[]>();
		velocityX = (float) (velocity * MathHelper.cos((float)angle));
		velocityY = (float) (velocity * MathHelper.sin((float)angle));
		disposed = false;
		region = new Rectangle(Math.round(x - 8.0f), Math.round(y - 8.0f), SIZE, SIZE);
	}
	
	public Rectangle getRegion(){
		return region;
	}
	
	public void update(Block block){
		if(disposed){
			return;
		}
		x += velocityX;
		y += velocityY;
		region.x = Math.round(x);
		region.y = Math.round(y);
		if(x < 0 || y < 0 || x > block.getWidthInTiles() * 51.0f || y > block.getHeightInTiles() * 51.0f){
			disposed = true;
		}
	}
	
	public void render(){
		if(disposed){
			return;
		}
		float x_ = x - SIZE, y_ = y - SIZE;
		glColor4f(color.getRed() / 255.0f, color.getGreen() / 255.0f, color.getBlue() / 255.0f, 1.0f);
		glBegin(GL_QUADS);
			glVertex2f(x_, y_);
			glVertex2f(x_ + SIZE, y_);
			glVertex2f(x_ + SIZE, y_ + SIZE);
			glVertex2f(x_, y_ + SIZE);
		glEnd();
		glColor3f(1.0f, 1.0f, 1.0f);
	}
	
}

I hope someone can help me figure out why there is such a steep FPS drop :frowning:

CopyableCougar4

Well first of all, try not to measure performance by FPS, but rather by screen latency (I believe that’s the term, correct me if I’m wrong).
The reason is, after 60 frames per second, it doesn’t matter much. Think about it; 60 FPS vs 200 FPS isn’t a very noticeable difference whereas 30 to 60 is very noticeable. Just food for thought. I have to run so I can’t look at your question thoroughly, but hopefully someone else can :slight_smile:

Hi

As an update, when I made it so that the bullet doesn’t have it’s Rectangle object constructed every frame, that seemed to help a little bit. Now the FPS drops to 100 or so.

Also, in response to Slyth, the only reason why 60fps worries me right now is that there are still a lot of features and effects that I want to add to the game rendering, and 60fps now could mean as low as 30fps in the future.

CopyableCougar4

Hi

So I went back to my code, cleaned up my collision detection system, and the FPS seems to be back up at a steady 200 fps (I capped it at 200). So I think my issue is solved :slight_smile:

CopyableCougar4