I am working on a game where you control a bat that goes left and right. There are 4 bats. The top ones are controlled with left and right and the side ones are controlled with up and down. I need help on making the bounce physics. Ie:
When ball hits bat, it reverses direction and stays same speed
When ball hits bat (when bat is moving) ball may bounce differently and perhaps gain speed.
I have looked everywhere for this code, but can't find it and I have no idea on how to create it. I am only beginning to use actionscript.
Need more information to make any code for that...
1) in what direction off the bat do you want the ball to move (if you hit the left of the bat do you want the ball to go to the right, or to the left)? Do you want the release angle to be relative to the distance from center the ball hits the bat.
2) in which instances do you want the ball to increase speed, ie:
a) if the bat is moving over a given speed the ball increases.
b) if the ball hits the bat closer to the edge will it go faster? and closer to center it slows down.
c) do you want to use some sort of a bonus to alter the speed dynamics.
I'm sure you see what I mean. If you supply it in english it should be simple to make the code, after all it is just angles.
1) I am not sure I understand you there. When I say bat, I mean a platform that translates from side to side or up and down. (The ones on the side go up and down.) I want the ball to bounce off of it like it would in a normal breakout game.
2) I want it to increase speed when the bat is moving and it hits the bat
I also want it to slightly increase every time it is hit just to make it harder. The goal of the game is to keep the ball in the screen. You have four platforms to control to keep it in the level.
Ok for clarification... you have four bats on a two dimentional baord?;
One at the bottom?
One at the top?
One at the left?
One at the right?
There are no obsticles in the center?
When the ball hits the bat, the angle remains constant?
When the ball hits the bat and it is moving the ball goes faster?
When the ball hits the bat and it isn't moving, it remains at the same speed?
One at the bottom? Controlled with left and right.One at the top? Controlled with left and right.
One at the left? Controlled with up and down.
One at the right? Controlled with up and down.
There are no obsticles in the center? Correct
When the ball hits the bat, the angle remains constant? What do you mean angle?
When the ball hits the bat and it is moving the ball goes faster? Yes
When the ball hits the bat and it isn't moving, it remains at the same speed? This one is harder to explain. It gets a little faster every time it gets hit regardless of anything else. If the bat is moving and it hits the bat, it will get a little faster. The ball will also get a little smaller if hit by the bat. Eventually it will dissapear and give the user points, but I can work out that part.
I'll do this in a few installments cos there is a lot of it.
Right, to give you a chance to write the code yourself this is everything you need to take into account...
You need a speed variable and direction handlers.
_global.speed = 1 // this would be the start speed.
_global.leftRight = 0 // start direction. (-89 to 89)
_global.upDown = 0 // start direction. (-89 to 89)
You don't need to act on every bat seperatley as they are all the same size from the edge.. as i don't know that size I have used batWidth.
batWidth = 20; // just a guess
function batCheck () { // every time the ball moves this needs to be called
if (ball._x == batWidth) {
checkLeftBat();
}
if (ball._x == Stage.width - batWidth) {
checkRightBat();
}
if (ball._y == batWidth) {
checkTopBat();
}
if (ball._y == Stage.height - batwidth) {
checkBottomBat()
}
}
function checkLeftBat() {
if (leftBat._y >= ball._y) { //check if the bat is touching the ball
if (leftBat._y + batHeight <= ball._y) { //same again
temp = ball._y - leftBat.Y; // get the difference between the bat top and the ball.
_global.updown = Math.round(temp / batHeight * 188) - 89; // This sets the angle of the ball.
_global.leftRight = -_global.leftRight; // reverses the direction.
}
}
else {
killplayer();
}
}
you will need to do this for each of the bats, changing the xs and ys respectivly.
Last edited by deldelfarrom; 12-10-2006 at 03:10 PM.
Reason: adding code
I am sorry but I am a noob at this. Here is the code I have for the bats:
onClipEvent (enterFrame)
{
moveSpeed=8;
{
if (this._y<320)
if(Key.isDown(Key.DOWN))
{
this._y += moveSpeed;
}
}
{
if (this._y>80)
if(Key.isDown(Key.UP))
{
this._y -= moveSpeed;
}
}
}