2
votes

I am making this in Unity 3D (in 2D mode) so my code examples will be in Unity/C# but I think it is more a general physics problem.

I have a 2D spaceship that fly around and it occasionally encounters some asteroids (or other ships) and the way I have implemented this is by converting user input to a 2D vector like this:

Vector2 movement = new Vector2(0, currentPlayerSpeed);

The "currentPlayerSpeed" is calculated by the ship max speed and added in a acceleration over time, and I then add that Vector2D to the ships transform in the Update method like this (the ship always fly "up" relative to its own rotation):

transform.Translate(movement);

I rotate the ship based on input (and added in some max rotation force) and I rotate the ship directly and not via the physics body (I set it via transform.Rotate (rotationVector)) and the ship always fly in the direction of "up" with the current velocity, and this means that it does not fly even the the least sideways never ever. This is the way I want it to work, but... I would also like the ship to bounce of any asteroids (or other ships) it hits and I would like to use the build in physics for this but I do not know how to apply the forces correctly so that I maintain the correct velocity/direction.

I have converted my Update to FixedUpdate and I now do this to move the ship instead:

rigidbody2D.AddForce (transform.up * currentPlayerSpeed);
if(rigidbody2D.velocity.magnitude > maxShipSpeed)
{
    rigidbody2D.velocity = rigidbody2D.velocity.normalized * maxShipSpeed;
}

So with this I get the bounce off effect I want, but obviously the ship now fly sideways most of the time as the force is not directly translated to the ships velocity and manipulating the rigidbody2D.velocity directly gives me strange results so I have no idea how to do this. Here is a illustration of what I want and why it fails to clear things up (the vectors are not drawn correctly but I hope it makes sense anyway).

enter image description here

I have tried with the following code:

float magnitude = rigidbody2D.velocity.magnitude;
rigidbody2D.velocity = transform.up * magnitude;

and this makes the ship fly as I want it to, but obviously it also cansels out the collision forces so I am back to square zero. Any help/ideas are welcome as I am quite stuck with this.

Thank you
Søren

1
The situation you describe is physically impossible. Every action has an equal and opposite reaction. You could I suppose give your ship a huge mass, so the apparent effect of a collision on it is minimised.Tony Hopkinson
I was afraid it might be physically impossible. Giving my ship a huge mass won't solve anything as I want the collisions to have a effect, I just want to combine it with a non-physical movement of my ship. I guess I have to implement my own fake-physics if I want this to work.Neigaard
Um the collisions will have an effect, the greater the mass of one object the greater the impact on the other. What you'd have to do though is perform two calculations. One to calculate the effect on the rotational velocity of your ship, the other to calculate the impact on the asteroid. Where physics breaks down is the laws of motion would differ for asteroid and ship. Perhaps this old game might trigger an idea. youtube.com/watch?v=HlPgB9rEkBg :)Tony Hopkinson

1 Answers

2
votes

It's easy as pie to achieve this:

you're really just trying to "bounce it around" (that is to say "left and right" .. "back and fore") as it continues to proceed upwards.

The simplest way in Unity is - just make the colliders triggers, don't actually use PhysX collisions. On each collision, apply a force (Unity's "explosive force" will be fine) in the relevant direction, left or right (perhaps with a little up-down variance).

Enjoy!