Skip to content

Instantly share code, notes, and snippets.

View jrenner's full-sized avatar

Jon Renner jrenner

  • Minneapolis, Minnesota, USA
View GitHub Profile
private Vector2 getVelocityCorrectionSteerpoint(Vector2 waypoint) {
if (waypoint == null) {
return null;
}
Vector2 selfVel = body.getLinearVelocity().cpy();
// if this is not a regular waypoint, but instead a point we should chase
// (like a moving enemy target) then we want to correct for the target's velocity as well
if (isChaseTargetPosition(waypoint)) {
// add target's velocity to the waypoint, so we can predict movement
Vector2 targVel = targetEntity.body.getLinearVelocity();
void seekWaypoint(Vector2 waypoint) {
Vector2 pos = body.getPosition();
float dist = waypoint.dst(pos);
// you should set a threshold for arrival distance
// that is good enough to be considered "arrived" for your purposes
if (dist < arrivalThreshold) {
if (waypoints.contains(waypoint)) {
// if we are chasing a target, don't remove the waypoint upon reaching arrival distance
waypoints.remove(0);
waypoint = getCurrentWaypoint();
void steerToDesiredFacing() {
if (desiredFacing == null) {
return;
}
float facing = getForwardFacing();
// this contrain method keeps the angle between 180 and -180
float diff = constrainAngle180(facing - desiredFacing);
// steer within n degrees of desired facing, use this to control precision
float angularThreshold = 1;
float angularVel = body.getAngularVelocity();
public static float constrainAngle180(float angle) {
while (angle > 180) {
angle = angle - 360;
}
while (angle < -180) {
angle = angle + 360;
}
return angle;
}
@jrenner
jrenner / Desktop.java
Created September 10, 2013 14:04
a desktop launching class for libgdx with convenient screen size options, good for debugging
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
public class DesktopStarter {
private static final short FULLSCREEN = 0;
private static final short GALAXY_S3 = 1;
private static final short NEXUS_ONE = 2;
private static final short EVO3D = 3;
private static final short BIG_WINDOW = 4;
private static final short SMALL_PHONE = 5;
@jrenner
jrenner / box2dproper.java
Created November 14, 2013 02:18
box2d working properly
package com.example.libgdx.skeleton;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
float frameTime = dt;
if (frameTime > 0.5f) {
frameTime = 0.5f;
}
accumulatedDT += frameTime;
while (accumulatedDT >= Physics.TIME_STEP) {
frame++;
tick();
Physics.runPhysics();
//System.out.printf("ran physics - accum: %.3f\n", accumulatedDT);
@jrenner
jrenner / headless-test-libgdx
Created December 14, 2013 01:57
LwjglHeadlessApplication test server
package com.example.libgdx.skeleton;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Net;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.net.ServerSocket;
import com.badlogic.gdx.net.Socket;
import com.badlogic.gdx.physics.box2d.Body;
float frameTime = dt;
if (frameTime > 0.5f) {
frameTime = 0.5f;
}
accumulatedDT += frameTime;
while (accumulatedDT >= Physics.TIME_STEP) {
frame++;
tick();
Physics.runPhysics();
//System.out.printf("ran physics - accum: %.3f\n", accumulatedDT);
fun main(args: Array<String>) {
val arr = array("cats", "dogs")
for (i in 0..10) {
println(arr.random())
}
// this version works fine
fun <T> Array<T>.random() : T {
val rand = Random()
return this.get(rand.nextInt(this.size))