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
2
Question by MasterTakeshi · Mar 11, 2017 at 10:15 AM · collisionmathvectorbreakout

Creation of an algorithm that deflects the ball correctly at all possible paddle angles in a Breakout style game

Hi, my name is Maikel and I have been using Unity for a bit now. Mostly learning purposes and small projects. My latest project is a Breakout styled game. I have ran into a problem with the design of the paddle-ball collision system and I just can't figure it out, hence my ask for your help.

The unique selling point/ feature of the game is that it has circular movement. The player can move around a circle and has to defend an inner circle. This makes it that all systems: movement and such are a little more complex.

So far I have been able to handle it but I can't seem to make the ball-paddle collision work properly. The design I'm hoping to implement correctly is that when the ball hits the right side it is deflected to the right and when its hits the left side it is deflected to the left.

I have implemented a function that uses the position of the collision to determine how to deflect the ball. I have read much about the use of this system and it works quite well but I can't figure out to alter it to my needs. The function reflects the incoming velocity vector of the ball with the collision contact normal of the paddle.

It negates or does not negate the y value of the velocity based on the position of the ball.

 private void DirectionalMovement(Vector2 ballPos,Collision2D collider)
     {
         Vector2 racketPos = collider.transform.position;
         float racketWidth = collider.collider.bounds.size.x;
         float x  = (ballPos.x - racketPos.x) / racketWidth;
 
         Vector2 dir = new Vector2(x, 1).normalized;
 
         if (this.transform.position.y <= 0)
         {
             dir = new Vector2(x, -1).normalized;
         }
      
         ballRigidbody.velocity = dir * speed;
     }

This works, but not good enough. The main problem with this approach is that it leaves, what I call, "dead-zones", some areas that the ball can't reach when bouncing of the paddel. I have illustrated them in the picture below, along with some extra information.

I have been thinking about the problem a lot and can't seem to come up with a solution to the problem or a system that would work. I'm not a pro when it comes to geometry and vectors, though I can understand them I haven't been able to use them to solve this problem.

Can someone guide me to the right direction for solving this specific problem. I am not asking for a clear-cut answer or algorithm. Just a general direction for me to look , because I seem to be lost. Thanks in advance! (Pardon my grammar if it is incorrect in some places, English is not my native language)

alt text

collision.jpg (158.0 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
2
Best Answer

Answer by Bunny83 · Mar 11, 2017 at 11:39 AM

Well, your problem is that you use an "absolute reflection angle". In games where the paddle is always oriented horizontally that's not a big deal. However when you rotate your paddle it makes not much sense.

You may want to calculate everything in local space of the paddle:

 Vector2 ballLocalPos = collider.transform.InverseTransformPoint(ballPos);
 float x  = (ballLocalPos.x) / racketWidth;
 Vector2 dir = new Vector2(x, 1);
 if (ballLocalPos .y <= 0)
     dir.y = -dir.y;
 dir = collider.transform.TransformDirection(dir).normalized;
 ballRigidbody.velocity = dir * speed;

It should be obvious that this approach in general doesn't result in a realistic reflection (ignoring the fact that a straight paddle is handled like a curved paddle). Since you calculate a fix direction in which the ball will bounce off, it doesn't matter at which angle it arrives.

From your drawing it looks a bit like Shield Defence ^^. Though that game uses the correct angle of reflection.

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 Scribe · Mar 11, 2017 at 11:54 AM 1
Share

Was going to answer similarly, I had started drawing up an image in case it is helpful, the important part to note is; based on your desired 'inco$$anonymous$$g' and 'outgoing' vectors, the important vector to extract is the one that is in the middle of them both as this acts as the reflection normal as shown by the grey line in the first image! +1 for both of ya,good job added some images to the question @$$anonymous$$asterTakeshi alt text

reflection.png (49.0 kB)
avatar image MasterTakeshi · Mar 12, 2017 at 11:09 PM 0
Share

I hadn't thought about the local and global concept because I hand't used it in such way before. This solution works very well for my use case. Thank you kindly for this valuable insight!

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

Unrealistic bounce 2 Answers

How to avoid changing transform.parent? 2 Answers

How to split velocity into two smaller vectors at a chosen degree? 1 Answer

Vector direction after collision 1 Answer

Object thrusted violently in opposite direction on collision 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