Skip to content

Instantly share code, notes, and snippets.

View jrenner's full-sized avatar

Jon Renner jrenner

  • Minneapolis, Minnesota, USA
View GitHub Profile
public static float constrainAngle180(float angle) {
while (angle > 180) {
angle = angle - 360;
}
while (angle < -180) {
angle = angle + 360;
}
return angle;
}
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();
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();
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();
@jrenner
jrenner / sigsegv.txt
Created May 5, 2013 07:52
libgdx box2d block allocator SIGSEGV
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f7e0ad30504, pid=10789, tid=140178498938624
#
# JRE version: 7.0_21-b11
# Java VM: Java HotSpot(TM) 64-Bit Server VM (23.21-b01 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C [libgdx64.so+0x23504] b2BlockAllocator::Allocate(int)+0x54
#
@jrenner
jrenner / box2d_air_resistance.java
Last active January 14, 2022 04:30
box2d air resistance
float dragForce;
Vector2 appliedDrag = new Vector2();
float dragAngle;
float p, A, Cd, v; // elements of the formula see wikipedia entry for Drag (physics)
/*
ρ is the density of the fluid,
v is the speed of the object relative to the fluid,
A is the cross-sectional area
Cd is the drag coefficient – a dimensionless number.
sample drag co-efficients (http://en.wikipedia.org/wiki/Drag_coefficient)
#!/usr/bin/python
# test memory info from a glances server
import xmlrpclib
import sys
if len(sys.argv) < 2:
print "Please give 'host:port' as argument"
sys.exit()
host = sys.argv[1]
print "Getting data from " + host
@jrenner
jrenner / autounit_test.py
Last active December 15, 2015 03:29
A script for testing glances' autoUnit function
#!/usr/bin/python
__author__ = 'jrenner'
import random
import sys
"""
This script is useful for testing the autoUnit function. It is not guaranteed
to be the same as in glances.py. You may need to copy and paste the exact
code you want to test from glances.py
@jrenner
jrenner / unsafe.go
Created September 21, 2012 04:14
Can't convert unsafe.Pointer to int
func getPixel(surface *sdl.Surface, x, y int) uint32 {
var bpp uint8 = surface.Format.BytesPerPixel
/* Here p is the address to the pixel we want to retrieve */
var p *uint32 = *uint32(int(surface.Pixels) + y*int(surface.Pitch) + x*bpp)
return *p
return 1 /* shouldn't happen, but avoids warnings */
}
@jrenner
jrenner / countchars.c
Created August 19, 2012 12:42
count number of chars in a string and display by rank
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int** count_chars(char *s)
{
int **counts = (int**)malloc(256 * sizeof(int) * sizeof(char));
int i;
for(i = 0; i < 256; i++)
{