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 /
  • Help Room /
avatar image
0
Question by D43DB33F · Jul 02, 2017 at 02:44 PM · raycastvelocityplanepoint

Determining point coordinates on a plane for a known normal

Hello, I'm currently trying to implement realistic boat physics. So far I got the buoyancy right, but I'm a bit stuck with drag.

I would like to have a "wall" of ray casts constantly "ahead" of the collider (i.e in the direction it moves) so that I can cast those ray casts into the collider, in the opposite direction of the velocity, and determine, foreach ray cast, a drag force to be applied on the hit triangle of the collider.

My problem is that, I do have the direction of the ray casts, which is the opposite of the velocity, but I don't have the ray casts starting points (i.e the points belonging to the "wall" plane) and don't know how to compute them. Below is an image to give you a better idea of what I mean alt text

The ball is my boat (which isn't necessarily moving forward). The red arrow is its velocity. The green lines are (some of) the ray casts start from the plane and (maybe) hitting the collider. What I would like to compute is the starting points of these ray casts.

Could someone please put me into the direction ? Thank you very much.

EDIT : I thought about creating a new Transform object. Position that transform based on the opposite velocity direction and on a position far enough "ahead" of the collider so that all ray casts start outside of the collider. And then determine ray cast starting points in local coordinates of that transform and then convert them to world space using that transform. Could this work ? Thanks.

raycast-wall.jpg (101.1 kB)
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
0
Best Answer

Answer by D43DB33F · Jul 02, 2017 at 04:05 PM

Ok I found a solution ! The idea is to create a new transform using a dummy empty game object. Then set the position of that transform by adding a large enough distance to the boat's position, than make that transform look at the boat. From there it is possible to compute the ray casts starting points locally to that transform, and then transform them into real world coordinates.

The code below only shows how to compute the ray casts starting points, not how to compute the drag forces (not done yet).

 private void ProcessDragForces()
 {
     rayCastStartPoints = new List<Vector3>(rayCastCountX * rayCastCountY);
     rayCastHitPoints = new List<Vector3>(rayCastStartPoints.Count);

     // The wall must be at least as big as the collider projection on that wall can be,
     // regardless of its rotation. Using the diagonal of the bounds ensure that.
     float wallSize = boatMeshCollider.bounds.size.magnitude;
     float halfWallSize = wallSize / 2f;

     // Divding the raycast wall into multiple cells in order to determine the starting
     // point for each ray cast locally to the ray cast wall's transform.
     float cellSizeX = wallSize / rayCastCountX;
     float cellSizeY = wallSize / rayCastCountY;

     Vector3 cellOffset = new Vector3(-halfWallSize + cellSizeX / 2f, -halfWallSize + cellSizeY / 2f, 0f);

     // Cast in the opposite direction of the magnitude.
     Vector3 raycastDirection = -boatRigidbody.velocity;

     raycastWallTransform.position = gameObject.transform.position + boatRigidbody.velocity.normalized * boatMeshCollider.bounds.size.magnitude * 2f;
     raycastWallTransform.rotation = gameObject.transform.rotation;
     raycastWallTransform.LookAt(gameObject.transform);

     for (int x = 0; x < rayCastCountX; ++x)
     {
         for (int y = 0; y < rayCastCountY; ++y)
         {
             Vector3 localRayCastStartPoint = cellOffset + new Vector3(x * cellSizeX, y + cellSizeY, 0f);
             Vector3 rayCastStartPoint = raycastWallTransform.TransformPoint(localRayCastStartPoint);

             rayCastStartPoints.Add(rayCastStartPoint);

             RaycastHit raycastHit;
             if (Physics.Raycast(rayCastStartPoint, raycastDirection, out raycastHit) && raycastHit.collider == boatMeshCollider)
             {
                 rayCastHitPoints.Add(raycastHit.point);
                 boatRigidbody.AddForceAtPosition(new Vector3(10f, 10f, 10f), raycastHit.point);
             }
         }
     }
 }
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

133 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I move an object in the direction another object is facing. 1 Answer

How to limit the rotation speed of my player 0 Answers

Problem with Vector3.MoveTowards 2 Answers

Get Direction From Raycast 0 Answers

Plane.RayCast not hitting at all 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