Hey folks,
I'm trying to implement movement of game-controlled opponents (mobs) in my project. Specifically, I'm trying to calculate the position (Vector3f in the game world) I should move to, given…
- my present position
- the position of my "target" (player)
- a rate of speed
- a # of milliseconds since I last moved
I discovered the LineSegment class. It's not documented, but it looks like it should do what I need, but it's not giving me results that match expectations. Here's the code that does the calculation…
Vector3f originLoc = new Vector3f(origin.get(WorldEntity.LOCATION_X),
origin.get(WorldEntity.LOCATION_Y),
origin.get(WorldEntity.LOCATION_Z));
Vector3f targetLoc = new Vector3f(target.get(WorldEntity.LOCATION_X),
target.get(WorldEntity.LOCATION_Y),
target.get(WorldEntity.LOCATION_Z));
float speedInGameUnits = scaleManager.toGameUnits(origin.get(Martial.SPEED));
float cycleFactor = Long.valueOf(cycleTime).floatValue() / Long.valueOf(1000L).floatValue();
float distanceTraveled = cycleFactor * speedInGameUnits;
LineSegment ls = new LineSegment(originLoc, targetLoc, distanceTraveled);
Vector3f newLoc = ls.getPositiveEnd(null);
if (log.isTraceEnabled()) {
log.trace("originLoc=" + originLoc.toString());
log.trace("tergetLoc=" + targetLoc.toString());
log.trace("newLoc=" + newLoc.toString());
}
I left the TRACE logging in there because now I'm going to paste a snippet of the log...
TRACE [AI Invoker] ai.BasicAi.[] May/26 15:28:13 - originLoc=com.jme.math.Vector3f [X=106.0, Y=15.757242, Z=100.0]
TRACE [AI Invoker] ai.BasicAi.[] May/26 15:28:13 - tergetLoc=com.jme.math.Vector3f [X=87.92792, Y=15.819277, Z=90.35227]
TRACE [AI Invoker] ai.BasicAi.[] May/26 15:28:13 - newLoc=com.jme.math.Vector3f [X=297.68286, Y=50.243263, Z=296.96796]
TRACE [AI Invoker] ai.BasicAi.[] May/26 15:28:13 - originLoc=com.jme.math.Vector3f [X=297.68286, Y=15.222096, Z=296.96796]
TRACE [AI Invoker] ai.BasicAi.[] May/26 15:28:13 - tergetLoc=com.jme.math.Vector3f [X=85.961754, Y=15.821485, Z=89.49793]
TRACE [AI Invoker] ai.BasicAi.[] May/26 15:28:13 - newLoc=com.jme.math.Vector3f [X=486.7987, Y=50.02936, Z=493.8634]
These are the log statements from the first 2 "passes" through the movement code. You can see that the 'newLoc' value is really, really far away from the 'originLoc' and 'targetLoc' positions.
What I had expected was for 'newLoc' to be between 'originLoc' and 'targetLoc', and exactly 'distanceTraveled' (3rd argument to LineSegment constructor) away from 'originLoc'.
Is this class working as intended? Is there a better way to make this calculation?