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

Username:   Password: 
 RegisterRegister   
 Need Help With Collisions & Boundaries
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Abbas_M




PostPosted: Tue Jan 19, 2010 11:31 am   Post subject: Need Help With Collisions & Boundaries

What is it you are trying to achieve?

I'm making an easy screen saver in which there are two balls bouncing across the screen, and if they collide with each other then they should bounce off.

What is the problem you are having?

When the balls collide, they don't bounce off. The boundary of the Second Ball is not right as well.

Describe what you have tried to solve this problem

I tried to change the boundaries but couldn't get the right ones.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Turing:


% Variables
var xvalue, yvalue : int := 50
var x : int := 1150
var y : int := 10
var dyvalue, dxvalue := 1
var dx, dy := 1
var mX, mY, mX1, mY1, button : int
var winID : int
var redball : int
var blueball : int
var radius1 : int
var radius2 : int

redball := Pic.FileNew ("redball.gif")
blueball := Pic.FileNew ("blueball.gif")

radius1 := Pic.Width (redball) div 2
radius2 := Pic.Width (blueball) div 2

winID := Window.Open ("title:Screensaver,graphics:max;max")    % Fills The Screen Saver To The Whole Screen

View.Set ("nobuttonbar")    % Makes The Screen With No Button Bar On Top

mousewhere (mX, mY, button)
mX1 := mX
mY1 := mY

loop    % Starts The Loop

    % If The Mouse Is Moved Or Pressed Then The Window Will Close And Exit, Like In Real Screen Savers
    mousewhere (mX, mY, button)
    if mX not= mX1 or mY not= mY1 or button = 1 then
        Window.Close (winID)
        exit
    end if

    % If Any Key Is Pressed Then The Window Will Close And Exit, Like In Real Screen Savers
    if hasch then
        Window.Close (winID)
        exit
    end if

    % Boundaries For Ball 1
    if xvalue < 0 or xvalue = maxx - 140 then     % Ball 1 Bounces Off If Collides With The Top or Bottom Walls
        dxvalue := -dxvalue
    elsif yvalue = 0 or yvalue = maxy - 140 then     % Ball 1 Bounces Off If Collides With The Two Side Walls
        dyvalue := -dyvalue
    end if

    % Boundaries For Ball 2
    if x < 0 or x = maxx - 1000 then     % Ball 2 Bounces Off If Collides With The Top or Bottom Walls
        dx := -dx
    elsif y = 0 or y = maxy - 1000 then  % Ball 2 Bounces Off If Collides With The Two Side Walls
        dy := -dy
    end if

    % Ball 1
    yvalue := yvalue + dyvalue
    xvalue := xvalue + dxvalue

    % Ball 2
    x := x - dx
    y := y - dy

    setscreen ("graphics:max;max, offscreenonly, nobuttonbar")

    % Ball 1
    Pic.Draw (redball, xvalue, yvalue, picMerge)

    % Ball 2
    Pic.Draw (blueball, x, y, picMerge)

    View.UpdateArea (0, 0, maxx, maxy)
    cls

    if Math.Distance (xvalue, yvalue, x, y) < (radius1 + radius2) then
        xvalue := xvalue + 2
        yvalue := yvalue + 2
        x := 50
        y := 50
    end if
end loop     % Ends The Loop



Please specify what version of Turing you are using

4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Turing_Gamer




PostPosted: Tue Jan 19, 2010 5:39 pm   Post subject: Re: Need Help With Collisions & Boundaries

For the collision of the 2 balls, reverse the direction of the 2 balls by having x, y, xdir, and ydir values for each ball.
When they both collide, all directions should reverse by saying 'xdir := xdir * 1', etc.

I havn't worked with radius in a long time so this is the best I can do... Crying or Very sad
Hope it helps
Abbas_M




PostPosted: Wed Jan 20, 2010 10:14 am   Post subject: Re: Need Help With Collisions & Boundaries

Thank You so much. Got it working. Thanks.
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  [ 3 Posts ]
Jump to:   


Style:  
Search: