x


Spaceship physics - adding resistance

Hi,

I don't know enough about PhysX (or actual physics!) so was wondering if someone could help me out.

I'm trying to create some simple spaceship controls on a rigidBody. I am doing this by using AddForce to add a direction vector and move the ship accordingly. The problem is (rather obviously), once I have added the force, it stays in effect endlessly. I obviously need to add some kind of resistance to the ship so that after I move in a direction, it slowly comes to a stop (which is specified by the resistance variable or whatever.)

Firstly, is using AddForce the wrong thing to do? Secondly, can anyone suggest a method in which I could apply resistance to my equation:

var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

if (directionVector != Vector3.zero) {
    var directionLength = directionVector.magnitude;
    directionVector = directionVector / directionLength;

    directionLength = Mathf.Min(1, directionLength);

    directionLength = directionLength * directionLength;

    directionVector = directionVector * directionLength;
}

gameObject.rigidbody.AddForce(directionVector);
more ▼

asked Nov 04 '10 at 02:05 PM

Disaster gravatar image

Disaster
482 48 57 67

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Every force has an equally strong reaction. Action is reaction.

If you hit something from left to right, you can hit it from right to left aswell using a timer or anything else depending on your desired result.

If you are in space and hit something, you will not see the real reactions in games. So just cheat and make a % of the force added to be added on opposite over a time period.

Velocity -= (Decelleration + Time.deltaTime);

more ▼

answered Nov 04 '10 at 02:23 PM

Proclyon gravatar image

Proclyon
1.4k 10 13 32

Where abouts would velocity come into play in the code sample I posted? I'm a little bit confused how I'd work that in.

Nov 04 '10 at 02:47 PM Disaster

Assuming your code above runs only when for example you're pressing a key, then when you're not pressing a key, drag will slowly reduce the velocity until you stop. I use similar code for my spaceship game, using ForceMode.Acceleration as the type of force added.

Nov 04 '10 at 02:50 PM runonthespot

I was giving you an explanation in physics. You are going to have an object and it is going to have a speed (E.a.) Velocity, which is distance over time.

You can measure it or just extract it from the rigidbody.

To do that just get the RB out of object with if (GetComponent() != null) { RigidBody myRB = GetComponent(); }

private float velocity = myRB.velocity;

Nov 04 '10 at 02:54 PM Proclyon

or in your sample above, Debug.Log(gameObject.rigidbody.velocity); (or something like that)

Nov 04 '10 at 02:55 PM runonthespot

Thanks, I think I have enough to go on for now :)

Nov 04 '10 at 03:14 PM Disaster
(comments are locked)
10|3000 characters needed characters left

Try setting the drag to something like 1, and just play around with it.

Angular drag is for slowing rotation speeds.

In actual space, you wouldn't slow down, because there is nothing to slow you down. But that might make a pretty crazy game.

And using AddForce is the right thing to do (if you get the desired result)

more ▼

answered Nov 04 '10 at 02:12 PM

Atnas1010 gravatar image

Atnas1010
1.1k 6 10 26

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5086
x1879
x1794

asked: Nov 04 '10 at 02:05 PM

Seen: 1966 times

Last Updated: Nov 04 '10 at 02:05 PM