简体   繁体   English

如何转动物体并向相反方向移动?

[英]How to turn an object around and move into the opposite direction?

Considering out of bounds position is 6 and -6. 考虑到越界位置是6和-6。

I want to make the ship turn around and move in the opposite direction. 我想让船转向并向相反方向移动。

This is the code I have.. It is still not working 100% how I want it. 这是我的代码..它仍然没有100%我想要的工作。 I am curious to see if anyone had any ideas how do improve. 我很想知道是否有人有任何改进的想法。 Here is the logic of my code. 这是我的代码的逻辑。

//If the ship hits a boundary it turns around and moves in the opp.
//direction. To do this, the ship's velocty should be flipped from a 
//negative into a positive number, or from pos to neg if boundary
//is hit.

//if ship position is -5 at velocity -1 new ship pos is -6
//if ship position is -6 at velocity -1 new ship velocity is +1
//                                      new ship position is +5

Here is my code: 这是我的代码:

public void move() 
{
    position = velocity + position;

    if (position > 5)
    {
        velocity = -velocity;
    }
    else if (position < -5)
    {
        velocity = +velocity;
    }
}

You can do this: 你可以这样做:

public void move() 
    {
    //first check where the next move will be:
    if ((position + velocity) > 5 || (position + velocity) < -5){

        // Here we change direction (the velocity is multiplied for -1)
        velocity *= -1;

    }

    position += velocity;
}

The code velocity = +velocity; 代码velocity = +velocity; will not change a negative velocity into a positive velocity. 不会将负速度变为正速度。 What this will do is equivalent to multiplying velocity with +1 which does not change the sign. 这样做相当于将速度乘以+1而不改变符号。

To flip the sign of velocity when going out of bounds you need to always multiply -1 . 要在超出界限时翻转速度符号,您需要始终乘以-1

It's not very clear what the bounds are so below I assume that they are 6 and -6. 我不清楚界限是什么,所以我认为它们是6和-6。

position += velocity;
//make sure the ship cannot go further than the bounds
//but also make sure that the ship doesn't stand still with large velocities
if (position > 6)
{
    velocity = -velocity;
    position = 6;
}
if (position < -6)
{
    velocity = -velocity;
    position = -6;
}

当它到达边界时,将速度乘以-1。

Firstly, your logic looks a bit flawed. 首先,你的逻辑看起来有点瑕疵。 If the position is -6 and the velocity is -1 , to start moving in the opposite direction, you new position should be -5 (and not +5 ) and the velocity +1 . 如果位置为-6且速度为-1 ,则要开始向相反方向移动,新位置应为-5 (而不是+5 ),速度为+1

Also, you need to invert the sign of your velocity whenever your position hits the boundary conditions. 此外,只要您的位置达到边界条件,您就需要反转速度符号。

public void move() {
    if (Math.abs(position + velocity) > 5) { // Check for boundary conditions
        velocity *= -1;
    }
    position = position + velocity; // position += velocity;
}

If you want it to change direction you need to flip the sign. 如果你想改变方向,你需要翻转标志。 This is the same a * -1 or negating it. 这是相同的* -1或否定它。

public void move() {    
    // prevent it jumping through the wall.
    if (Math.abs(position + velocity) > 5)
        velocity = -velocity;

    position += velocity;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM