Search code examples
javaorientationsimulationdirectionangle

Java how to calculate the angle between an entity's current direction and it's target destination


I am developing a Java program that will transmit entity state PDUs over a network for use in a networked simulation. I am using the open-DIS library to help with doing this.

Currently, when I run my program, I have a number of entities running on one simulation, and want to display them in the other simulation by having the second simulation receive the PDUs that are sent by the first simulation, and represent the information that they contain graphically.

As it stands, I am able to see all of the entities from the first simulation in the second simulation, but currently when any of the entities in the first simulation move, although I see those same entities move in the direction that they should in the second simulation, they stay facing the same direction as they originally were... i.e. they don't turn to face the direction in which they are now moving.

I know that the open-DIS library has an 'Orientation' class, with setPhi, setPsi & setTheta methods which is what I need to set the direction/ angle in which they should be facing, but what I'm not sure about is how I work out what values to give those methods... Is there a library available that provides the mathematic methods for getting these values, or is this something that I will have to write myself?

EDIT

I've tried adding the following lines to my EntitStateFormatter.java class (this is the class that creates the PDUs that are to be sent on to the second simulation, having obtained the data in the PDUs that were received from the first simulation):

orientation.setPhi((float) (entity.getLocation().getAzimuth()));
orientation.setPsi((float) (entity.getLocation().getElevation()));
orientation.setTheta((float) (entity.getLocation().getTilt()));

'orientation' is an 'Orientation' variable (edu.nps.moves.dis.Orientation), which I have created with the line:

Orientation orientation = new Orientation();

'entity' is an 'Entity' variable (Entity being another class in my program), which I have passed as a parameter for the method using the line:

public static EntityStatePdu createPdu(Entity entity){...

But for some reason, when running my program, although I can see the entities that have been created in the first simulation displayed in the second simulation, when moving across the terrain (as commanded to on the first simulation), in the second one, they do not turn to face the direction in which they are moving, they just move in that direction (i.e. vehicles move sideways, etc).

When running my program, I have a console window open that is displaying some debug- every now and again, that throws out a message that says:

INTERNAL ERROR: java.lang.ArrayIndexOutOfBoundsException: 154

I have no idea why this is- particularly since I don't actually have any arrays in the EntityStateFormatter.java class...

Anyone have any ideas? Thanks in advance!


Solution

  • The phi/psi/theta are the Euler angles. For now, you should be able to set the 2 or 3 angles you use for each of your entities (depends if you need roll or not) in the orientation part of the PDU, then on the receiving end, use the associated getters, without regard for correctness of your own angles with those of DIS (this only matters when your PDU are used by third-party simulation components, but you should first work out your internal data exchange). For example (pseudocode),

    // sender: 
    pdu.orientation.setPhi(entity.heading)
    pdu.orientation.setPsi(entity.pitch)
    pdu.orientation.setTheta(entity.roll)
    
    // receiver: 
    entity.heading = pdu.orientation.getPhi()
    entity.pitch = pdu.orientation.getPsi()
    entity.roll = pdu.orientation.getTheta()
    

    Once you show that that above works, you can figure out how to convert your angles to phi, psi and theta. Look at the Euler Angles page on wikipedia, it contains many conversion formula, all you need to determine is which one of the discussed orientation systems are you using for your entities.