Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by aidinz · May 09, 2017 at 05:51 AM · mathspherical

How to move on the surface of a sphere using spherical math? (Without Rigidbodies or raycasts)

Hi,

I want to be able to walk inside a imaginary sphere and facing the center of it. I know I can create the sphere, cast RayCast and inverse normal of the collision point or use Rigidbodies but I want to do it with math but I can't get my head around the math, specially the moving part.

To put it in another word, imagine super mario galaxy but inverse, being inside the sphere while still walking on the surface, like mario.

I've been searching for this for hours but can't find what I want. Thanks.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by elenzil · May 09, 2017 at 08:42 PM

one cheapo- way to achieve this sort of stuff has two parts which you would do every movement update (ie in Update()):

  1. move your player like normal. forget about the sphere. eg, if your player currently has velocity of (1, 2, 3), just move the player by (1, 2, 3).

  2. constrain the player's position back to the surface, and re-calculate velocity.

let's talk about step 2. in step 1, it's likely that the player will move off the surface of the sphere. so step 2 is to put them back on the sphere.

the math looks something like this:

 // unity-ish psuedo-code.
 // this assumes that the player is:
 // 1) originally on the surface of the sphere
 // 2) moving tangentially to the surface of the sphere. (not jumping/falling)
 // we'll use this value after constraining position to the surface.
 void updatePlayerMotion(Player player, float dT, Sphere world) {
     // stash the original player position & velocity
     Vector3 originalPos = player.position;
     Vector3 originalVel = player.velocity;
 
     // ordinary linear motion.
     // maybe this is done w/ unity physics instead. up to you.
     player.position += player.velocity * dt;
 
 
     // vector from the center of the world to the player position.
     // if the world is centered at 0, 0, 0, then this is just player.position.
     Vector3 worldCenterToPlayer = player.position - world.center;
 
     // scale that vector so that its the size of the sphere
     Vector3 constrainedPosition = worldCenterToPlayer.normalized * world.radius;
 
     // and offset it by the center of the world again.
     // if the world is at 0,0,0 this can be skipped.
     player.position = world.center + constrainedPosition;
 
     // okay, now the player position is on the surface.
     // let's deal with conserving surface velocity.
     // the goal of this section of code is to create a new velocity vector which:
     // 1. has the same magnitude as the original velocity
     // 2. is tangent to the sphere
     // 3. is going in the same direction, more or less, as the original velocity.
 
     // create a vector which is perpendicular to both the vector from the center of the world to the player
     // and also to the original velocity:
     Vector3 perpAxis = Vector3.Cross(worldCenterToPlayer, originalVel);
 
     // create a vector which is perpendicular to both our 'axis' vector and the radius vector.
     // this vector is going in the direction we want, but is likely the wrong size.
     Vector3 tangent = Vector3.Cross(perpAxis, worldCenterToPlayer);
 
     // re-scale the tangent vector so it's the same magnitude as the original.
     player.Vector3 = tangent.normalized * originalVel.magnitude;
 }


Comment
Add comment · Show 3 · 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 xingway · Jun 11, 2018 at 04:51 AM 0
Share

hi, elenzil. I have a question. what if i want to change the velocity with keyboard controls ? it seems to only work when the velocity is fixed...

avatar image elenzil xingway · Jun 11, 2018 at 04:06 PM 0
Share

hm, changing velocity should not be a problem. the quality of the approach will degrade as the velocity gets to be a sizable fraction of the radius of the sphere. like if you're traveling 100 units per frame and your sphere is only 200 units in radius or something.

avatar image xingway elenzil · Jun 12, 2018 at 01:11 AM 0
Share

i've made some progress, thanks for your help

avatar image
0

Answer by aidinz · May 10, 2017 at 01:17 PM

This question was waiting so long that I went to forums and it got solved there. Full code:

https://forum.unity3d.com/threads/how-to-move-on-the-surface-of-a-sphere-using-spherical-math-without-rigidbodies-or-raycasts.470041/#post-3063122

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Moveing on a Sphere in direction of Camera with Math 0 Answers

help with my orbit camera script 0 Answers

Simple Maths Problem - Help! 1 Answer

Math exponential number conversion 1 Answer

Any Good References for Game Math 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