예제 #1
0
파일: MotionSim.cs 프로젝트: wshanshan/DDD
        private void MoveObject(SimulationEvent e)
        {
            //AD: Inserted a return here as all motion logic lives in the DIS adapter now.
            return;


            //TODO: adjust heading (current, expected, delta)
            //use radius math to calculate turn radius

            //objectProxies = bbClient.GetObjectProxies();
            DataValue dv = null;
            string id = ((StringValue)e["ObjectID"]).value;
            SimulationObjectProxy obProx = objectProxies[id];

            double throttle = 0;
            double maxSpeed = 0;
            double speedMultiplier = 0;

            Vec3D curVec = new Vec3D(0, 0, 0);
            Vec3D destVec = new Vec3D(0, 0, 0);
            Vec3D velVec = new Vec3D(0, 0, 0);

            dv = obProx["Location"].GetDataValue();
            curVec.Set((LocationValue)dv);


            dv = obProx["MaximumSpeed"].GetDataValue();
            maxSpeed = ((DoubleValue)dv).value;

            dv = e["DestinationLocation"];
            destVec.Set((LocationValue)dv);

            obProx["DestinationLocation"].SetDataValue(dv);

            dv = e["Throttle"];
            throttle = ((DoubleValue)dv).value;
            obProx["Throttle"].SetDataValue(dv);

            dv = obProx["ActiveRegionSpeedMultiplier"].GetDataValue();
            speedMultiplier = ((DoubleValue)dv).value;

            velVec = curVec.VectorDistanceTo(destVec);
            velVec.Normalize();
            velVec = velVec.Multiply(maxSpeed * throttle * speedMultiplier);

            obProx["Velocity"].SetDataValue(velVec.ToVelocityValue());

           // System.Console.WriteLine("Object {0} Velocity={1}", id.ToString(), velVec.ToString());
        }
예제 #2
0
파일: MotionSim.cs 프로젝트: wshanshan/DDD
        /*
        Vec3D FindLandBoundaryIntersection(Vec3D fromP, Vec3D toP)
        {
            Vec2D r = null;

            Vec2D myFromP = new Vec2D(fromP.X, fromP.Y);
            Vec2D myToP = new Vec2D(toP.X, toP.Y);

            List<Vec2D> points = new List<Vec2D>();
            foreach (LandRegion reg in landRegions.Values)
            {
                r = Polygon2D.FindIntersect(reg.poly, myFromP, myToP);
                if (r != null)
                {
                    points.Add(r);
                }
            }

            if (points.Count == 0)
            {
                return null;
            }
            Vec2D closest = new Vec2D(points[0]);

            foreach (Vec2D p in points)
            {
                if (myFromP.ScalerDistanceTo(p) < myFromP.ScalerDistanceTo(closest))
                {
                    closest.Set(p);
                }
            }

            // return point on from side of intersection.
            if (myFromP.X < closest.X)
            {
                closest.X -= 1;
            }
            else if (myFromP.X > closest.X)
            {
                closest.X += 1;
            }
            if (myFromP.Y < closest.Y)
            {
                closest.Y -= 1;
            }
            else if (myFromP.Y > closest.Y)
            {
                closest.Y += 1;
            }

            return new Vec3D(closest.X, closest.Y, fromP.Z);
        }
        */
        /*
        Vec3D FindObstructionBoundaryIntersection(Vec3D fromP, Vec3D toP)
        {
            Vec3D r = null;

            List<Vec3D> points = new List<Vec3D>();
            foreach (BlockingRegion reg in blockingRegions.Values)
            {
                r = Polygon3D.FindIntersect(reg.poly, fromP, toP);
                if (r != null)
                {
                    points.Add(r);
                }
            }

            if (points.Count == 0)
            {
                return null;
            }
            Vec3D closest = new Vec3D(points[0]);

            foreach (Vec3D p in points)
            {
                if (fromP.ScalerDistanceTo(p) < fromP.ScalerDistanceTo(closest))
                {
                    closest.Set(p);
                }
            }

            // return point on from side of intersection.
            if (fromP.X < closest.X)
            {
                closest.X -= 1;
            }
            else if (fromP.X > closest.X)
            {
                closest.X += 1;
            }
            if (fromP.Y < closest.Y)
            {
                closest.Y -= 1;
            }
            else if (fromP.Y > closest.Y)
            {
                closest.Y += 1;
            }

            return closest;
        }
        */
        private void TimeTick(SimulationEvent e)
        {
            int oldTime = time;
            DataValue dv = null;

            Vec3D landIntersect;
            Vec3D obstructionIntersect;

            Vec3D curLocVec = new Vec3D(0, 0, 0);
            Vec3D newLocVec = new Vec3D(0, 0, 0);
            Vec3D destVec = new Vec3D(0, 0, 0);
            Vec3D velVec = new Vec3D(0, 0, 0);

            dv = e["Time"];
            time = ((IntegerValue)dv).value;

            double dTime = ((double)(time - oldTime)) / 1000;
            SimulationObjectProxy obProx = null;
            SimulationObjectProxy parentProx = null;

            bool launchStarted, launchDone;
            int launchEndTime;

            bool pursueStarted;
            string pursueTargetID;
            string parentObjectID;

            ScoringDB.ActorFrame actorFrame = new ScoringDB.ActorFrame();
            foreach (string id in objectProxies.Keys)
            {
                obProx = objectProxies[id];


                //*********************************
                // Launch movement logic 
                //*********************************

                launchStarted = ((BooleanValue)obProx["LaunchStarted"].GetDataValue()).value;
                launchDone = ((BooleanValue)obProx["LaunchDone"].GetDataValue()).value;

                pursueStarted = ((BooleanValue)obProx["PursueStarted"].GetDataValue()).value;
                pursueTargetID = ((StringValue)obProx["PursueTargetID"].GetDataValue()).value;

                if (launchStarted == false && launchDone == true)
                {
                    dv = obProx["LaunchDone"].GetDataValue();
                    ((BooleanValue)dv).value = false;
                    obProx["LaunchDone"].SetDataValue(dv);
                    launchDone = false;
                }
                if (launchStarted == true && launchDone == false)
                {
                    parentObjectID = ((StringValue)obProx["ParentObjectID"].GetDataValue()).value;
                    parentProx = objectProxies[parentObjectID];
                    launchEndTime = ((IntegerValue)obProx["LaunchEndTime"].GetDataValue()).value;
                    if (time >= launchEndTime)
                    {
                        Vec3D parentVec = new Vec3D((LocationValue)parentProx["Location"].GetDataValue());
                        dv = obProx["LaunchDestinationLocation"].GetDataValue();
                        Vec3D launchDestVec = new Vec3D((LocationValue)dv);
                        bool sendMoveRequest = ((LocationValue)dv).exists;

                        obProx["Location"].SetDataValue(parentVec.ToLocationValue());
                        //Now that location has been updated, set in helper library:
                        ObjectDistances.UpdateObjectLocation(id, parentVec);

                        dv = obProx["LaunchDone"].GetDataValue();
                        ((BooleanValue)dv).value = true;
                        obProx["LaunchDone"].SetDataValue(dv);

                        if (sendMoveRequest)
                        {
                            string userID = ((StringValue)obProx["OwnerID"].GetDataValue()).value;
                            distClient.PutEvent(SimUtility.BuildMoveObjectRequestEvent(ref simModel, time, userID, id, launchDestVec, 1.0));
                        }

                        if (((BooleanValue)obProx["LaunchIsWeapon"].GetDataValue()).value == true)
                        {
                            dv = obProx["PursueStarted"].GetDataValue();
                            ((BooleanValue)dv).value = true;
                            obProx["PursueStarted"].SetDataValue(dv);
                            pursueStarted = true;

                            dv = obProx["LaunchWeaponTargetID"].GetDataValue();
                            pursueTargetID = ((StringValue)dv).value;

                            dv = obProx["PursueTargetID"].GetDataValue();
                            ((StringValue)dv).value = pursueTargetID;
                            obProx["PursueTargetID"].SetDataValue(dv);

                            distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                                           time,
                                                           ((StringValue)(obProx["OwnerID"].GetDataValue())).value,
                                                           id + " has been launched from " + parentObjectID + " at " + pursueTargetID));
                            distClient.PutEvent(SimUtility.BuildHistory_SubplatformLaunchEvent(ref simModel,
                                                                                               time,
                                                                                               id,
                                                                                               parentObjectID,
                                                                                               parentVec,
                                                                                               launchDestVec,
                                                                                               true,
                                                                                               pursueTargetID));
                        }
                        else
                        {
                            distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                                           time,
                                                           ((StringValue)(obProx["OwnerID"].GetDataValue())).value,
                                                           id + " has been undocked from " + parentObjectID));
                            distClient.PutEvent(SimUtility.BuildHistory_SubplatformLaunchEvent(ref simModel,
                                                                                               time,
                                                                                               id,
                                                                                               parentObjectID,
                                                                                               parentVec,
                                                                                               launchDestVec,
                                                                                               false,
                                                                                               ""));
                        }
                    }
                }

                if (pursueStarted)
                {
                    if (objectProxies.ContainsKey(pursueTargetID))
                    {
                        dv = objectProxies[pursueTargetID]["Location"].GetDataValue();

                        String targetState = ((StringValue)objectProxies[pursueTargetID]["State"].GetDataValue()).value;
                        if (!((LocationValue)dv).exists || targetState == "Dead")
                        {
                            //if my target is dead or not existant, kill this weapon too.

                            obProx["PursueStarted"].SetDataValue(DataValueFactory.BuildBoolean(false));
                            obProx["PursueTargetID"].SetDataValue(DataValueFactory.BuildString(String.Empty));
                            distClient.PutEvent(SimUtility.BuildStateChangeEvent(ref simModel, time, id, "Dead"));
                            continue;
                        }

                        destVec.Set((LocationValue)dv);
                        obProx["DestinationLocation"].SetDataValue(destVec.ToLocationValue());

                        dv = obProx["Throttle"].GetDataValue();
                        ((DoubleValue)dv).value = 1;
                        obProx["Throttle"].SetDataValue(dv);

                        distClient.PutEvent(SimUtility.BuildHistory_PursueEvent(ref simModel,
                                                                                time,
                                                                                id,
                                                                                curLocVec,
                                                                                pursueTargetID,
                                                                                destVec));
                    }
                }


                //*********************************
                //   MoveObject logic
                //*********************************

                //
                // Ignore objects that don't have a location
                //
                dv = obProx["Location"].GetDataValue();
                if (!((LocationValue)dv).exists)
                {
                    continue;
                }
                curLocVec.Set((LocationValue)dv);
                

                //dv = obProx["Velocity"].GetDataValue();
                //velVec.Set((VelocityValue)dv);

                dv = obProx["DestinationLocation"].GetDataValue();
                if (!((LocationValue)dv).exists)
                {
                    destVec.Set(curLocVec);
                    obProx["DestinationLocation"].SetDataValue(destVec.ToLocationValue());
                }
                else
                {
                    destVec.Set((LocationValue)dv);
                }

                dv = obProx["Throttle"].GetDataValue();
                double throttle = ((DoubleValue)dv).value;

                dv = obProx["MaximumSpeed"].GetDataValue();
                double maxSpeed = ((DoubleValue)dv).value;




                //
                // If the object's throttle or maximum speed are zero, set velocity to (0,0,0) and 
                // set the destination to the current location
                //
                if (throttle == 0 || maxSpeed == 0 || ((StringValue)obProx["State"].GetDataValue()).value == "Dead")
                {
                    velVec.Set(0, 0, 0);
                    obProx["Velocity"].SetDataValue(velVec.ToVelocityValue());

                    destVec.Set(curLocVec);
                    obProx["DestinationLocation"].SetDataValue(destVec.ToLocationValue());

                    dv = obProx["Throttle"].GetDataValue();
                    ((DoubleValue)dv).value = 0;
                    obProx["Throttle"].SetDataValue(dv);

                }


                //
                // Update what active regions the object is in.
                //
                StateDB.physicalObjects[id].activeRegions = GetActiveRegions(curLocVec);
                dv = obProx["InActiveRegions"].GetDataValue();
                StateDB.physicalObjects[id].activeRegions.Remove(id);// dont want to be blocked by itself.
                ((StringListValue)dv).strings = StateDB.physicalObjects[id].activeRegions;
                obProx["InActiveRegions"].SetDataValue(dv);


                double speedMultiplier = GetActiveRegionSpeedMultiplier(StateDB.physicalObjects[id].activeRegions);
                dv = obProx["ActiveRegionSpeedMultiplier"].GetDataValue();
                if (((DoubleValue)dv).value != speedMultiplier)
                {
                    ((DoubleValue)dv).value = speedMultiplier;
                    obProx["ActiveRegionSpeedMultiplier"].SetDataValue(dv);
                    //distClient.PutEvent(SimUtility.BuildActiveRegionSpeedMultiplierUpdateEvent(ref simModel, time, id));
                    distributor.viewProBackChannelEvents.Add(SimUtility.BuildActiveRegionSpeedMultiplierUpdateEvent(ref simModel, time, id));
                }

                if (time % 1000 == 0)
                {
                    actorFrame.objectID = id;
                    actorFrame.speciesName = StateDB.physicalObjects[id].speciesName;
                    actorFrame.ownerID = StateDB.physicalObjects[id].ownerID;
                    actorFrame.activeRegions = StateDB.physicalObjects[id].activeRegions;

                    ScoringDB.UpdateScore_ObjectExists(actorFrame);
                }

                //
                // Update the object's velocity based on its destinations, throttle, and maximum speed.
                //

                if (curLocVec.ScalerDistanceTo(destVec) > 0.01)
                {
                    if (curLocVec.ScalerDistanceTo(destVec) >= (maxSpeed * throttle * dTime * speedMultiplier))
                    {
                        velVec = curLocVec.VectorDistanceTo(destVec);
                        velVec.Normalize();
                        velVec = velVec.Multiply(maxSpeed * throttle * speedMultiplier);
                    }
                    else
                    {
                        velVec = destVec.Subtract(curLocVec);
                    }

                    obProx["Velocity"].SetDataValue(velVec.ToVelocityValue());
                    //
                    // Update the object's location based on its velocity
                    //
                    newLocVec.X = curLocVec.X + (velVec.X * dTime);
                    newLocVec.Y = curLocVec.Y + (velVec.Y * dTime);
                    newLocVec.Z = curLocVec.Z + (velVec.Z * dTime);





                    // TODO: Do obstruction check here
                    /*
                    obstructionIntersect = FindObstructionBoundaryIntersection(curLocVec, newLocVec);
                    landIntersect = FindLandBoundaryIntersection(curLocVec, newLocVec);
                    if (obstructionIntersect != null)
                    {
                        curLocVec.Set(obstructionIntersect);
                        obProx["Location"].SetDataValue(curLocVec.ToLocationValue());
                        destVec.Set(curLocVec);
                        obProx["DestinationLocation"].SetDataValue(destVec.ToLocationValue());
                        distClient.PutEvent(SimUtility.BuildMoveDoneEvent(ref simModel, time, id));
                        dv = obProx["Throttle"].GetDataValue();
                        ((DoubleValue)dv).value = 0;
                        obProx["Throttle"].SetDataValue(dv);
                    }
                    else if (landIntersect != null && (obProx.GetObjectType() != "AirObject"))
                    {
                        curLocVec.Set(landIntersect);
                        obProx["Location"].SetDataValue(curLocVec.ToLocationValue());
                        destVec.Set(curLocVec);
                        obProx["DestinationLocation"].SetDataValue(destVec.ToLocationValue());
                        distClient.PutEvent(SimUtility.BuildMoveDoneEvent(ref simModel, time, id));
                        dv = obProx["Throttle"].GetDataValue();
                        ((DoubleValue)dv).value = 0;
                        obProx["Throttle"].SetDataValue(dv);
                    }
                    else
                    {
                        curLocVec.Set(newLocVec);
                        obProx["Location"].SetDataValue(curLocVec.ToLocationValue());
                    } */

                    bool stopMotion = false;
                    switch (obProx.GetObjectType())
                    {
                        case "SeaObject":
                            if (!IsOnSea(newLocVec))
                            {
                                stopMotion = true;
                                distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                            time,
                                            ((StringValue)(obProx["OwnerID"].GetDataValue())).value,
                                            id + " has been blocked by land"));
                            }
                            break;
                        case "LandObject":
                            if (!IsOnLand(newLocVec))
                            {
                                stopMotion = true;
                                distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                            time,
                                            ((StringValue)(obProx["OwnerID"].GetDataValue())).value,
                                            id + " has been blocked by sea"));
                            }
                            break;
                        default:
                            break;
                    }

                    if (IsObstructed(curLocVec, newLocVec,id))
                    {
                        stopMotion = true;
                        //distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                        //                    time,
                        //                    ((StringValue)(obProx["OwnerID"].GetDataValue())).value,
                        //                    id + " has been blocked by an obstruction"));
                    }

                    if (!stopMotion)
                    {
                        curLocVec.Set(newLocVec);
                        obProx["Location"].SetDataValue(curLocVec.ToLocationValue());
                        //Now that location has been updated, set in helper library:
                        ObjectDistances.UpdateObjectLocation(id, curLocVec);
                    }
                    else
                    {
                        obProx["Location"].SetDataValue(curLocVec.ToLocationValue());
                        //Now that location has been updated, set in helper library:
                        ObjectDistances.UpdateObjectLocation(id, curLocVec);
                        destVec.Set(curLocVec);
                        obProx["DestinationLocation"].SetDataValue(destVec.ToLocationValue());
                        distClient.PutEvent(SimUtility.BuildMoveDoneEvent(ref simModel, time, id, "Obstruction"));
                        distributor.viewProBackChannelEvents.Add(SimUtility.BuildMoveDoneEvent(ref simModel, time, id, "Obstruction"));
                        dv = obProx["Throttle"].GetDataValue();
                        ((DoubleValue)dv).value = 0;
                        obProx["Throttle"].SetDataValue(dv);
                        distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                            time,
                                            ((StringValue)(obProx["OwnerID"].GetDataValue())).value,
                                            id + " was stopped by an obstruction"));
                    }

                }
                else // if the object has reached its destination and is not pursuing, send MoveDone
                {


                    velVec.Set(0, 0, 0);
                    newLocVec.Set(destVec);
                    obProx["Location"].SetDataValue(newLocVec.ToLocationValue());
                    //Now that location has been updated, set in helper library:
                    ObjectDistances.UpdateObjectLocation(id, newLocVec);
                    obProx["Velocity"].SetDataValue(velVec.ToVelocityValue());


                    if (!pursueStarted && throttle > 0)
                    {
                        /* TODO: Make sure MoveDone is being sent correctly.*/

                        distClient.PutEvent(SimUtility.BuildMoveDoneEvent(ref simModel, time, id, "ReachedDestination"));
                        distributor.viewProBackChannelEvents.Add(SimUtility.BuildMoveDoneEvent(ref simModel, time, id, "ReachedDestination"));
                        dv = obProx["Throttle"].GetDataValue();
                        ((DoubleValue)dv).value = 0;
                        obProx["Throttle"].SetDataValue(dv);

                        distClient.PutEvent(SimUtility.BuildSystemMessageEvent(ref simModel,
                                            time,
                                            ((StringValue)(obProx["OwnerID"].GetDataValue())).value,
                                            id + " has reached its destination"));
                    }



                }

                

                /* TODO: Ignore obstruction code for now */
                /*
                if (((StringValue)obProx["State"].GetDataValue()).value != "Dead" &&
                    ((obProx.GetObjectType() == "LandObject" && !IsOnLand(newLocVec)) ||
                    (obProx.GetObjectType() == "SeaObject" && !IsOnSea(newLocVec)) ||
                    IsObstructed(curLocVec,newLocVec) || IsInObstruction(newLocVec)))
                {
                    velVec.Set(0, 0, 0);
                    dv = obProx["Velocity"].GetDataValue();

                    obProx["Velocity"].SetDataValue(velVec.ToVelocityValue());
                    SimulationEvent done = SimulationEventFactory.BuildEvent(ref simModel, "MoveDone");
                    ((IntegerValue)done["Time"]).value = time;
                    ((StringValue)done["ObjectID"]).value = id;
                    distClient.PutEvent(done);
                    System.Console.WriteLine("Motion: object {0} stopped by land boundery", id);

                    dv = obProx["Throttle"].GetDataValue();
                    ((DoubleValue)dv).value = 0;
                    obProx["Throttle"].SetDataValue(dv);

                    if (((obProx.GetObjectType() == "LandObject" && !IsOnLand(curLocVec)) ||
                        obProx.GetObjectType() == "SeaObject" && !IsOnSea(curLocVec)) ||
                        IsInObstruction(curLocVec))
                    {
                        SimulationEvent dead = SimulationEventFactory.BuildEvent(ref simModel, "StateChange");
                        ((IntegerValue)dead["Time"]).value = time;
                        ((StringValue)dead["ObjectID"]).value = id;
                        ((StringValue)dead["NewState"]).value = "Dead";
                        distClient.PutEvent(dead);
                    }

                    return;
                }
                 * */

            }
        }
예제 #3
0
 private double CalculateHeading(LocationValue current, LocationValue destination)
 {
     if (!(current.exists && destination.exists))
         return 0;
     Vec3D c = new Vec3D(current);
     Vec3D d = new Vec3D(destination);
     Vec3D headingVector = c.VectorDistanceTo(d);
     headingVector.Normalize();
     return 180 / Math.PI * Math.Atan2(headingVector.X, headingVector.Y);
 }
예제 #4
0
파일: MotionSim.cs 프로젝트: wshanshan/DDD
        private void MoveObject(SimulationEvent e)
        {
            //objectProxies = bbClient.GetObjectProxies();
            DataValue dv = null;
            string id = ((StringValue)e["ObjectID"]).value;
            SimulationObjectProxy obProx = objectProxies[id];

            double throttle = 0;
            double maxSpeed = 0;
            double speedMultiplier = 0;

            Vec3D curVec = new Vec3D(0, 0, 0);
            Vec3D destVec = new Vec3D(0, 0, 0);
            Vec3D velVec = new Vec3D(0, 0, 0);

            dv = obProx["Location"].GetDataValue();
            curVec.Set((LocationValue)dv);


            dv = obProx["MaximumSpeed"].GetDataValue();
            maxSpeed = ((DoubleValue)dv).value;

            dv = e["DestinationLocation"];
            destVec.Set((LocationValue)dv);

            obProx["DestinationLocation"].SetDataValue(dv);

            dv = e["Throttle"];
            throttle = ((DoubleValue)dv).value;
            obProx["Throttle"].SetDataValue(dv);

            dv = obProx["ActiveRegionSpeedMultiplier"].GetDataValue();
            speedMultiplier = ((DoubleValue)dv).value;

            velVec = curVec.VectorDistanceTo(destVec);
            velVec.Normalize();
            velVec = velVec.Multiply(maxSpeed * throttle * speedMultiplier);

            obProx["Velocity"].SetDataValue(velVec.ToVelocityValue());

           // System.Console.WriteLine("Object {0} Velocity={1}", id.ToString(), velVec.ToString());


        }
예제 #5
0
        static public VelocityValue ComputeVelocityVector(LocationValue start, LocationValue dest, Double maxSpeed, Double throttle)
        {
            Vec3D startVec = new Vec3D(start);
            Vec3D destVec = new Vec3D(dest);
            Vec3D velVec;
            if (LocationIsEqual(start, dest))
            {
                velVec = new Vec3D(0, 0, 0);
                return velVec.ToVelocityValue();
            }
            else
            {
                velVec = startVec.VectorDistanceTo(destVec);
            }

            
            velVec.Normalize();
            velVec = velVec.Multiply(maxSpeed * throttle);

            //if (System.Double.IsNaN(velVec.X))
            //{
            //    int i = 0;
            //}

            return velVec.ToVelocityValue();
        }