// All presses and releases of keys also update position/rotation to make sure the car hasn't desync'd void sendUpdate() { PositionVelocityAccelerationRotationJson newUpdate = getPositionVelocityAccelerationRotationJson(); WebSocketManager.instance.Dispatch("move", JsonUtility.ToJson(newUpdate), true); updateBuffer[currentSimulationStep] = newUpdate; currentSimulationStep = (currentSimulationStep + 1) % BUFFER_SIZE; }
PositionVelocityAccelerationRotationJson getPositionVelocityAccelerationRotationJson() { // Get Current Position Vec 3 and rotation quat Vector3 position = rb.position; Vector3 velocity = rb.velocity; // Vector3 acceleration = defined above Quaternion quat = Quaternion.Euler(0, 0, rb.rotation); // todo rotation is only in z plane? // JSON-ify position and rotation PositionVelocityAccelerationRotationJson posVelAccRot = new PositionVelocityAccelerationRotationJson(currentSimulationStep, position, velocity, acceleration, quat); // Then return return(posVelAccRot); //return JsonUtility.ToJson(posVelAccRot); }
public void setCollisionPosVelAccRot(Vector3 newVec3Pos, Vector2 newVelocity, Vector2 newAcceleration, Quaternion newQuat) { Debug.Log("CarController setCollisionPosVelAccRot: ENTER"); transform.position = newVec3Pos; transform.rotation = newQuat; velocityCurrent = velocityDestination; acceleration = newAcceleration; timeSinceLastFrame = 0f; // Now send back an ack with OUR position to let the remote player know where we were at the approximate time of collision int halfRTT = rtt / 2; int previousLocationIndex = currentSimulationStep - halfRTT; if (previousLocationIndex < 0) { previousLocationIndex += BUFFER_SIZE; } PositionVelocityAccelerationRotationJson previousPos = updateBuffer[previousLocationIndex]; //Debug.Log("CarController setCollisionPosVelAccRot: Dispatching collisionAck to WebSocketManager"); //WebSocketManager.instance.Dispatch("collisionAck", JsonUtility.ToJson(previousPos), true); }
void OnCollisionEnter2D(Collision2D collision) { Debug.Log("CarController OnCollisionEnter2D: ENTER"); // Only check collisions for the local player, the remote players will check on their end Debug.Log("CarController OnCollisionEnter2D: Test if this is the collision call from the local player"); if (isLocalPlayer) { Debug.Log("CarController OnCollisionEnter2D: Collided with object: " + collision.gameObject.name + ", Object component: " + collision.gameObject.GetComponent("CarController")); if (collision.gameObject.GetComponent("CarController") != null && collision.gameObject.name != UserName) { // We have collided with a remote player Debug.Log("CarController OnCollisionEnter2D: This is a remote collision!"); // We must notify the other player if (collisionMode == 2) { if (inCollision) { return; } Debug.Log("CarController OnCollisionEnter2D: MY ATEMPT"); PositionVelocityAccelerationRotationJson updateAtCollision = getPositionVelocityAccelerationRotationJson(); WebSocketManager.instance.Dispatch("collision", JsonUtility.ToJson(updateAtCollision), true); // Now we have to temporarily enter lockstep until we get a response from the other client inCollision = true; lockStep = true; } else if (collisionMode == 1) { // WATCHDOGS ATTEMPT Debug.Log("CarController OnCollisionEnter2D: WATCHDOGS ATTEMPT"); Debug.Log("CarController OnCollisionEnter2D: Call startBlendCollision on other player's CarController"); collision.gameObject.GetComponent <CarController>().startBlendCollision(); } } } }