Skip to content

Instantly share code, notes, and snippets.

@Kwauhn
Kwauhn / Advanced2DCamera.java
Last active August 29, 2015 13:56
Advanced 2D camera with OpenGL and Java
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.Display;
public class Camera
{
private float x, y, scale;
public Camera()
{
x = y = 0;
@Kwauhn
Kwauhn / ImprovedZoomMethod.java
Created February 18, 2014 18:24
Making the 2D camera zoom uniformly regardless of current zoom
public void zoom(float scale)
{
this.scale += scale * this.scale;
}
@Kwauhn
Kwauhn / ImprovedMoveMethod.java
Created February 18, 2014 18:21
Making the 2D camera translate proportionate to the scale
public void move(float x, float y)
{
x /= scale;
y /= scale;
this.x = x;
this.y = y;
}
@Kwauhn
Kwauhn / ImprovedUseMethod.java
Created February 18, 2014 18:16
Making the 2D camera zoom in/out of the center of the display
public void use()
{
glTranslatef(Display.getWidth() / 2, Display.getHeight() / 2, 0);
glScalef(scale, scale, 1);
glTranslatef(x, y, 0);
}
@Kwauhn
Kwauhn / Basic2DCamera.java
Last active June 22, 2020 17:30
Basic 2D Camera with OpenGL and Java
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.Display;
public class Camera
{
private float x, y, scale;
public Camera()
{
x = y = 0;