Need to make a ball bounce off a circle.
Show older comments
I have a pong game that I want to add a function circular bumper to. I already have a way of detecting when the ball enters the circle but now I need to figure out how to make it bounce off the wall of the circle.
My function for bouncing the ball off the walls is
function bounce (tempV)
%normalize vector
tempV(1) = tempV(1) ./ (sqrt(tempV(1)^2 + tempV(2)^2));
tempV(2) = tempV(2) ./ (sqrt(tempV(1)^2 + tempV(2)^2));
ballVector = tempV;
and I feed the function something along these lines...
bounce([ballVector(1), -1 * (Y_FACTOR + abs(ballVector(2)))]);
but I can't get it to work for the circle... any ideas?
1 Comment
Chad Greene
on 20 May 2015
We don't have enough information about your variables. Whether you're giving a talk or writing a report or posting on a forum, always explicitly describe any variable you ever show. If a variable is not relevant, do not show it to us. If a variable is relevant, describe it.
To increase your chances of getting helpful responses, provide a picture of what you're trying to do and some simple working code that we can tinker with.
When I look at your code, all I see is not related to your question, but you can replace
sqrt(tempV(1)^2 + tempV(2)^2)
with
hypot(tempV(1),tempV(2))
Answers (0)
Categories
Find more on Just for fun in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!