Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 help needed!
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
gamer




PostPosted: Fri Apr 09, 2004 4:02 pm   Post subject: help needed!

hi all here i hav a little program with bouncing two balls plus a square in the middle to "block" the balls

questions:
1. if u let the program run for awhile, u'll notice that when the ball hits the square at the corner, the ball does weird things (ex. sliding along the square).......why is this happenin?? how can i fix it?
2. noticing that both balls go at same speed (-2), is it possible if i want to hav one ball go at a random speed?
3. do i just use math.distance for the two bals to bounce off each other?

code:

setscreen ("graphics:800;600")
View.Set ("offscreenonly")
var x, y, x2, y2 : int %centre position
var obsx, obsy : int := 300 %obstacle position
var dx, dy, dx2, dy2 : int := -2 %centre and direction/speed
const ballradius : int := 20

randint (x, ballradius, maxx - ballradius) %ball1 random starting x-position
randint (y, ballradius, maxy - ballradius) %ball1 random starting y-position

randint (x2, ballradius, maxx - ballradius) %ball2 random starting x-position
randint (y2, ballradius, maxy - ballradius) %ball2 random starting y-position

proc obs %obstacles
    drawfillbox (obsx - 50, obsy - 50, obsx + 50, obsy + 50, yellow)
end obs

proc ball1
    drawfilloval (x, y, ballradius, ballradius, red)
    x += dx
    y += dy
    if x >= maxx - ballradius or x <= ballradius or Math.DistancePointLine (x, y, obsx - 50, obsy + 50, obsx - 50, obsy - 50) <= ballradius or Math.DistancePointLine (x, y, obsx + 50, obsy + 50, obsx
            + 50, obsy - 50) <= ballradius then
        dx := -dx
    elsif y >= maxy - ballradius or y <= ballradius or Math.DistancePointLine (x, y, obsx - 50, obsy + 50, obsx + 50, obsy + 50) <= ballradius or Math.DistancePointLine (x, y, obsx - 50, obsy - 50,
            obsx + 50, obsy - 50) <= ballradius then
        dy := -dy
    elsif Math.Distance (x, y, obsx - 50, obsy + 50) = ballradius or Math.Distance (x, y, obsx - 50, obsy - 50) = ballradius or Math.Distance (x, y, obsx + 50, obsy + 50) = ballradius or
            Math.Distance (x, y, obsx + 50, obsy - 50) = ballradius then
        dx := -dx
        dy := -dy
    end if
end ball1

proc ball2
    drawfilloval (x2, y2, ballradius, ballradius, white)
    x2 += dx2
    y2 += dy2
    if x2 >= maxx - ballradius or x2 <= ballradius or Math.DistancePointLine (x2, y2, obsx - 50, obsy + 50, obsx - 50, obsy - 50) <= ballradius or Math.DistancePointLine (x2, y2, obsx + 50, obsy +
            50, obsx + 50, obsy - 50) <= ballradius then
        dx2 := -dx2
    elsif y2 >= maxy - ballradius or y2 <= ballradius or Math.DistancePointLine (x2, y2, obsx - 50, obsy + 50, obsx + 50, obsy + 50) <= ballradius or Math.DistancePointLine (x2, y2, obsx - 50, obsy -
            50, obsx + 50, obsy - 50) <= ballradius then
        dy2 := -dy2
    elsif Math.Distance (x2, y2, obsx - 50, obsy + 50) = ballradius or Math.Distance (x2, y2, obsx - 50, obsy - 50) = ballradius or Math.Distance (x2, y2, obsx + 50, obsy + 50) = ballradius or
            Math.Distance (x2, y2, obsx + 50, obsy - 50) = ballradius then
        dx2 := -dx2
        dy2 := -dy2
    end if
end ball2

loop
    drawfillbox (0, 0, maxx, maxy, black)
    obs
    ball1
    ball2
    View.Update
    delay (5)
end loop


thnx
Sponsor
Sponsor
Sponsor
sponsor
Paul




PostPosted: Fri Apr 09, 2004 4:08 pm   Post subject: (No subject)

the reason for that is the ball isn't bouncing off the edge of the square properly, I had the same problem in paddleball. I didn't take a close look at the code but there could be some possiblilities:
1. the code only checks for the most up, left, down right, coordinates of the ball, so when the ball comes towards the square's corner diagonally, it doesn't detect the collision at all.
2. the collision checks for coordinates that are bypassed because of the large increments of the coordinates of the ball. IE: the wall is at 230, the ball checks for 229, doesn't detect anything, the ball moves 5 forward, checks for 234, and misses the wall.
I dunno how to fix it, because I haven't learned that complex equation that zylum was going to explain.
gamer




PostPosted: Fri Apr 09, 2004 4:35 pm   Post subject: (No subject)

thnx paul, after i looked at ur 2nd suggestion i tried changing the speed of the ball to 1 instead of 2.... and everythin was fine!!

however, remember i had the other problem of wanting one ball to move at random speed (ie: speed of 1 for 2 seconds then speed of 3 for 2 seconds etc)....so would it stil work?? n how?
Paul




PostPosted: Fri Apr 09, 2004 4:41 pm   Post subject: (No subject)

it would be weird? I mean you want it to suddenly speed up while it's bouncing?
the you just go like
code:

%incX and incY are the variables that hold the values by which your ball
%increases each time of the loop.
if incX > 0 then
randint (incX, 1, 5)
else
randint (incX, 1, 5)
incX:= -incX
end if
if incY>0 then
incY:=incX
else
incY:= -incX
end if

or are you asking for the ball to bouce off at random angles as it hits the walls? that could be a little more complicated.
gamer




PostPosted: Fri Apr 09, 2004 4:48 pm   Post subject: (No subject)

no no no....like 4get the collision for a sec....what i need is one ball travel at a varying pace/speed

so if normally it goes at speed of 1, then one or two sec later it goes faster OR slower......n continues

but if talking about collision of the obstacle's corner, i think i must need the speed to be 1.....then maybe i can random the delay? lol but then delay applies for both of the balls, n i need just one ball to be delay, how can i do this??
Paul




PostPosted: Fri Apr 09, 2004 4:54 pm   Post subject: (No subject)

so one ball travels at a constant speed, the other speed varies.
Maybe have a counter inside the loop, and have the 2 balls increase by 2 different variables, when the counter counts that the loop has run say, 10 times, it changes the increase variable from 1 to 3, then after it runs another 10 times, it changes to 2, so just randint inside the loop? I dunno about the delay thing though. It could probably mess up your collision detection too.
gamer




PostPosted: Fri Apr 09, 2004 5:27 pm   Post subject: (No subject)

damn even tho i change the speed to 1 the corner detection would screwup if i let the program run longer.....i guess this isnt the way to fix it
wut should i do then?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: