/** * This function returns the time series tuple for the current frame. * formate:"ts,mID,mDiff,mType,Lx,Ly,Rx,Ry,Bx,By,Lvel,Rvel,Bvel,Plen,H1input,H2input,composInput,Lscore,Rscore\n" * ts: timestamp * mID: match ID * mDiff: match difficulty * mType: match type * Lx,Ly: left paddle x and y position * Rx,Ry: right paddle x and y position * Bx,By: ball x and y position * Lvel: left paddle velocity * Rvel: right paddle velocity * Bvel: ball velocity * Plen: paddle length * H1input: human 1 input * H2input: human 2 input (if not applicable, then value is 9999) * composInput: composite input of both human players (if not applicable, then value is 9999) * Lscore: left paddle score * Rscore: right paddle score * */ public static string GetTimeSeriesTuple() { string t; Vector2 garbage = BallUtils.GetBallVelocity(); //TODO: please figure out why removing this breaks everything t = GeneralUtils.GetTimeStamp() + "," + GeneralUtils.m_dataScript.currMatchNum.ToString() + "," + GeneralUtils.GetCurrentMatch().difficulty.ToString() + "," + GeneralUtils.GetCurrentConfig().ToString() + "," + GeneralUtils.GetHumanPosition().x.ToString() + "," + GeneralUtils.GetHumanPosition().y.ToString() + "," + GeneralUtils.GetAgentPosition().x.ToString() + "," + GeneralUtils.GetAgentPosition().y.ToString() + "," + BallUtils.GetBallPosition().x.ToString() + "," + BallUtils.GetBallPosition().y.ToString() + "," + VelocityUtils.GetVelocity("leftPaddle").ToString() + "," + VelocityUtils.GetVelocity("rightPaddle").ToString() + "," + VelocityUtils.GetVelocity("ball").ToString() + "," + GeneralUtils.GetPaddleSize("Left") + "," + GeneralUtils.GetPaddleSize("Right") + "," + GeneralUtils.GetHumanInput(1).ToString() + "," + GeneralUtils.GetHumanInput(2).ToString() + "," + GeneralUtils.GetHumanInput(3).ToString() + "," + GeneralUtils.GetPlayerScore("Left").ToString() + "," + GeneralUtils.GetPlayerScore("Right").ToString() + "\n"; return(t); }
/** * Default constructor. */ public BallHitEvent(string _paddleID_) : base(_Event.BALL_HIT) { ballPosition = BallUtils.GetBallPosition(); leftPosition = GeneralUtils.GetHumanPosition(); rightPosition = GeneralUtils.GetAgentPosition(); paddleSize = GeneralUtils.GetPaddleSize(_paddleID_); }
/** * Default constructor. */ public MatchEndEvent() : base(_Event.MATCH_END) { ballPosition = BallUtils.GetBallPosition(); leftPosition = GeneralUtils.GetHumanPosition(); rightPosition = GeneralUtils.GetAgentPosition(); paddleSize = GeneralUtils.GetPaddleSize("Left"); matchDuration = GeneralUtils.TimeSinceMatchBegan(); LeftScore = GeneralUtils.GetPlayerScore("Left"); RightScore = GeneralUtils.GetPlayerScore("Right"); }
void Start() { bmAuto = new BallMovementAutomaton(); agentComm = GameObject.Find("Agent").GetComponent <AgentInput>(); ballLaunced = false; ballCollidedPaddle = false; timeThreshold = 25; //milliseconds timeOfLastCurrPosCapture = DateTime.Now; prevBx = 0F; currBx = 0F; currPos = BallUtils.GetBallPosition(); posSamples = new PositionSamples(currPos); }
/** * This function calculates the destination of the agent paddle. */ public static Vector2 GetAgentDestination() { Vector2 intersection; Vector2 b = BallUtils.GetBallPosition(); Vector2 v = BallUtils.GetBallVelocity(); float m = (v.x == 0 ? 0 : (v.y / v.x)); float x_dest = 42.47662F; float y_intercept = ((-1F * b.x * v.y) / v.x) + b.y; intersection = new Vector2(x_dest, m * x_dest + y_intercept); return(intersection); }
/** * This function returns the current EnvState object. */ public static EnvState GetEnvState(string extraInfo = "NULL") { int humanScore = m_sessionScript.LeftScore; int agentScore = m_sessionScript.RightScore; Position3D ballOrientation = Position3D.Vector3ToPosition3D(m_ballScript.transform.eulerAngles); Position2D humanPos = Position2D.Vector2ToPosition2D(GeneralUtils.GetHumanPosition()); Position2D agentPos = Position2D.Vector2ToPosition2D(GeneralUtils.GetAgentPosition()); Position2D ballPos = Position2D.Vector2ToPosition2D(BallUtils.GetBallPosition()); float leftPaddleLen = m_humanScript.transform.localScale.y; float rightPaddleLen = m_agentScript.transform.localScale.y; float leftPaddleWidth = m_humanScript.transform.localScale.x; float rightPaddleWidth = m_agentScript.transform.localScale.x; Match currMatch = m_sessionScript.currMatch; int sessionState = m_sessionScript.sessionAuto.CurrState; EnvState envState = new EnvState(humanScore, agentScore, ballOrientation, ballPos, agentPos, humanPos, leftPaddleLen, rightPaddleLen, leftPaddleWidth, rightPaddleWidth, sessionState, currMatch, extraInfo); return(envState); }
public void OnCollisionExit2D(Collision2D c) { if (GeneralUtils.MatchInProgress() && GeneralUtils.GetProcessRole() == GeneralUtils.ROLE_SERVER) { //increment the ball hit count ballHitCount += 1; //if the ball hit a paddle foreach (ContactPoint2D contactPt2d in c.contacts) { if (contactPt2d.collider.tag == "paddle") { //announce that the ball has hit a paddle EventUtils.LogEvent(_Event.BALL_HIT); //if this is Rally mode, announce a point if (GeneralUtils.IsRally(GeneralUtils.GetCurrentConfig())) { EventUtils.AnnouncePoint(GeneralUtils.RALLY_ID); } //get ball and paddle positions as well as paddle length Vector3 paddlePos; float Pl = 0F; if (contactPt2d.collider.transform.name == "P1") { //announce hit event EventUtils.AnnounceBallHitEvent("Left"); Pl = GeneralUtils.GetPaddleSize("Left"); paddlePos = GeneralUtils.GetHumanPosition(); } else { //announce hit event EventUtils.AnnounceBallHitEvent("Right"); Pl = GeneralUtils.GetPaddleSize("Right"); paddlePos = GeneralUtils.GetAgentPosition(); } Vector3 ballPos = BallUtils.GetBallPosition(); //store y component of paddle pos float Py = paddlePos.y; //get distance between paddle hit and paddle center float d = Math.Abs(Py - ballPos.y); //piecewise function for adding force perturbation based on paddle hit location float scalar = 1F; if (d < (Pl / 10F)) //use slow scalar { scalar = Cs; } else if (d > (3F * Pl / 10F)) //use fast scalar { scalar = Cf; } else //use medium speed scalar { scalar = Cm; } //apply force perturbation myRigidbody2D.velocity = Vector2.zero; myRigidbody2D.angularVelocity = 0; if (contactPt2d.collider.name == "P1") { myRigidbody2D.AddForce(new Vector2(scalar * 10F, scalar * 2.5F * (signs[rand.Next(0, 8)] ? 1F : -1F))); myRigidbody2D.AddTorque(20F); } else { myRigidbody2D.AddForce(new Vector2(scalar * -10F, scalar * 2.5F * (signs[rand.Next(0, 8)] ? 1F : -1F))); myRigidbody2D.AddTorque(-20F); } } else { //tell the agent that the ball hit something agentComm.ballHitSomething = true; } } } }
void Update() { //if this process is the server if (GeneralUtils.GetProcessRole() == GeneralUtils.ROLE_SERVER) { //store ball position changes if (DateTime.Now.Subtract(timeOfLastCurrPosCapture).TotalMilliseconds > 50) { //capture the current time timeOfLastCurrPosCapture = DateTime.Now; } currPos = BallUtils.GetBallPosition(); posSamples.AddSample(currPos); // //draw lines TODO: remove this when done testing // Vector2 p1 = BallUtils.GetBallPosition(); // Vector2 ballVel = BallUtils.GetBallVelocity(); // Vector2 p2 = new Vector2( p1.x+5F*ballVel.x, p1.y+5F*ballVel.y ); // Vector3 draw1 = new Vector3 ( p1.x, p1.y, 0 ); // Vector3 draw2 = new Vector3 ( p2.x, p2.y, 0 ); // Debug.DrawLine ( draw1, draw2, Color.green ); //CURRENT STATE: MATCH HAS NOT YET BEGUN if (bmAuto.CurrState == BallMovementAutomaton.MATCH_NOT_IN_PROGRESS) { //if the match begins if (GeneralUtils.MatchInProgress()) { //enact transition to the next state bmAuto.Transition(BallMovementAutomaton.BALL_NOT_LAUNCHED); } } //CURRENT STATE: BALL HAS NOT YET BEEN LAUNCHED / COLLIDED else if (bmAuto.CurrState == BallMovementAutomaton.BALL_NOT_LAUNCHED) { //if the ball is launched if (ballLaunced) { //reset flags ballLaunced = false; //capture the current time timeBeganObserving = DateTime.Now; //capture the ball's position prevBx = BallUtils.GetBallPosition().x; //enact transition to the next state bmAuto.Transition(BallMovementAutomaton.OBSERVING_TRAJECTORY); } //if the match ends if (!GeneralUtils.MatchInProgress()) { //enact transition to the next state bmAuto.Transition(BallMovementAutomaton.MATCH_NOT_IN_PROGRESS); } } //CURRENT STATE: CURRENTLY OBSERVING THE BALL'S TRAJECTORY else if (bmAuto.CurrState == BallMovementAutomaton.OBSERVING_TRAJECTORY) { //update the position sample object with the current position posSamples.AddSample(currPos); //if the ball has a collision if (ballCollidedPaddle) { //reset flag ballCollidedPaddle = false; //capture the current time timeBeganObserving = DateTime.Now; //capture the ball's position prevBx = BallUtils.GetBallPosition().x; //enact transition to the next state bmAuto.Transition(BallMovementAutomaton.OBSERVING_TRAJECTORY); } //if enough time has elapsed if (DateTime.Now.Subtract(timeBeganObserving).TotalMilliseconds > timeThreshold) { //capture the ball's current position currBx = BallUtils.GetBallPosition().x; //if the ball has moved closer to the right paddle if (currBx > prevBx) { //annouce that the ball is moving towards the right paddle agentComm.ballMovingToRight = true; agentComm.ballMovingToLeft = false; //print ( "======> RIGHT, v = " + GeneralUtils.GetBallVelocity().ToString() ); } //otherwise else { //annouce that the ball is moving towards the left paddle agentComm.ballMovingToRight = false; agentComm.ballMovingToLeft = true; //print ( "======> LEFT, v = " + GeneralUtils.GetBallVelocity().ToString() ); } //enact transition to the next state bmAuto.Transition(BallMovementAutomaton.OBSERVING_TRAJECTORY); //intended self transition } //if the match ends if (!GeneralUtils.MatchInProgress()) { //enact transition to the next state bmAuto.Transition(BallMovementAutomaton.MATCH_NOT_IN_PROGRESS); } } } }