0

I am trying to make a game where i got a ball with an arrow rotating around it. The ball starts as immovable, but when i hit a button i launch it to the direction the arrow is pointing at. Player will have the option mid air to press another button to make the ball "sticky" so when it hits a wall it will stick to the surface and repeat the process till he gets to the top of the level.

Aside from that, i want to give the option to the player to not stick to the wall if he doesn't press that button and instead bounce off the wall but when he does that, the player should speed up with every bounce, giving the option to either play it safe and slow or try to go fast getting more points as he does.

For the early prototyping i used force to move the player up every time he launches but i am not sure how i can make him speed up every time he bounces off the wall. It feels to me like a math problem more than it is a coding challenge. What i am thinking is that i have to find the angle on which player hits the wall, and add force according to that towards the direction he is supposed to follow after the collision.

Sadly i am not that good with trigonometry (working on it though). I am thinking that i might need to use a formula containing sin, cos, tan formulas but i m not sure how to do it. Any help is much appriciated! If you need more information on it please tell me and i ll be happy to provide.

Edit: After the first reply to this question i also found out those links that dive deeper into the subject. I m gona link them here for people that have the same issue.

Bouncing a ball off a wall with arbitrary angle?

http://www.3dkingdoms.com/weekly/weekly.php?a=2

THK
  • 38
  • 6
  • 1
    Do you know [physics material](https://docs.unity3d.com/Manual/class-PhysicMaterial.html)? – shingo Oct 06 '22 at 08:23
  • @shingo Yes i do, but i don't think this is the right approach for this. I guess its easy to try it out though. Edit: While with full bounce material it feels better i still need to apply extra force after each hit with the wall for it to keep going upwards each time with more strength. – THK Oct 06 '22 at 08:26
  • Why do you think so. It's a physics game isn't it? If so move the ball with physical methods, mathematics should be left to physics engine. – shingo Oct 06 '22 at 08:33
  • 1
    To add extra force, use `AddForce` when the ball collides with the wall, you can get the forward direction with its `velocity`, so you don't have to calculate the direction by yourself. – shingo Oct 06 '22 at 08:36
  • @shingo Now thats what i m talking about!! Thanks a lot this is actually a very good idea. I ll test it out right now. – THK Oct 06 '22 at 08:44

2 Answers2

1

If your ball has velocity vector V=(vx,vy), then after bouncing from standing surface with normal N, ball's new velocity is

V' = V - 2 * N * (V.dot.N)

where dot is scalar product of vectors (vx*nx+vy*ny)

Particular cases: bouncing from vertical wall (N=(+-1, 0)) causes reversing of vx-component, vy component remains the same. V' = (-vx, vy)

bouncing from horizontal wall (N=(0, +-1)) causes reversing of vy-component, vx component remains the same. V' = (vx, -vy)

Note I recommend to work in velocity vector components, and use angles only when they are really needed.


If you need to calculate bouncing from moving bat, it is worth to change stationary coordinate system to moving one, connected with the bat, find reflection in that system, and revert to stationary system.

MBo
  • 77,366
  • 5
  • 53
  • 86
  • Hey thank you very much for this reply! I ll need some time to process this. I guess today is going to be the "back to highschool" day of studying geometry again! – THK Oct 06 '22 at 08:18
  • Whilst fine theory, there is no need to _roll-your-own_ physics in Unity. This isn't say XNA. –  Oct 06 '22 at 12:20
0

Ok so problem was solved!

I want to thank both @MBo and @shingo for their contribution to the solution. While the answer of Mbo solved the problem via trigonometry and gave me nice material to study and figure out how things work, i followed shingo's advice in the comments of my question and managed to do it without diving deep into math.

So basically what he said, and what i did, was to use Unity's physics engine and let the ball hit the wall. After the ball bounces off the wall, it gets a new Velocity vector towards the direction it would go based on physics. I then created an OnColllisionExit check, and when the ball stops colliding with the wall, i AddForce to it to the direction of its new Velocity Vector3.

Works like a charm!

Thank you all for your contributions!

THK
  • 38
  • 6