Hello All,
This is my first post, so forgive me if this isn't the proper place for questions. I have far too much code to post it all so I will be mostly giving pseudo-code of the relevant parts, but this is mainly a theoretical/how do I question so code may not be needed at all.
I'm working on a game with cameras that are scanning for the player, I've implemented the field of view of the camera as a sensor attached to a Kinematic body. When the Sensor is colliding with the player I'm using a World.raycast() from the camera to the player to determine if there is Line of sight. This happens every frame that the player is in the FoV of the camera. When the player is in the FoV of a camera there is a slight drop in frame rate, which gets worse the more cameras the player is within view of.
My question is if it's safe to spawn a new thread to do the cast on the world, or add a requested cast to a thread safe queue to allow worker-threads to cast?
The JBox2D documentation doesn't specify any thread safety, so my assumption is that it is not safe to do.
What would be the best way to:
1) reduce the number of ray casts per frame (simply skipping some frames would probably be fine, but I don't want the player to be able to get through the FoV of the camera before a ray cast occurs)
2) speed up the ray casts or
3) offload the ray casts so they won't lower my frame rate.
Using JBox2D 2.3.0.
If you need code I can post some later, right now my game loop looks something like this (pseudo-code):
This is my first post, so forgive me if this isn't the proper place for questions. I have far too much code to post it all so I will be mostly giving pseudo-code of the relevant parts, but this is mainly a theoretical/how do I question so code may not be needed at all.
I'm working on a game with cameras that are scanning for the player, I've implemented the field of view of the camera as a sensor attached to a Kinematic body. When the Sensor is colliding with the player I'm using a World.raycast() from the camera to the player to determine if there is Line of sight. This happens every frame that the player is in the FoV of the camera. When the player is in the FoV of a camera there is a slight drop in frame rate, which gets worse the more cameras the player is within view of.
My question is if it's safe to spawn a new thread to do the cast on the world, or add a requested cast to a thread safe queue to allow worker-threads to cast?
The JBox2D documentation doesn't specify any thread safety, so my assumption is that it is not safe to do.
What would be the best way to:
1) reduce the number of ray casts per frame (simply skipping some frames would probably be fine, but I don't want the player to be able to get through the FoV of the camera before a ray cast occurs)
2) speed up the ray casts or
3) offload the ray casts so they won't lower my frame rate.
Using JBox2D 2.3.0.
If you need code I can post some later, right now my game loop looks something like this (pseudo-code):
Code: Select all
class GameWorld extends JPanel implements Runnable, (various listeners) {
World phys; //physics world
boolean running;
BufferedImage backBuf;
///lots of other stuff
public GameWorld(){
//init everything
}
public void start(){running=true; new Thread(this).start();}
public void stop(){running=false;}
@Override
public void run(){
while(running){
step();
draw();
}
}
private void step(){
world.step(1/60f);
///update all game entities
///camera entities perform the ray casts when they are stepped
//remove dead entities
}
private void draw(){
Graphics g = backBuff.getGraphics();
//draw everything
g.dispose();
Graphics g2 = getGraphics();
g2.drawImage(backBuf,0,0);
}
/////////Listener methods and probably others.
public static void main(String[] args){
GameWorld world=new GameWorld();
JFrame frame = new JFrame();
frame.add(world);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
world.start();
}
}