0
\$\begingroup\$

I currently use

if(cam.frustum.pointInFrustum(new Vector3((float)posX, (float)posY, 0)))

To check if my sprite is in the camera's view. I have to make a new Vector3 every frame wich is very inefficient.

Is there a better way to do this?

\$\endgroup\$
2
1
\$\begingroup\$

If it's the allocation of the Vector3 you're worried about you might consider keeping an instance as a private member of the class and re-use that;

private final Vector3 v3 = new Vector3();

public void yourMethod() {
  if(cam.frustum.pointInFrustum(v3.set((float)posX, (float)posY, 0))) {
  }
}

Another way, if you have many of these instances you could apply pooling;

private final Pool<Vector3> pool = new Pool();

public void yourMethod() {
  Vector3 v3 = pool.obtain();
  if(cam.frustum.pointInFrustum(v3.set((float)posX, (float)posY, 0))) {
  }
  pool.free(v3);
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .