Problem Bouncing
Author |
Message |
TokenHerbz
|
Posted: Thu Jul 21, 2005 10:02 pm Post subject: Problem Bouncing |
|
|
Hi, i read all the totorials, believe you me, and maybe i dont know what its sopposed to be like, and passed it, so sorry if i made you mad.
setscreen ("Graphics,500,500")
var ballc: array 1 .. 5 of int
var ballx: array 1 .. 5 of int
var bally: array 1 .. 5 of int
var ballx1: array 1 .. 5 of int
for i: 1 .. 5
randint(ballc(i),1,99)
end for
for i: 1 .. 5
randint (ballx(i),100,400)
end for
for i: 1 .. 5
randint (bally(i),100,400)
end for
for i: 1 .. 5
randint (ballx1(i),1,10)
end for
for i: 1 .. 5
drawfilloval (ballx(i),bally(i),ballx1(i),ballx1(i),ballc(i))
end for
------------------------
Thats my code for now, i know how to make boudries OFF the sides, i need help making the balls bounce off each other...
Also, i dont know how to make them move in random directions from start, gaining speed each time 1 hits either another ball, or the sides.
Im thinking i need a loop AFTER my for statments... but i tryed like
for i: 1 .. 5
ballx(i) := ball + 1
end for
it dosn't work, i tryed more, nothings random, and i just need some help trying to understand more of this.
with the boundries,
for i: 1 .. 5
if ballx(i) >= maxx - 10 then
ballx(i) := maxx - 10
elsif ballx(i) <= 10 then
ballx(i) := 10
end if
end for
Thats assuming my random ball size is maximum 10, well i think that'll work, either way... Id like help...
Over Veiw...
To get 5 balls of random speed, size, color, place, etc: moving in random directions gaining speed each time they collide with eachother OR the boundries (Which i want to be set to what ever the user sets the screen too)... I also want to outr ball to hit the end, so maybe we have to add in some Radious??? variables? yes?
Anywho, to just accomplish all this, Help with explainations and correct code would be much appreciated... I dont want no bunk proceadures at all, i read there crap, so im not going to ever use them.
Thanks! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TokenHerbz
|
Posted: Thu Jul 21, 2005 10:12 pm Post subject: (No subject) |
|
|
Um, me again, i notice other peoples code is really small, and blue...
how do u do that, cause my big post must bug you guys/girls off...
|
|
|
|
|
|
Tony
|
Posted: Thu Jul 21, 2005 11:28 pm Post subject: (No subject) |
|
|
for "blue" code tags:
[code] ... [/code]
better yet - "red" syntax
[syntax="turing"] ... [/syntax] |
|
|
|
|
|
Cervantes
|
Posted: Fri Jul 22, 2005 8:29 am Post subject: Re: Problem Bouncing |
|
|
tokenherbz wrote:
Thats my code for now, i know how to make boudries OFF the sides, i need help making the balls bounce off each other...
Making balls bounce off each other is quite difficult. For some source code, check out thoughtful's pool tutorial.
For more source code of this in action, check out my Evasive Maneuvers game.
Also, I'm not sure what "ballc" and "ballx1" are supposed to be. You should probably be using "ballx", bally", ballvx", and ballvy". The last two represent the velocity in the x and y planes, repsectively.
your_code: |
for i: 1 .. 5
if ballx(i) >= maxx - 10 then
ballx(i) := maxx - 10
elsif ballx(i) <= 10 then
ballx(i) := 10
end if
end for
|
That won't make the ball bounce off the wall, it will only make the ball stick to the wall. To bounce off, it has to reverse its direction in the plane perpendicular to the wall. Since the wall (two of them, anyway) are in the y plane, we must reverse the velocity in the x plane:
code: |
ball.vx *= -1 %same as ball.vx := ball.vx * -1
|
tokenherbz wrote:
gaining speed each time they collide with eachother OR the boundries
This goes against the laws of physics. If you really want this though, just multiply the velocity of the ball by 1.1 or a number close to but above one each time a collision occurs.
Cheers! |
|
|
|
|
|
TokenHerbz
|
Posted: Fri Jul 22, 2005 7:27 pm Post subject: (No subject) |
|
|
this is that i have so far... again
code: |
setscreen ("Graphics,500,500")
var ballc: array 1 .. 10 of int %colors
var balls: array 1 .. 10 of int %size
var ballx: array 1 .. 10 of int %x position
var bally: array 1 .. 10 of int %y position
var ballvx: array 1 .. 10 of int %x direction
var ballvy: array 1 .. 10 of int %y direction
%getting random numbers for variables
for i: 1 .. 10
randint(ballc(i),1,99)
end for
for i: 1 .. 10
randint(balls(i),2,10)
end for
for i: 1 .. 10
randint (ballx(i),100,400)
end for
for i: 1 .. 10
randint (bally(i),100,400)
end for
for i: 1 .. 10
loop
randint (ballvx(i),-3,3)
if ballvx(i) not= 0 then
exit
end if
end loop
end for
for i: 1 .. 10
loop
randint (ballvy(i),-3,3)
if ballvy(i) not= 0 then
exit
end if
end loop
end for
%Moving the ball / Setting Boundries outsides
loop
for i: 1 .. 10
drawfilloval (ballx(i), bally(i), balls(i)+1, balls(i)+1, 0)
ballx(i) := ballx(i) + ballvx(i)
bally(i) := bally(i) + ballvy(i)
if ballx(i) >= maxx - balls(i) then
ballvx(i) *= -1
elsif ballx(i) <= balls(i) then
ballvx(i) *= -1
elsif bally(i) >= maxy - balls(i) then
ballvy(i) *= -1
elsif bally(i) <= balls(i) then
ballvy(i) *= -1
end if
drawfilloval (ballx(i), bally(i), balls(i), balls(i), ballc(i))
delay(1)
end for
%Setting balls to bounce off eachother
for i: 1 .. 10
for j: 1 .. 10
if abs (ballx(i) - ballx(j)) <= balls(i) + balls(j) then
ballx(i) *= -1
ballx(j) *= -1
end if
if abs (bally(i) - bally(j)) <= balls(i) + balls(j) then
bally(i) *= -1
bally(j) *= -1
end if
end for
end for
end loop
|
Form setting balls to bounce down, it screws up royaly, take it out, itll go nicly..
I looked over the pool game, and am still stumped... Perhaps just tell me? i cant seem to find it anywhere in the totoreals...
Please tell me the fastest, easyist code to acomplish the bouncing of the balls. I just want the balls to bounce off of each other.
Thanks |
|
|
|
|
|
MysticVegeta
|
Posted: Sat Jul 23, 2005 4:23 pm Post subject: (No subject) |
|
|
couple of things.
1> Array declarations shouldnt take more than 1 line.
2> You can randint the 4 things in jsut 1 for loop. No need to make 6
3> So many arrays Check out record in the Turing Walkthrough. Example of what you could do ->
code: | var something : array 1 .. 10 of
record
x : int
y : int
vy : int
vx : int
c : int
end record
|
And to call a record, its really easy..
code: | for s : 1 .. 10
something (s).x := s * 2
something (s).y := s div 2
something (s).vy := s ** 2
something (s).vx := s - 2
something (s).c := s + 2
end for
|
|
|
|
|
|
|
TokenHerbz
|
Posted: Sat Jul 23, 2005 9:31 pm Post subject: (No subject) |
|
|
ok, but hows that help my problem?
the balls, they don't bounce off of each other. |
|
|
|
|
|
Cervantes
|
Posted: Mon Jul 25, 2005 8:12 pm Post subject: (No subject) |
|
|
Have you looked at the link I gave you? The one to thoughtful's pool. You'll have to adapt it to your program yourself.
tokenherbz wrote:
ok, but hows that help my problem?
It makes your code more organized, readable, understandable, and therefore better. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
MysticVegeta
|
Posted: Tue Jul 26, 2005 6:05 pm Post subject: (No subject) |
|
|
couldnt have said that better myself, Geoff |
|
|
|
|
|
|
|