Need to make a ball bounce off a circle.

조회 수: 3 (최근 30일)
brian grove
brian grove 2015년 5월 19일
댓글: Chad Greene 2015년 5월 20일
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