Beispiel #1
0
        PlayerUnit(Gamestate oldState, GamestateBuilder newState)
        {
            Assert.Ref(oldState, newState);

            PlayerUnit oldUnit = oldState.PlayerUnit;

            texture = oldUnit.texture;

            InputDevice device = oldState.Input.Devices[oldUnit.inputDevice];
            RigidBody   body   = (RigidBody)(oldState.Physics.PhysicsBodies[oldUnit.rigidBody]);
            Sprite      spr    = (Sprite)(oldState.Video.Drawables[oldUnit.sprite]);

            float   x    = device.GetAxisFrac("Horizontal");
            float   y    = device.GetAxisFrac("Vertical");
            Vector2 move = new Vector2(x, y).ClampMagnitude(0, 1) * accel;

            body = body.AddImpulse(move);

            inputDevice = newState.Input.AddDevice(device);
            rigidBody   = newState.Physics.AddPhysicsObject(body);
            sprite      = newState.Video.AddDrawable(spr.Reposition(body.Position));
            newState.Audio.SetListenerPosition(body.Position);

            Window     window       = oldState.Video.Windows[0];
            Vector2    centerOffset = new Vector2(window.Rect.W / 2, window.Rect.H / 2);
            WorldPoint cameraPos    = body.Position.PixelTranslate(-centerOffset + new Vector2(16, 16));

            newState.Video.SetCamera(new Camera(cameraPos));
        }
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.I))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                if (collisionDispatcher.Raycast(ray, out DynamicCollider hit, 150.0f))
                {
                    if (hit.transform)
                    {
                        RigidBody hittedBody = hit.GetComponent <RigidBody>();

                        float   objectPlaneDist = Vector3.Dot(hit.transform.position - cam.transform.position, cam.transform.forward);
                        Vector2 dist            = cam.WorldToScreenPoint(hit.transform.position) - Input.mousePosition;


                        Vector3 currPt  = new Vector3(Input.mousePosition.x, Input.mousePosition.y, objectPlaneDist);
                        Vector3 worldPt = cam.ScreenToWorldPoint(currPt);

                        Vector3 relHitPt = worldPt - hit.transform.position;
                        hittedBody.AddImpulse(impulse * cam.transform.forward, relHitPt);
                    }
                }
            }
        }
Beispiel #3
0
 public void ApplyImpulse(RigidBody body)
 {
     //Debug.Log(contactNormal.ToString());
     if (Mathf.Approximately(body.friction, 0))
     {
         // converting the impulse back to world coordinates, then we
         // can apply it
         Vector3 impulse = desiredDeltaVelocity * invTotalInertia * contactBase[0];
         body.AddImpulse(impulse, relativeContactPosition);
     }
     else
     {
         // converting the impulse back to world coordinates, then we
         // can apply it
         Vector3 impulse = contactBase * CalculateImpulseWithFriction(body);
         body.AddImpulse(impulse, relativeContactPosition);
     }
 }