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
1
Question by skail · Dec 19, 2011 at 07:54 PM · javascriptphysicsangle

Help with bounce angle and velocity change

Hi, I am trying to write code to cause a ball to bounce off of the walls of an oval shaped arena. I dont want to use the built in physics engine because that dose a bunch of other things that I do not need. I am using the variables of xDirection and zDirection as the values of its x and z speed as well as using the value velocity for it's total speed. I figure I need the program to calculate the normal of the surface I hit and then somehow "flip" my xDirection and zDirection over that. I am not really sure how to proceed though. If anyone can help me to get this work it would be greatly appreciated, my math skills seem to be failing me and I can't seem to find all the proper info online (I must not be looking up the right things).

EDIT: OK so I got something working now. I am at least able to draw the correct lines of where it should go, except that after the first bounce it starts behaving weird, and going bounceing nearly back on itself, can anyone help me figure out why?

 var xDirection:float;
 var zDirection:float;
 var velocity:float;
 function Start()
 {
 print ("start");
     zDirection = velocity;
 }
 function Update () 
 {
     transform.position.x += xDirection * Time.deltaTime;
     transform.position.z += zDirection* Time.deltaTime;    
 }
 function OnCollisionEnter(collision : Collision) 
 {
     if(collision.collider.tag == "wall")
     {
         // Debug-draw all contact points and normals
         for (var contact : ContactPoint in collision.contacts) 
         {
             var reflection = Vector3.Reflect (transform.position, contact.normal);
             var reflectionNorm = reflection.normalized;
             Debug.DrawRay(contact.point, contact.normal, Color.green, 100);
             Debug.DrawRay(contact.point, -transform.position, Color.red, 100);
             Debug.DrawRay(contact.point, reflection,Color.blue,100);
             print("Contact point: "+contact.point);
             print("Cantact normal: "+contact.normal);
             print("Reflect: "+reflection);
             print("Position: "+transform.position);
             print("Normalized Reflection: "+reflectionNorm);
             print("Angle: "+ Vector3.Angle((contact.point - transform.position),transform.forward));
             xDirection = velocity * reflectionNorm.x;
             zDirection = velocity * reflectionNorm.z;
         }
     }    
 }
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
4
Best Answer

Answer by aldonaletto · Dec 20, 2011 at 04:41 AM

The Vector3.Reflect example in the docs is terrible, the worst possible - this function is intended to reflect vectors, not positions. I would use a Vector3 as the velocity, and always zero its y component to avoid small errors that could accumulate and make the object "escape" from the horizontal plane. I would also use FixedUpdate, because collisions are detected in the physics cycle. My script would be something like this:

var startVel: float = 5; private var velocity: Vector3;

function Start(){ // start going in the z direction velocity = Vector3(0, 0, startVel); }

function FixedUpdate(){ velocity.y = 0; // make sure velocity is strictly horizontal // move the object (velocity is in world space): transform.Translate(velocity * Time.deltaTime, Space.World); }

function OnCollisionEnter(coll: Collision){ if (coll.collider.tag == "wall"){ var hit = coll.contacts[0]; // the first contact is enough hit.normal.y = 0; // keep normal in the horizontal plane Debug.DrawRay(hit.point, hit.normal, Color.green, 0.3); // draw green normal Debug.DrawRay(hit.point, -velocity, Color.red, 0.3); // draw red "in" velocity velocity = Vector3.Reflect(velocity, hit.normal); // reflect the velocity Debug.DrawRay(hit.point, velocity, Color.blue, 0.3); // draw blue "out" velocity } }

Anyway, you may have weird direction problems because Translate doesn't process collisions correctly - the rigidbody just "appears" inside a collider, and receive the "penalty force", which may not be in the correct direction.
Only physical methods (applying forces or changing rigidbody.velocity) do it the right way. A simple version using physics could be something like this:

var startVel: float = 5;

function Start(){ // start going in the z direction rigidbody.velocity = Vector3(0, 0, startVel); rigidbody.constraints = RigidbodyConstraints.FreezePositionY; // avoid changes in Y }

Comment
Add comment · Show 4 · 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 skail · Dec 20, 2011 at 05:19 AM 0
Share

Thank you sooo much. it works like a charm.

avatar image cj_coimbra · Jan 27, 2013 at 01:06 AM 0
Share

This is old as hell, but just saved my sanity trying to solve a problem similar to this.

avatar image kreso · Mar 22, 2014 at 08:04 AM 0
Share

@aldonaletto - Seems like where you attempt to provide an example that uses rigidbody.velocity ins$$anonymous$$d of 'translate' got cut off. Any chance you might want to repost it? I know it's been a while - but the topic is still relevant I guess.

Appreciate it!!

avatar image aksh2143 · Sep 12, 2015 at 12:13 PM 0
Share

@aldonaletto im trying to write script in C# but in your answer, there is a code "var hit = coll.contacts[0]; // the first contact is enough" i dont understand what is that. you havent defined its type,and what is coll.contacts[0] ???

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

8 People are following this question.

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

Related Questions

Need a simple car physics script working on unity 5? 1 Answer

Return Angle as Vector 3 2 Answers

Possible Fall Damage OnCollisionEnter Won't Work! 0 Answers

Can I use Javascript (Unityscript) for Box2D for Unity or Farseer Physics? 1 Answer

Calculate the normal of a collider 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