///// <summary>
        ///// Gets the center of mass of an array of Rigidbodies.
        ///// </summary>
        //public static Vector3 GetCenterOfMass(Rigidbody[] rigidbodies) {
        //	Vector3 CoM = Vector3.zero;
        //	float c = 0f;

        //	for (int i = 0; i < rigidbodies.Length; i++) {
        //		if (rigidbodies[i].gameObject.activeInHierarchy) {
        //			CoM += rigidbodies[i].worldCenterOfMass * rigidbodies[i].mass;

        //			c += rigidbodies[i].mass;
        //		}
        //	}

        //	return CoM / c;
        //}

        ///// <summary>
        ///// Gets the velocity of the center of mass of an array of Rigidbodies.
        ///// </summary>
        //public static Vector3 GetCenterOfMassVelocity(Rigidbody[] rigidbodies) {
        //	Vector3 CoM = Vector3.zero;
        //	float c = 0f;

        //	for (int i = 0; i < rigidbodies.Length; i++) {
        //		if (rigidbodies[i].gameObject.activeInHierarchy) {
        //			CoM += rigidbodies[i].velocity * rigidbodies[i].mass;

        //			c += rigidbodies[i].mass;
        //		}
        //	}

        //	return CoM / c;
        //}

        ///// <summary>
        ///// Divides an angular acceleration by an inertia tensor.
        ///// </summary>
        //public static void DivByInertia(ref Vector3 v, Quaternion rotation, Vector3 inertiaTensor) {
        //	v = rotation * Div(Quaternion.Inverse(rotation) * v, inertiaTensor);
        //}

        ///// <summary>
        ///// Scales an angular acceleration by an inertia tensor
        ///// </summary>
        //public static void ScaleByInertia(ref Vector3 v, Quaternion rotation, Vector3 inertiaTensor) {
        //	v = rotation * Vector3.Scale(Quaternion.Inverse(rotation) * v, inertiaTensor);
        //}

        ///// <summary>
        ///// Returns the angular acceleration from one vector to another.
        ///// </summary>
        //public static Vector3 GetFromToAcceleration(Vector3 fromV, Vector3 toV) {
        //	Quaternion fromTo = Quaternion.FromToRotation(fromV, toV);
        //	float requiredAccelerationDeg = 0f;
        //	Vector3 axis = Vector3.zero;
        //	fromTo.ToAngleAxis(out requiredAccelerationDeg, out axis);

        //	Vector3 requiredAcceleration = requiredAccelerationDeg * axis * Mathf.Deg2Rad;

        //	return requiredAcceleration / Time.fixedDeltaTime;
        //}

        ///// <summary>
        ///// Returns the angular acceleration from the current rigidbody rotation to Quaternion.identity.
        ///// Does not guarantee full accuracy with rotations around multiple axes).
        ///// </summary>
        //public static Vector3 GetAngularAcceleration(Quaternion fromR, Quaternion toR) {
        //	Vector3 axis = Vector3.Cross(fromR * Vector3.forward, toR * Vector3.forward);
        //	Vector3 axis2 = Vector3.Cross(fromR * Vector3.up, toR * Vector3.up);
        //	float angle = Quaternion.Angle(fromR, toR);
        //	Vector3 acc = Vector3.Normalize(axis + axis2) * angle * Mathf.Deg2Rad;

        //	return acc / Time.fixedDeltaTime;
        //}

        ///// <summary>
        ///// Returns the linear acceleration from one point to another.
        ///// </summary>
        //public static Vector3 GetLinearAcceleration(Vector3 fromPoint, Vector3 toPoint) {
        //	return (toPoint - fromPoint) / Time.fixedDeltaTime;
        //}


        //      /// <summary>
        //      /// The rotation expressed by the joint's axis and secondary axis
        //      /// </summary>
        //      public static Quaternion ToJointSpace(ConfigurableJoint joint) {
        //	Vector3 forward = Vector3.Cross (joint.axis, joint.secondaryAxis);
        //	Vector3 up = Vector3.Cross (forward, joint.axis);
        //	return Quaternion.LookRotation (forward, up);
        //}

        ///// <summary>
        ///// Calculates the inertia tensor for a cuboid.
        ///// </summary>
        //public static Vector3 CalculateInertiaTensorCuboid(Vector3 size, float mass) {
        //	float x2 = Mathf.Pow(size.x, 2);
        //	float y2 = Mathf.Pow(size.y, 2);
        //	float z2 = Mathf.Pow(size.z, 2);

        //	float mlp = 1f/12f * mass;

        //	return new Vector3(
        //		mlp * (y2 + z2),
        //		mlp * (x2 + z2),
        //		mlp * (x2 + y2));
        //}

        ///// <summary>
        ///// Divide all the values in v by the respective values in v2.
        ///// </summary>
        //public static Vector3 Div(Vector3 v, Vector3 v2) {
        //	return new Vector3(v.x / v2.x, v.y / v2.y, v.z / v2.z);
        //}



        /// <summary>
        /// align to vector
        /// </summary>
        /// <param name="r"></param>
        /// <param name="alignmentVector"></param>
        /// <param name="targetVector"></param>
        /// <param name="stability"></param>
        /// <param name="speed"></param>
        public static void AlignToVector(TSRigidBody r, TSVector alignmentVector, TSVector targetVector, FP stability, FP speed, bool debug = false)
        {
            if (r == null)
            {
                return;
            }

            // 围绕着某个轴进行了一个角度的旋转 angleAxis(x,y). x = 角度 y = 轴
            // x 角度值为 当前转向速度 * Stability 是一个 radian 值, * 57.29 转为角度值, 除以 speed 后 才是真正的角度值
            // part.angularVelocity 是当前旋转速度, 把旋转速度变为轴?
            Quaternion angleAxis = Quaternion.AngleAxis(r.angularVelocity.magnitude.AsFloat() * 57.29578f * stability.AsFloat() / speed.AsFloat(), r.angularVelocity.ToVector());



            // Vector3.Cross 是叉乘, alignmentVector 是当前我们把part 某个轴要对准target Vector 的向量, angleAxis 四元素乘以AlignmentVector 是把alignmentVector 方向调整为四元素方向
            // targetVector 是我们想要对准的角度
            // 叉乘出来的 a 就是 两个向量的九十度角的向量用来作为我们的旋转轴
            Vector3 a = Vector3.Cross(angleAxis * alignmentVector.ToVector(), targetVector.ToVector() * 10f);

            if (!float.IsNaN(a.x) && !float.IsNaN(a.y) && !float.IsNaN(a.z))
            {
                //Debug.Log(a.ToTSVector());

                r.AddTorque(a.ToTSVector() * speed * speed);

                Debug.DrawRay(r.position.ToVector(), alignmentVector.ToVector() * 0.3f, Color.red, 0f, false);
                Debug.DrawRay(r.position.ToVector(), targetVector.ToVector() * 0.3f, Color.green, 0f, false);
            }
        }
Exemplo n.º 2
0
    public override void OnSyncedStart()
    {
        thisBody = GetComponent <TSRigidBody>();
        IBody3D body1 = GetComponent <TSCollider>().Body;
        IBody3D body2 = connectedBody.Body;

        Vector3  worldPos   = transform.TransformPoint(anchor);
        TSVector TSworldPos = worldPos.ToTSVector();

        Vector3 worldAxis = transform.TransformDirection(Axis);

        TSWorldAxis = worldAxis.ToTSVector();


        if (useLimits)
        {
            thisJoint = new LimitedHingeJoint(PhysicsWorldManager.instance.GetWorld(), body1, body2, TSworldPos, TSWorldAxis, -Limits.Min, Limits.Max);
        }
        else
        {
            thisJoint = new MyHingeJoint(PhysicsWorldManager.instance.GetWorld(), body1, body2, TSworldPos, TSWorldAxis);
        }

        thisJoint.Activate();
    }
Exemplo n.º 3
0
 public void Awake()
 {
     trb         = this.transform.parent.GetComponent <TSRigidBody>();
     tsTransform = this.transform.parent.GetComponent <TSTransform>();
     minLength   = restLength - springTravel;
     maxLength   = restLength + springTravel;
 }
Exemplo n.º 4
0
    public void TapNearLine(TSVector tapLocation)
    {
        Debug.LogWarning("TAPNEARLINE");
        TSRigidBody lineRB = GetClosestLine(tapLocation);

        if (lineRB.angularVelocity.magnitude > 0) // exit this method if line is moving, can't hit lines while they move
        {
            return;
        }
        FP distanceToLine  = GetDistanceToLineFromPosition(lineRB, tapLocation);
        FP direction       = TSVector.Cross(tapLocation.normalized, lineRB.tsTransform.forward).normalized.y;
        FP tapEffectRadius = PlayerConfig.Instance.TapEffectRadius;
        FP minTapDistance  = PlayerConfig.Instance.MinTapDistance;
        FP effect          = FP.One;

        if (distanceToLine < minTapDistance) // apply penalty, tap was too close to the line
        {
            ApplyExplosiveForceOnLine(lineRB, -1 * direction * PlayerConfig.Instance.PenaltyEffectForce);
        }
        else if (tapEffectRadius >= distanceToLine) // within range, but not on line.  Apply normal force
        {
            effect = 1 - (distanceToLine - minTapDistance) / tapEffectRadius;
            ApplyExplosiveForceOnLine(lineRB, direction * PlayerConfig.Instance.TapEffectForce * effect);
        }
    }
Exemplo n.º 5
0
    public override void OnSyncedStart()
    {
        Debug.Log(owner.Id);
        Debug.Log(localOwner.Id);
        GameManager.Instance.AddPlayerToList(this.gameObject);
        Spawn();

        rigidBody = GetComponent <TSRigidBody> ();
    }
Exemplo n.º 6
0
    /**
     * @brief Initial setup when game is started.
     **/

    void CreateBoxes()
    {
        for (int i = 0; i < numberOfBoxesX; i++)
        {
            for (int j = 0; j < numberOfBoxesZ; j++)
            {
                GameObject  box  = TestManager.SyncedInstantiate(this.boxPrefab, TSVector.zero, TSQuaternion.identity);
                TSRigidBody body = box.GetComponent <TSRigidBody>();
                body.position = new TrueSync.TSVector(i * 2 - 5, 1, j * 2);
            }
        }
    }
Exemplo n.º 7
0
    /**
     *
     * /**
     * @brief Initial setup when game is started.
     **/
    public override void OnSyncedStart()
    {
        tsRigidBody = GetComponent <TSRigidBody>();
        // Sets sprite and animator controller based on player's id
//		if (owner.Id == 1) {
//			tsRigidBody.position = new TSVector(0, -5, 0);
//		} else {
//            TSVector offset = new TSVector(-0.63f, -5, -0.87f);
//            tsRigidBody.GetComponent<TSCollider>().Center = offset;
//			tsRigidBody.position = new TSVector(-1 - offset.x, 0, 0);
//		}
    }
Exemplo n.º 8
0
    public override void OnSyncedStart()
    {
        _rigidbody = GetComponent <TSRigidBody>();

        if (_rigidbody != null)
        {
            _rigidbody.velocity = tsTransform.forward * speed;
        }
        else
        {
            Debug.LogError("Missing TSRigidBody on current GameObject!");
        }
    }
Exemplo n.º 9
0
    /**
     * @brief Initial setup when game is started.
     **/
    public void Start()
    {
        //CBGlobalEventDispatcher.Instance.AddEventListener((int)EVENT_ID.EVENT_FRAME_TICK, OnSyncedUpdate);
        //KBEngine.Event.registerOut("recieveFrameTick", this, "OnSyncedUpdate");


        tsRigidBody = GetComponent <TSRigidBody>();

        tsTransform = GetComponent <TSTransform>();


        Debug.Log("PlayerContorl::start:" + tsRigidBody + ",tsTransform:" + tsTransform);

        // if is first player then changes ball's color to black
    }
Exemplo n.º 10
0
    /**
     * @brief Initial setup when game is started.
     **/
    public override void OnSyncedStart()
    {
        controlledBody = this.GetComponent <TSRigidBody> ();

        // if is first player then changes ball's color to black
        if (owner != null && owner.Id == 1)
        {
            GetComponent <Renderer> ().material.color = Color.black;
        }

        if (!createdRuntime)
        {
            controlledBody.position = new TrueSync.TSVector(-3 + (owner.Id - 1) * 4, 1, 16);
        }
    }
Exemplo n.º 11
0
    public static TSRigidBody GetClosestLine(TSVector position)
    {
        TSRigidBody closestLine   = Line.LineList[0];
        FP          closestResult = new FP(-1); // DOT product of -1, farthest possible value to check direction of a vector

        foreach (TSRigidBody thisLine in Line.LineList)
        {
            FP result = TSVector.Dot(thisLine.tsTransform.forward, position.normalized);

            if (result > closestResult)
            {
                closestResult = result;
                closestLine   = thisLine;
            }
        }

        return(closestLine);
    }
Exemplo n.º 12
0
    public void OnSyncedStart()
    {
        tsRigidBody = GetComponent <TSRigidBody>();

        tsTransform = GetComponent <TSTransform>();

        actions = GetComponent <Actions>();

        controller = GetComponent <PlayerController>();

        foreach (PlayerController.Arsenal a in controller.arsenal)
        {
            weapon_list.Add(a.name);
        }
        if (projectilePrefab == null)
        {
            projectilePrefab = (GameObject)Resources.Load("Perfabs/bullet");
        }

        Debug.Log("PlayerContorl::start:" + tsRigidBody + ",tsTransform:" + tsTransform + ",projectilePrefab:" + projectilePrefab);
    }
Exemplo n.º 13
0
    public override void OnSyncedStart()
    {
        nickLabel = (Instantiate(nickPrefab) as GameObject).GetComponent <Text>();
        nickLabel.transform.SetParent(GameObject.Find("Canvas").transform, false);
        nickLabel.text = owner.Name;

        rb = GetComponent <TSRigidBody> ();
        ss = (SphereShape)GetComponent <TSSphereCollider> ().Shape;

        rb.position          += new TSVector(owner.Id * 2, 0, 0);
        originalRadius        = ss.Radius;
        originalGraphicsScale = graphics.localScale;

        // needed for rollbacks
        StateTracker.AddTracking(this, "currentScale");

        if (owner.Id == localOwner.Id)
        {
            Camera.main.GetComponent <TopDownCamera> ().target = transform;
        }
    }
Exemplo n.º 14
0
    /// <summary>
    /// RepositionLines - repositions the player lines evenly with a calculated angle determined by the number of players
    /// </summary>
    private void RepositionLines()
    {
        FP lineAngle = FP.Zero;

        if (_playerList.Count < 3)
        {
            lineAngle = new FP(170);
        }
        else
        {
            lineAngle = new FP(360 / _playerList.Count);
        }

        for (int index = 0; index < Line.LineList.Count; index++)
        {
            TSRigidBody rb = Line.LineList[index];
            rb.MovePosition(new TSVector(0, 1, 0));
            //rb.angularVelocity = TSVector.up;
            rb.tsTransform.rotation = TSQuaternion.AngleAxis(index * lineAngle, TSVector.up);
            rb.gameObject.name      = "Line " + index;
        }
    }
Exemplo n.º 15
0
 public void OnSyncedCollisionEnter(TSCollision other)
 {
     if (other.gameObject.tag == "Food")
     {
         // grow slow and destroy food
         currentScale *= growSlow;
         // move food to a new place (deterministic randoms)
         TSRigidBody food = other.rigidbody;
         food.position = new TSVector(TSRandom.Range(-35, 35), 0, TSRandom.Range(-20, 20));
     }
     else if (other.gameObject.tag == "Player")
     {
         LockPlayer enemy = other.gameObject.GetComponent <LockPlayer> ();
         if (ss.Radius > enemy.ss.Radius)
         {
             // grow fast and eat other
             currentScale *= growFast;
             enemy.tsRigidBody.position = new TSVector(TSRandom.Range(-35, 35), 0, TSRandom.Range(-20, 20));
             enemy.currentScale         = FP.One;
             //TrueSyncManager.SyncedDestroy(other);
         }
     }
 }
Exemplo n.º 16
0
 public override void OnSyncedStart()
 {
     rbody = GetComponent <TSRigidBody>();
 }
Exemplo n.º 17
0
 public static void ApplyExplosiveForceOnLine(TSRigidBody lineRB, FP force)
 {
     lineRB.AddTorque(TSVector.up * force);
 }
 public static void AddRelativeTorque(this TSRigidBody thisTSBody, TSVector torque)
 {
     thisTSBody.tsCollider.Body.TSApplyRelativeTorque(torque);
 }
Exemplo n.º 19
0
 public override void OnSyncedStart()
 {
     rigidBody = GetComponent <TSRigidBody> ();
     Target    = GameManager.Instance.GetRandomPlayer();
     //Debug.Log (Target.name);
 }
Exemplo n.º 20
0
 void Start()
 {
     tsRigidBody = GetComponent <TSRigidBody>();
     //tsRigidBody.position = new TrueSync.TSVector(-2, 1, 16);
     //StartCoroutine(UpdateCorutine());
 }
Exemplo n.º 21
0
 public static FP GetDistanceToLineFromPosition(TSRigidBody lineRB, TSVector position)
 {
     return(TSVector.Distance(lineRB.tsTransform.forward * position.magnitude, position));
 }
Exemplo n.º 22
0
 private void Awake()
 {
     TSRigidBody = this.GetComponent <TSRigidBody>();
     // TSRigidBody.centerOfMass = TSVector.right * 2;
     TSRigidBody.drag = 1;
 }
Exemplo n.º 23
0
 public override void OnSyncedStart()
 {
     thisBody = GameObject.FindGameObjectWithTag("Bar").GetComponent <TSRigidBody>();
 }
Exemplo n.º 24
0
 /**
  * @brief Initial setup.
  **/
 void Start()
 {
     audioSource = GetComponent <AudioSource>();
     tsRigidBody = GetComponent <TSRigidBody>();
 }
Exemplo n.º 25
0
    public override void OnSyncedStart()
    {
        rigidBody = GetComponent <TSRigidBody> ();

        rigidBody.AddForce(tsTransform.forward * bulletSpeed, ForceMode.Impulse);
    }