【发布时间】:2013-12-01 21:51:24
【问题描述】:
(使用 unity3d 4.3 2d,它使用 box2d 类似物理)。
我在预测轨迹方面遇到问题 我正在使用:
Vector2 startPos;
float power = 10.0f;
float interval = 1/30.0f;
GameObject[] ind;
void Start (){
transform.rigidbody2D.isKinematic = true;
ind = new GameObject[dots];
for(int i = 0; i<dots; i++){
GameObject dot = (GameObject)Instantiate(Dot);
dot.renderer.enabled = false;
ind[i] = dot;
}
}
void Update (){
if(shot) return;
if(Input.GetAxis("Fire1") == 1){
if(!aiming){
aiming = true;
startPos = Input.mousePosition;
ShowPath();
}
else{
CalculatePath();
}
}
else if(aiming && !shot){
transform.rigidbody2D.isKinematic = false;
transform.rigidbody2D.AddForce(GetForce(Input.mous ePosition));
shot = true;
aiming = false;
HidePath();
}
}
Vector2 GetForce(Vector3 mouse){
return (new Vector2(startPos.x, startPos.y)- new Vector2(mouse.x, mouse.y))*power;
}
void CalculatePath(){
ind[0].transform.position = transform.position; //set frist dot to ball position
Vector2 vel = GetForce(Input.mousePosition); //get velocity
for(int i = 1; i < dots; i++){
ind[i].renderer.enabled = true; //make them visible
Vector3 point = PathPoint(transform.position, vel, i); //get position of the dot
point.z = -1.0f;
ind[i].transform.position = point;
}
}
Vector2 PathPoint(Vector2 startP, Vector2 startVel, int n){
//Standard formula for trajectory prediction
float t = interval;
Vector2 stepVelocity = t*startVel;
Vector2 StepGravity = t*t*Physics.gravity;
Vector2 whattoreturn = ((startP + (n * stepVelocity)+(n*n+n)*StepGravity) * 0.5f);
return whattoreturn;
}
使用这个,我得到了错误的轨迹。 1. 就好像重力根本不会拖曳轨迹,是的,我知道重力很弱,因为:
t*t*Physics.gravity = 0.03^2 * vector2(0, -9.8) = vector2(0, -0.00882)
但这就是公式:S 2.由于重力低,速度太强。
这是视频: http://tinypic.com/player.php?v=1z50w3m&s=5
轨迹公式形式: http://www.iforce2d.net/b2dtut/projected-trajectory
我该怎么办?
我发现如果我设置 StepGravity 到更强大的东西,例如 (0, -0.1) 并将 startVel 除以 8 我得到了几乎正确的轨迹,但我不想要那样,我需要真正的轨迹路径。
来自 answer.unity3d.com 的用户说我应该在这里问,因为这里有更多的数学编码人员。
我搜索了很多关于这个问题(我是如何找到那个公式的)。
【问题讨论】:
标签: unity3d 2d physics game-physics prediction