Need to make a ball bounce off a circle.

2 次查看(过去 30 天)
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 个评论
Chad Greene
Chad Greene 2015-5-20
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))

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by