Little Fenix Help: Ball Collision


Splinter

Member
Joined
Jan 19, 2004
Messages
230
Age
35
Location
Donny, England (follow signs that say craphole)
Website
gp32place.150m.com
K, ive been trying a few things in fenix, specifically to create a breakout clone to get the feel for the language. No suprise to me but ive already ran into a problem. After managing to draw the paddle and sort that out im having trouble with the ball. The ball comes from the paddle and moves up the screen. I want it to 'bouce/reflect' and go back towards the paddle. But i dont know how it is done. Here is what ive done so far:

Code:
Program Drawpaddle;

Global

//GP Keys
__A = _control;
__B = _alt;
__SELECT = _space;
__START = _enter;
__R = _tab;
__L = _backspace;

begin 

//initialize screen:
full_screen = false;
set_mode(m320x240);

//Load graphics
LOAD_FPG("breakout.fpg");

//proccesses
paddle(160,200);
ball(160,190);
end

process paddle(x,y)

Begin
graph = 1;

Loop
//Defines control of paddle
if(key(_left))x-=6;end
if(key(_right))x+=6;end
 
//Defines boundaries for paddle
if(x<22)x=22;end
if(x>298)x=298;end
Frame;
end
end

process ball(x,y)

begin
graph = 2; 

loop
//makes the ball move
y-=5;

//detects ball collision
if(x<5)x=5;end
if(x>315)x=316;end
if(y<5)y+=5;end
FRAME;
end

   
end

Thanks in advance :)
 
Code:
process ball(x,y)
private
vx=5;//xspeed
vy=-5;//yspeed
begin
graph = 2; 

loop
//makes the ball move
y+=vy;
x+=vx;

//detects ball collision
if(x<5 or x>315)vx=-vx;end
if(y<5 or y>235)vy=-vy;end
FRAME;
end

  
end
that should do it i think :)
but ofcourse now you simply got a ball bouncing around

i still got the code of the first "game" i made with fenix i think, a simple pong game, kind of what you're trying to achieve :)


http://users.pandora.be/racemaniac/Fenix/Example.zip
when bouncing the ball just gets a random speed in the y direction, so no speed depending on where you hit it with the paddle :)
i think the code is easy to understand, even without comment :)
 
Hmmmm....having some more trouble, i really havent got much further ;). In trying to draw some blocks i tried:

Code:
Program Breakout;

Global

//GP Keys
__A = _control;
__B = _alt;
__SELECT = _space;
__START = _enter;
__R = _tab;
__L = _backspace;

begin 

//initialize screen:
full_screen = false;
set_mode(m320x240);

//Load graphics
LOAD_FPG("breakout.fpg");

//proccesses
paddle(160,200);
ball(160,190);

end

process paddle(x,y)

Begin
graph = 1;

Loop
//Defines control of paddle
if(key(_left))x-=6;end
if(key(_right))x+=6;end
 
//Defines boundaries for paddle
if(x<20)x=20;end
if(x>300)x=300;end
Frame;
end
end

process ball(x,y)

private
vx=5;//xspeed
vy=-5;//yspeed

begin
graph = 2; 

loop
//makes the ball move
y+=vy;
x+=vx;

//detects ball collision
if(x<5 or x>315)vx=-vx;end
if(y<5)vy=-vy;end 
if(collision(type paddle))vy=-vy;end
if(y>235)x=160; y=190;return;end
FRAME;
end
end

process brick(x,y) 

begin
x=50;
y=50;

graph = 3;
 
Frame;  
end

but for some reason the brick isnt drawn, im sure im doing something stupid but im not sure what.

Also i find once the ball goes past 235 on the y axis, i want it to reset. i can manage this (different to the code above) but then the ball moves down off the screen again and keeps doing so until the paddle is in the way. What im asking is, is there a way to say 'if blah then paddle process+ball process restart but not the bricks? I dont know if that makes any sense but ill see what u guys think.

Many thanks for the help im about to recieve, the Fenix section of the boards is a great little community in itself :)
 
Splinter posted on Sep 29 2005 at 02:03 AM said:
Hmmmm....having some more trouble, i really havent got much further ;). In trying to draw some blocks i tried:

Code:
Program Breakout;

Global

//GP Keys
__A = _control;
__B = _alt;
__SELECT = _space;
__START = _enter;
__R = _tab;
__L = _backspace;

begin 

//initialize screen:
full_screen = false;
set_mode(m320x240);

//Load graphics
LOAD_FPG("breakout.fpg");

//proccesses
paddle(160,200);
ball(160,190);

end

process paddle(x,y)

Begin
graph = 1;

Loop
//Defines control of paddle
if(key(_left))x-=6;end
if(key(_right))x+=6;end
 
//Defines boundaries for paddle
if(x<20)x=20;end
if(x>300)x=300;end
Frame;
end
end

process ball(x,y)

private
vx=5;//xspeed
vy=-5;//yspeed

begin
graph = 2; 

loop
//makes the ball move
y+=vy;
x+=vx;

//detects ball collision
if(x<5 or x>315)vx=-vx;end
if(y<5)vy=-vy;end 
if(collision(type paddle))vy=-vy;end
if(y>235)x=160; y=190;return;end
FRAME;
end
end

process brick(x,y) 

begin
x=50;
y=50;

graph = 3;
 
Frame;  
end

but for some reason the brick isnt drawn, im sure im doing something stupid but im not sure what.

Also i find once the ball goes past 235 on the y axis, i want it to reset. i can manage this (different to the code above) but then the ball moves down off the screen again and keeps doing so until the paddle is in the way. What im asking is, is there a way to say 'if blah then paddle process+ball process restart but not the bricks? I dont know if that makes any sense but ill see what u guys think.

Many thanks for the help im about to recieve, the Fenix section of the boards is a great little community in itself :)
about the brick: you made no loop, the brick will be drawn once and then it comes to the end of the process...
about the ball: as the code you showed is, once the ball goes below the paddle it gets reset, but since you also put a return command there it also gets removed.
check my sourcecode, before the return call a gameover process, and if you press a or so, let the gameover process make a new paddle and ball :), or you could store the process id of the paddle (and ball) somewhere and just access the x and y values from there :)
i'll see if i can make a basic breakout to demonstrate ^^

*edit* http://users.pandora.be/racemaniac/Fenix/BreakoutDemo.rar
a basic breakout game, draws 30 blocks you can destroy, and a paddle off which a ball bounces :), and dependent on where the ball hits the paddle the horizontal speed of the ball changes :)
and documented :)

ow, btw, your globals, they're rather constants, little difference, but still^^, better define them as const instead of global :)
 
Last edited by a moderator:
Back
Top