Sign in to follow this  

Block Breaker , ANGLE calcution HELP!

This topic is 2810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i try to make brick breaker game. i got 1 problem that the ball doesn't bounce correctly i thought for 2 days and i don't get the answer that i can understand from internet i use this fomula ball.x = sin(angle)*speed; ball.y = cos(angle)*speed; angle is in radian value; and whatever the ball hits, the angle will mutiply by minus which i think it gonna work but when i hit the wall not the brick the ball doesn't bounce and move like parallel. can someone tell me how to calculate the opposite site properly? Please. this is my assignment that i have to finish now

Share this post


Link to post
Share on other sites
you don't negate the angle, but you negate either x or y depending on what wall you hit.

if(hit left or right wall) ball.x = -ball.x;
if(hit up or down wall) ball.y = -ball.y;
angle = atan2(x, y);

Share this post


Link to post
Share on other sites
Quote:
Original post by oliii
you don't negate the angle, but you negate either x or y depending on what wall you hit.

if(hit left or right wall) ball.x = -ball.x;
if(hit up or down wall) ball.y = -ball.y;
angle = atan2(x, y);


thanks for your help, i gonna try it
but i just want to learn.

i don't understand about what you wrote "angle = atan2(x, y);"

atan2??what is it? arctan? 2??
how can i use it?

Share this post


Link to post
Share on other sites
Quote:
Original post by GregMichael
Try googling "atan2"...it's quite helpful.


well,
atan2 will make me get the opposite angle
it the reverse value of tan.

however, i try to apply for the program, it won't work


angle = atan2(ball.y,ball.x); // atan2 value is already radian
ball.x += sinf(angle)* speedX;
ball.y += cosf(angle)* speedY;

when the wall hit up or down -> speedY *(-1)
when the wall hit left or right -> speedX *(-1)

it doesn't work
when the ball hit block/brick , the ball move backward and
not collide to the bar....confused

i want to make this game work by some physic or math
because i can apply for another game too

please help.

Share this post


Link to post
Share on other sites
you don't have to bother with angles. just negate the x or the y when you hit a wall or a side of a brick. The angle can be useful at the initial start to initialise the x and y, but after, it's overkill.

Share this post


Link to post
Share on other sites
Quote:
Original post by oliii
you don't have to bother with angles. just negate the x or the y when you hit a wall or a side of a brick. The angle can be useful at the initial start to initialise the x and y, but after, it's overkill.


but it the ball hit the edge of the brick......
how can i do?
i understand your point and i agree with you
however, the next step that i want to try is billard game
so i want to know how to get the reflect angle

Share this post


Link to post
Share on other sites
When you do billiards, you'll also be better off not using angles. If you have an incoming vector and a normal vector and you want to compute a reflected vector, you can do that with simple vector arithmetic. The formula is something like this:
reflected = incoming - 2*dot_product(incoming, normal)*normal


Share this post


Link to post
Share on other sites
I don't know your maths level, but vector maths are pretty easy. for the very basics, see here.

Vector, triginometry, arithmetics, algebra, calculus, geometry, cartesian and polar coordinates, it's pretty much A-level maths, and that's 90% of collision detection and physics (the simple kind!) sorted. Vectors, matrices and geometrical properties being used extensively.

Share this post


Link to post
Share on other sites

reflected = incoming - 2*dot_product(incoming, normal)*normal


That equation is actually applied when you hit a vertical or horizontal wall. Say the wall normal is vector(1, 0) (the left wall),

result_velocity = velocity - 2 * dot(velocity, normal) * normal
result_velocity = velocity - 2 * (velocity, vector(1, 0)) * vector(1, 0)
result_velocity = velocity - 2 * ((velocity.x * 1) + (velocity.y * 0)) * vector(1, 0)

=>

result_velocity.x = velocity.x - 2 * velocity.x * (1)
result_velocity.y = velocity.y - 2 * velocity.y * (0)

=>
result_velocity.x = -velocity.x
result_velocity.y = velocity.y

it's basic geometry (and actually demonstrated optics for mirrors and stuff).

Share this post


Link to post
Share on other sites
Quote:
Original post by oliii

reflected = incoming - 2*dot_product(incoming, normal)*normal


That equation is actually applied when you hit a vertical or horizontal wall. Say the wall normal is vector(1, 0) (the left wall),

result_velocity = velocity - 2 * dot(velocity, normal) * normal
result_velocity = velocity - 2 * (velocity, vector(1, 0)) * vector(1, 0)
result_velocity = velocity - 2 * ((velocity.x * 1) + (velocity.y * 0)) * vector(1, 0)

=>

result_velocity.x = velocity.x - 2 * velocity.x * (1)
result_velocity.y = velocity.y - 2 * velocity.y * (0)

=>
result_velocity.x = -velocity.x
result_velocity.y = velocity.y

it's basic geometry (and actually demonstrated optics for mirrors and stuff).



thanks for your advice.
truely, geometry is my weak point in math.

the way that you explain like
in the end, i just use
result_velocity.x = -velocity.x
result_velocity.y = velocity.y

that is the same result as
result_velocity = velocity - 2 * dot(velocity, normal) * normal
result_velocity = velocity - 2 * (velocity, vector(1, 0)) * vector(1, 0)
result_velocity = velocity - 2 * ((velocity.x * 1) + (velocity.y * 0)) * vector(1, 0)

this calculation?
"result_velocity = velocity - 2 * dot(velocity, normal) * normal"
this formula, i never study before
even though your link which you told me,
i still don't get it
and i have no idea how it gonna be in c program

anyway, thanks



Share this post


Link to post
Share on other sites

This topic is 2810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this