Important facts about commercial licenses

  • Licenses are perpetual. They do not expire and do not need to be renewed.
  • Licenses can be upgraded. You can upgrade to a more expensive license later paying only the difference in cost.
  • Pay attention to the distribution type - Hosted (sites / servers), binary (applications) or source (includes all the others). Choose according to your needs (more below).
  • All licenses allow commercial use unless otherwise indicated.
  • Read the full license by clicking on the icon.
  • Read more about licenses in our handy license guide.
Free

Personal License

1 application Binary restricted distribution Non-commercial use only Attribution required
$19

Developer License

Unlimited projects Source and binary distribution 1 year support
You need to log-in or create an account
  • Create an account
  • Log-in
  • Please use your real name.
  • Account activation link will be sent to this address.
  • Minimum 8 characters

Clicking this button confirms you read and agreed to the terms of use and privacy policy.

  • Released: May 26, 2011
    Last Update: May 25, 2011
  • Language: Java
  • Category: Games & Entertainment
  • Time / costs savings: 60h / $3600 *

MovePath

MovePath
Developed by Ralph Chapin, Released May 26, 2011

Open source code to assign and track object movement in up to three dimensions.

Java

Tags: 2d , 3d , code , game

MovePath tracks movement as a series of line segments in three dimensions. It creates and edits the path, and will give the location and facing of an object traveling the path at any requested time. It is intended for serious multi-threaded use, allowing parallel updates and queries with a minimum of blocking. Editing functions include the current time as a parameter to prevent path changes from affecting movement already made. Determining locations with MovePath rather than periodically updating a fixed location provides smoother moves along with less frequent location updates.

MovePath is thread-safe and fully Javadoc'ed. The source code includes a simple example for specifying a path and a second that also demonstrates stopping, removing path segments, and jogging around obstacles.

Back to top

Usage Example

package rrc12;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import rrc12.arena.*;
import rrc12.util.*;

/** A sample program to demonstrate MovePath.  It displays a window
 * with a vehicle on it.  The vehicle can be made to move by clicking
 * at points on the window. */
public class MovePathExample  extends JFrame  {

    private static final Color  GREEN = new Color( 16, 128, 0 );

    private MovePath  movePath;

    /** Creates a MovePathExample window and displays it. */
    public static void main( String[] args )  {
        new MovePathExample().setVisible( true );
    }

    public MovePathExample()  {
        movePath = new MovePath();
        movePath.moveTo( 0L, new Location( 200, 200, 0 ), 0.05 );

        setBounds( 100, 100, 400, 400 );
        setDefaultCloseOperation( EXIT_ON_CLOSE );
        final Container  cp = new JPanel()  {
            public void paintComponent( Graphics g )  {
                Graphics2D  g2 = (Graphics2D)  g;
                g.setColor( GREEN );
                g.fillRect( 0, 0, 1000, 1000 );
                BasicStroke  stroke = new BasicStroke( 4.0f,
                            BasicStroke.CAP_ROUND,
                            BasicStroke.JOIN_ROUND );
                Line2D.Double  line = new Line2D.Double();
                g2.setStroke( stroke );
                long  time = System.currentTimeMillis();
                Location  loc = movePath.getLocation( time );
                plotPath:  {
                    Location[]  moves =
                                movePath.getMovementArray( time );
                    if (moves == null  ||  moves.length < 1)
                        break plotPath;
                    Location  loc1 = loc;
                    stroke = new BasicStroke( 3.0f,
                                BasicStroke.CAP_ROUND,
                                BasicStroke.JOIN_ROUND );
                    g2.setStroke( stroke );
                    g.setColor( Color.YELLOW );
                    int  max = moves.length - 1;
                    for (int i = 0;; i++)  {
                        Location  loc2 = moves[i];
                        line.setLine( loc1.x, loc1.y, loc2.x, loc2.y );
                        g2.draw( line );
                        if (i >= max)  break;
                        loc1 = loc2;
                    }
                }
                g.setColor( Color.RED );
                Ellipse2D  vehicle = new Ellipse2D.Double( loc.x - 5,
                            loc.y - 5, 10, 10 );
                g2.fill( vehicle );
                g.setColor( Color.BLACK );
                g.drawString( "Click on destinations.", 120, 300 );
            }
        };
        setContentPane( cp );
        // Listen to the Mouse.
        MouseListener  mouseListener = new MouseListener()  {
            private boolean  anyClick = false;

            public void mouseClicked( MouseEvent e )  {}
            public void mousePressed( MouseEvent e )  {
                anyClick = false;
                if (e.isConsumed())  return;
                anyClick = true;
                e.consume();
            }
            public void mouseReleased( MouseEvent e )  {
                if (!anyClick)  return;
                anyClick = false;
                int  x = e.getX();
                int  y = e.getY();
                Location  temp = new Location( x, y, 0 );
                movePath.moveTo( System.currentTimeMillis(), temp,
                            0.05 );
                cp.repaint();
            }
            public void mouseEntered( MouseEvent e )  {}
            public void mouseExited( MouseEvent e )  {
                anyClick = false;
            }
        };
        cp.addMouseListener( mouseListener );
        // Repaint regularly.
        java.util.Timer  timer = new java.util.Timer( true );
        timer.schedule( new TimerTask()  {
            public void run()  { cp.repaint(); }
        }, 50L, 50L );
    }
}
Back to top

Javadoc for MovePath (extends MoveLocation for editing) in plain text

rrc12.arena Class MovePath

java.lang.Object rrc12.arena.MoveLocation rrc12.arena.MovePath

All Implemented Interfaces: Travel

public class MovePath extends MoveLocation

A path along which something is moving. Among other things, an instance of this class will give a location for a given time. Extends MoveLocation to allow thread-safe editing. Objects of this class are thread-safe.

Methods inherited from class rrc12.arena.MoveLocation

copy, copy, getDestination, getDistanceBack, getEnd, getEndTime, getFacing, getLocation, getMovement, getMovementArray, hasCornered, hasMoved, isMoving, reader, willMove, write

Method Detail

setSpeed()

public void setSpeed(long time,
                     double speed)

Keeps the same path, but resets the speed after the specified time.

Parameters:

  • time - the time the change in speed is to occur.
  • speed - the new speed that is to affect movement after the specified time.

jog()

public void jog(long time,
                Location loc,
                double speed)

Inserts a move, or jog, at the current time while keeping the same path ending location. If the unit is not moving at the passed time, it will move to the passed location. Specifically, if the time is on a segment from point C to point D, that segment will be replaced by segments from C to T, T to X, and X to D, where T is the point at the passed time, and X is the passed location.

Parameters:

  • time - the time the jog is to occur.
  • loc - the point to which to jog.
  • speed - the speed for the jog. Should be set to maximum.

removeHistory()

public void removeHistory(int moves,
                          long time)

Removes old moves. The specified number of moves will always be retained, no matter how long ago they occurred. No moves will be removed, no matter how many they may be, if they finish at or after the specified time.

Parameters:

  • moves - the number of moves to retain. Values less than 3 are treated as if they were 3.
  • time - the time before which moves may be removed.

setFacing()

public boolean setFacing(short facing,
                         long time)

Sets the facing if the current path terminates before the specified time.

Parameters:

  • facing - the new facing. In degrees, clockwise, where 3 o'clock is zero. In the range of zero through 359.
  • time - the time of the facing change.

Returns: true if the facing was changed, false if this MovePath remains unchanged.

removeOne()

public boolean removeOne(long time)

Removes one MovePath segment from the movement to the extent that that can be done without undoing movement before the specified time. This method may just shorten the last segment so it ends at the specified time. Parameters:

  • time - the current time, before which no movement can be undone.

Returns: true if something was done, false if there was no future movement to start with and nothing was done.

stop()

public boolean stop(long time)

Ends the MovePath at the specified time. All movement after that is removed. If the passed time is in the middle of a segment, that segment will be truncated.

Parameters:

  • time - the time to stop the movement.

Returns: returns true if this method had any effect. Returns false if the path was already ended at or before the specified time.

moveTo()

public void moveTo(long time,
                   Location loc,
                   double speed)

Causes the path to be extended to the given location. The existing path remains unaffected. It will often be desirable to call stop before calling this method. If there is no path, this method will merely start one at the passed time and point. (This will mean the object with this path will appear at the end point at the passed starting time.)

Parameters:

  • time - the time to start the movement. Unused unless it is after the path's current finish time. A valid time should always be passed to avoid the possibility of starting movement before the present.
  • loc - the location to which to move.
  • speed - the speed with which to move to this location.

read() public final void read(java.io.DataInput in) throws java.io.IOException

Reads data into this MovePath via a DataInput instance. Reads the data written by write.

Overrides: read in class MoveLocation

Parameters:

  • in - the DataInput instance.

hasChanged()

public boolean hasChanged()

Returns true if this MovePath has changed since the last call to this method. Use this method with care, as multi-threading and recursion can render its return meaningless.

Overrides: hasChanged in class MoveLocation

Back to top

Javadoc for MoveLocation in plain text:

rrc12.arena Class MoveLocation

java.lang.Object rrc12.arena.MoveLocation

All Implemented Interfaces: Travel

Direct Known Subclasses: MovePath

public class MoveLocation extends java.lang.Object implements Travel

A path along which something is moving. An instance of this class will give a location for a given time. Objects of this class are pretty much immutable and hence thread-safe. Specifically, the only way to change a MoveLocation is to read data into it from a stream.

MoveLocation objects are obtained primarily by using the copy method in a subclass that does allow editing (MovePath).

Method Detail

getDestination()

public Location getDestination(long time)

Returns the next point on the path after the specified time. This would be a segment end point.

Parameters:

  • time - the time when the next point on the path is wanted.

Returns: the location as an independent object which may be freely modified and/or saved. May be null.

read() public void read(java.io.DataInput in) throws java.io.IOException

Reads data into this MoveLocation via a DataInput instance. Reads the data written by write. This is the one time the data in a MoveLocation may change. This change is not absolutely thread-safe, so use caution. Retrieving data while this method is called may give inconsistent results.

Parameters:

  • in - the DataInput instance.

write()

public final void write(java.io.DataOutput out)
                 throws java.io.IOException

Writes the data in this MoveLocation to a DataOutput instance.

Parameters:

  • out - the DataOutput instance.

getEndTime()

public long getEndTime()

Returns the finish time for this move.

hasChanged()

public boolean hasChanged()

Returns false. An unextended MoveLocation is immutable.

getLocation()

public Location getLocation(long time)

Returns the location at the specified time.

Parameters:

  • time - the time at which the location is desired.

Returns: the location as an independent object which may be freely modified and/or saved. May be null.

getEnd()

public Location getEnd()

Returns the end point of the path, with no reference to time. This point may have been reached in the distant past, or may not be reached until the distant future.

Returns: the location. May be null if there is no position at all.

getDistanceBack()

public Location getDistanceBack(long time,
                                double distance)

Returns the location that is the specified distance from this MoveLocation's position at the specified time. Used to help one moving object follow another.

Parameters:

  • time - the time at which this MoveLocation's location is determined.
  • distance - the distance back along this MoveLocation from the current position whose location is to be returned.

Returns: the position back along the path. An independent Location instance. May be null if the path is not defined that far back.

copy()

public Travel copy()

Returns a deep copy of this MoveLocation.

Returns: a fully independent MoveLocation instance that will not be subject to random changes and that may be freely modified if it is of a subclass than can be modified.

copy()

public Travel copy(long start,
               long end)

Returns a deep copy of this MoveLocation over a specified time frame. Intended to produce a Travel instance whose purpose is to report the current location (for a span of seconds) rather than to examine the whole of what may be an extensive movement. The time cutoffs, both start and end, will be thorough and precise because the returned information may well go to enemy players.

Parameters:

  • start - the start of the time span in game time.
  • end - the end of the time span.

Returns: a fully independent MoveLocation instance that will not be subject to random changes and that may be freely modified.

hasCornered

public boolean hasCornered(long begin,
                       long end)

Returns true if this MoveLocation goes around any corners over the passed time span and false if it does not. If it has, this indicates that a path segment has been dropped from future movement as returned by getMovementArray.

Parameters:

  • begin - the start of the time span.
  • end - the end of the time span.

hasMoved()

public boolean hasMoved(long begin, long end)

Returns true if this MoveLocation indicates any movement over the passed time span and false if it does not.

Parameters:

  • begin - the start of the time span.
  • end - the end of the time span.

isMoving()

public boolean isMoving(long time)

Returns true if this MoveLocation indicates any movement at the passed time and false if it does not.

Parameters:

  • time - the instant in time at which the question is asked.

willMove()

public boolean willMove(long time)

Returns true if this MoveLocation indicates any movement after the passed time and false if all movement has stopped at or before the passed time.

getFacing()

public short getFacing(long time)

Returns the facing at the specified time. It will be the direction of current movement if there is any and of last movement if there is none. If there never was any movement, but future movement is planned, the direction of the first future move will be returned.

Returns: facing in integral degrees, with values between zero and 359 inclusive. Zero degrees is the positive X direction. The angle is measured clockwise. A -1 indicates that there never was any movement to provide a facing.

getMovementArray()

public Location[] getMovementArray(long time)

Returns the moves to be made after the specified time as an array of points, starting with the first point after the specfied time, then the next point, and so on, into the future. Speeds and times are not taken into account. Points directly on the path to the next point (presumably entered to mark differing speeds) will not be included.

Parameters: * time - the point in time the list of points should start.

Returns: an array of points to be moved through. Will be null if there is no movement after the specified time.

User Reviews

No reviews have been submitted yet.

Questions & Comments


Or enter your name and Email
No comments have been posted yet.
You must be logged-in to vote. Log-in to your account or register now.