Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by max1975 · Feb 28, 2014 at 11:27 PM · rigidbodyrelative movementspaceflight

Relative Velocity for Spaceship Braking

Hello. I'm building a spaceflight simulator game, and having a maddening problem.

I want my "Brake" function to apply thrust to my ship (which is a Rigidbody) to match the velocity of another object, ReferenceBody (which is not necessarily a Rigidbody, because it might be something huge like a planet that needs a mass>10).

This seems like it should be very simple.

Relative Velocity = My Velocity - Reference Velocity, Thrust Vector = -Relative Velocity

But when I run the game and step on the brakes, ReferenceBody keeps getting farther away, even though after some time holding down the breaks, my velocity reads as equal to ReferenceBody's. Braking slows me down, but then I appear to be stopped in absolute coordinates, not relative ones.

I must be doing something really dumb. I've stripped the operations down to their simplest form and the problem won't go away, but I don't see my mistake. Hopefully it will be obvious to someone else?

Relevant bits of code here:

 //ReferenceBody Class
 
     void FixedUpdate() {
 //everything here is a Vector3
         position = _transform.position;
         velocity = (position - lastPosition);
         lastPosition = position;
     }
 
 //Ship Class
         void FixedUpdate ()
         {
                 _velocity = rigidBody.velocity;
                 _position = rigidBody.position;
                 _relativePosition = (rigidBody.position - ReferenceBody.position);
                 _relativeVelocity = (rigidBody.velocity - ReferenceBody.velocity);
         }
 
         public void Brake ()
         {
                 Vector3 _reverseVector = -_relativeVelocity;
                 rigidBody.AddForce (_reverseVector);
         }

Thanks for any help!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by max1975 · Mar 01, 2014 at 07:37 AM

I was finally able to fix the problem by using _transform.position-_lastPosition in the ship class to determine my absolute velocity, instead of Rigidbody.velocity.

I still don't understand why this worked. Was Rigidbody.velocity misreporting? Or maybe it is calculated differently, leading to a discrepancy with the ReferenceBody which was not a Rigidbody. But the discrepancy was much larger that I would expect from two different programs trying to arrive at the same number.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Mar 01, 2014 at 07:42 AM 0
Share

You could just have set the velocity to match I guess. I'm wondering if the problem is where Brake is called perhaps?

Also the calculation of the position of the reference body will probably not be correct - the problem being that physics runs before everything else (or not at all) and so if the reference body is not moved by physics or in its FixedUpdate there will be a frame inaccuracy.

avatar image max1975 · Mar 01, 2014 at 08:48 AM 0
Share

Setting the velocity to match sort of worked (I think; it was in one of many iterations of trying different things) but then when I took my foot off the brakes my previous velocity was restored and I'd go slam$$anonymous$$g into the wall of the station where I'd just gently landed.

It almost seems like the physics engine and Transform are actually using different units. After fixing the brakes as described above, I noticed all my velocities were reading much slower than they had previously (though I seemed to be moving the same speed). So I decided to make the ReferenceBody a Rigidbody also, use the Rigidbody.velocity for all the calculations, and solve whatever problems that causes as they arise.

Brake is called (if a key is down) from FixedUpdate() on a controller class.

Thanks for the reply!

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make a child stay with parent object? 1 Answer

Relative Movement & Momentum problem - Space sim 1 Answer

How can I roll a sphere relative to the camera's Y axis but not X axis? 1 Answer

Reset position for rigidbody2d 1 Answer

How do i make local velocity?,How do i make velocity move the player the way the object is facing? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges