Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Joe0418 · Aug 21, 2011 at 07:59 PM · rotationrigidbodyvelocitydirectionmagnitude

Modify rotation based on passing rigidbody's velocity and direction?

Hello!

I've got a fairly complex scenario where I need to update an objects rotation speed and clockwise/counter-clockwise direction. The rotation is just about the Z axis as I've scripted my game to be 2D in the X and Y. I'm not using gravity but have created my own custom "orbiting" gravity (it's a slingshot physics/puzzle game in space).

Here's a picture of the scenario I'm trying to accomplish:




The black circle represents an object who's position doesn't change, but is rotating via a transform.RotateAround. The blue sphere is traveling with a rigidbody and velocity throughout the game area.

I already detect when the blue circle is close enough to affect the black circle's rotation, but how can I determine which direction the black circle needs to rotate in, and by what magnitude? I think it would relate to the blue circle's velocity, normalized vector, and position relative to the black circle- but I can't piece the three together logically.

Keep in mind the blue circle may come from any direction, not just from below.


What I've tried which sorta works but not smoothly/realistically:

I've been attempting to calculate which Cartesian quadrant the blue circle is in based on the black circle representing the origin. Then, I determine if the blue circle is moving faster in the X or Y direction, and spin appropriately. This doesn't always work and doesn't look "natural"... I'm not too good at linear algebra, so I'm unsure of any other way to achieve this? Please help!

Note: I can post scripts if needed (I'm using JavaScript), but they're a bit lengthy- I'll leave them out for now because I think I'm going in the wrong direction and don't want to mess anyone else up who may be in my position.

EDIT: Sorry about the pictures not showing up, they don't seem to want to cooperate =/

Comment
Add comment · Show 1
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 aldonaletto · Aug 22, 2011 at 12:12 PM 0
Share

This is too vague. I suppose that when the sphere passes within a certain distance, the black object must follow the sphere velocity - if the sphere velocity is 5 m/s and it's at 3 m from the object center, for instance, the object must rotate at 0.26 revolutions per second, which gives 5m/s at 3m.
But there are several other possibilities to consider. What should happen when:
1- The sphere passes at a large distance (nothing?)
2- The sphere hits the rotator (never happens?)
3- The object is rotating in one direction and the sphere passes in the opposite direction (follow the sphere velocity? reduces velocity?)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Aug 23, 2011 at 04:45 PM

Despite the description is somewhat vague about some points, I wrote a script which can serve to your purpose - or at least can show you how to do the necessary calculation (which isn't so easy as it may seem).
To test this script, follow these steps:
1- Create an empty object and attach the script below
2- Add a sphere colldier and set its radius to the distance the sphere can be detected
3- Create the rotor object and call it "Rotor"
4- Child the Rotor to the first object
It's ready. Whenever a rigidbody enters the trigger, the script calculates the distance it will pass from the rotor and sets the variable rps (revolutions per second) to a suitable speed (as the sphere "pushed" the rotor):

private var rotor: Transform; private var rps: float = 0; // revolutions per second

function Start(){ rotor = transform.Find("Rotor"); }

function OnTriggerEnter(hit: Collider){ if (hit.rigidbody){ // ignore non rigidbody objects var vel = hit.rigidbody.velocity.magnitude; // gets the sphere speed var dir = hit.rigidbody.velocity.normalized; // and its direction var start = hit.transform.position; // start is the sphere position var point = rotor.position; // point is the rotor center // find the nearest point to the rotor the sphere will pass through var tgPt = start + Vector3.Dot(point-start, dir)*dir; var radius = Vector3.Distance(tgPt, point); // distance from the rotor center rps = vel/(2*Mathf.PI*radius); // calculate rotations per second var cross = Vector3.Cross(dir, tgPt-point); // cross product will give the direction if (cross.z > 0) rps = -rps; // correct rotation direction if needed } }

function Update(){ rotor.Rotate(0, 0, 360*rps*Time.deltaTime); // rotate around Z axis }

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rigidbody slowing right down when colliding only when I'm rotating the velocity to always be going forward 1 Answer

checking angular magnitude 1 Answer

How to move rigidbody object towards a target object 0 Answers

How to apply relative force to Ball using another object? 0 Answers

leftover rb.velocity.z that causes magnitude to spike randomly 0 Answers


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