在3D球形附近的球运动

时间:2012-03-15 14:17:36

标签: java c# coordinates game-physics

虽然也可以使用C#,但我使用的是Unity,主要是Java代码。

在我的游戏中,玩家控制一个球(刚体),应用球体物理并完成与collisons一起工作,你必须引导自己环绕一个行星Mario Galaxy-esque。

我遇到的问题是,控制系统似乎与我所追求的相反,取决于我在这个星球上的位置。有没有办法约束控制系统,这样无论行星或摄像机的方向如何,向上总是向前,向左总是等等。

I have found some documentation regarding spherical corodinate systems,但我不确定这是否可行,以及我是否会过度复杂化。

用于保持球员在地球上的球的代码:

    FauxGravityAttractor.js

// Set to true for mono-directional gravity
var useLocalUpVector : boolean = false;

// Force applied along gravity up-vector (negative = down)
var fauxGravity = -10.0;

function Attract ( body : FauxGravityBody ){

    var gravityUp : Vector3;
    var localUp: Vector3;
    var localForward : Vector3;

    var t : Transform = body.transform;
    var r : Rigidbody = body.rigidbody;


    // Figure out the body's up vector

    if(useLocalUpVector){
        gravityUp = transform.up;   

    } else {

        gravityUp = t.position - transform.position;
        gravityUp.Normalize();
    }



    // Accelerate the body along its up vector

    r.AddForce( gravityUp * fauxGravity * r.mass );
    r.drag = body.grounded ? 1 : 0.1;



    // If the object's freezerotation is set, we force the object upright

    if(r.freezeRotation){

        // Orient relatived to gravity

        localUp = t.up;
        var q = Quaternion.FromToRotation(localUp, gravityUp);
        q = q * t.rotation;
        t.rotation = Quaternion.Slerp(t.rotation, q, 0.1);
        localForward = t.forward;

    }

}

球运动脚本:

using UnityEngine;
using System.Collections;

public class MarbleControl : MonoBehaviour {

    public float movementSpeed = 6.0f;

    void Update () {

        Vector3 movement = (Input.GetAxis("Horizontal") * Vector3.left * movementSpeed) + (Input.GetAxis("Vertical") * -Vector3.forward *movementSpeed);

        rigidbody.AddForce(movement, ForceMode.Force);
    }

    void OnTriggerEnter  (Collider other  ) {

        if (other.tag == "Pickup")

        {

            MarbleGameManager.SP.FoundGem();

            Destroy(other.gameObject);

        }    

    }
}

1 个答案:

答案 0 :(得分:0)

Vector3.leftVector3.forward[-1,0,0][0,0,-1]之类的常量吗?在这种情况下,看起来你需要用相对于大理石的orinetation协调​​的矢量替换它们,以便例如Vector3.left被替换为向量的世界空间坐标,指向您想要成为大理石左方向的方向。