This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static float constrainAngle180(float angle) { | |
while (angle > 180) { | |
angle = angle - 360; | |
} | |
while (angle < -180) { | |
angle = angle + 360; | |
} | |
return angle; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |