PDA

View Full Version : physics about ball+game


georgew
03-07-2008, 11:33 AM
Hi,

I try to make the arcanoid game an I have some questions about the ball movement. I want when the ball hit paddle ALTERS the angle of reflection based on the position of the ball relative to the centre of the paddle.

How can I success that? I have try a lot about it, but I cant.

Can somebody give the code about it and explain me?

P.S. I have change the first post of the tread.... SORRY. I did that to make it more understable....

ASWC
03-22-2008, 02:21 PM
Can you describe the problem or what you are trying to do and can't for some reasons?

georgew
03-22-2008, 05:15 PM
Well, I want make the code when ball hiT paddle. I want the ball alters the angle of reflection based on the position of the ball relative to the centre of the paddle.

I I use this....

xspeed=8
yspeed=-8

if (ball.hitTestObject (paddle)){
yspeed=-yspped
}

the ball do't change angle....

So, what I must do?

ball.y=yspeed


...the angle of ball movement don't change.

So, what can I do?

rrh
03-24-2008, 06:18 PM
There's a function:
Math.atan2(y,x); Very good for getting the angle.

But then what kind of change do you want for the ball? Some arkanoid imitators just use the current speed to the paddle to change the ball's speed, which is the simpler approach. But I think akanoid proper alters the angle of reflection based on the position of the ball relative to the centre of the paddle.

Math.sin(angle)*distance and Math.cos(angle)*distance will get you the x and y values from the angle.

rrh
03-27-2008, 10:03 PM
tan is the opposite of atan.

This thread has some angle functions:
http://www.actionscript.org/forums/showthread.php3?t=156007&highlight=angle&page=2

georgew
04-03-2008, 11:46 AM
Can somebody give me a help?

Sorry, but I can't find the soluttion...

Durnus
04-03-2008, 06:37 PM
Help us help you. What is the problem? You haven't really described what you want, you have already been answered.

rrh
04-03-2008, 08:11 PM
Here's a thread I found with a suggested approach, which is simpler than what I initially thought of:
http://delphigamedev.com/forums/viewtopic.php?t=259

It's not Actionscript, but the principle is the same.

georgew
04-04-2008, 12:24 PM
Well, I decide to use this actionscript code:



if (ball.hitTestObject(paddle)) {//if ball hit paddle

var distanceX,distanceY,distance3,angle;

distanceX=Math.abs(ball.x-player.x);// I find adjacent
distanceY=Math.abs(ball.y-player.y);// I find opposite
distance3=Math.sqrt((distanceX^2)+(distanceY^2))//I find Hypotenuse

angle=Math.atan2(distanceY,distanceX)

yspeed=-(Math.sin (angle)*8)
xspeed=Math.cos(angle)*8

}


But, with this code the angle sometimes is very small ,so the ball goes too sidelong...
Also, the xspeed is always, so the ball goes always right...

So, how can I fix this code...

Help about your help.....

rrh
04-04-2008, 03:12 PM
The first thing that jumps out at me is the abs() which is why it would always go to the right. If you want to flatten the curve, you could get the y distance from ball.y-player.y+50 or something.

Also, maybe you could incorporate the angle of the speed of the ball.

Durnus
04-04-2008, 06:47 PM
Add -90 to the angle before applying it.

Also, can you really use ^ for powers?!? I've needed that... sorely.

georgew
04-04-2008, 06:52 PM
THANKS, A LOT rrh!!! Using the code with abs solved my problem. About the second problem the solution is to add to the ball.y-palyer.y a number...

I want also know how can keep the speed of ball in the same rate...

If I use this:

yspeed=-(Math.sin (angle)*8)
xspeed=18- Math.abs(yspeed)

It keep the speed in the same rate, but it is true...??
Or it change in false way the angle?

rrh
04-04-2008, 11:14 PM
Using the code with abs solved my problem.Do you in fact mean using it without abs() ?

To keep it the same speed, you use what you had before, only multiply it by the speed you want where you have *8. You can find the speed you want with the hyp=sqrt((o^2)+(a^2)) formula, only for your speeds instead of distances.