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

Username:   Password: 
 RegisterRegister   
 Air Hockey collision problem
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
poll262




PostPosted: Mon Jun 18, 2012 5:27 pm   Post subject: Air Hockey collision problem

I am making an air hockey game and am having difficulties getting the puck to bounce off of the paddle in the right direction. Plus sometimes the puck will get stuck inside of the paddle. Here is my code so far.

Turing:

View.Set ("graphics:1000;600, offscreenonly")
var paddleX, paddleY, paddleRadius, button : int
var ballX, ballY, ballRadius, vx, vy : int

ballX := 500
ballY := 300
paddleRadius := 20
ballRadius := 20
vx := (Rand.Int (0, 1) * 2) - 1
vy := (Rand.Int (0, 1) * 2) - 1

procedure graphics

    drawarc (0, 300, 90, 90, 270, 90, red)
    drawarc (1000, 300, 90, 90, 90, 270, red)
    drawfillbox (490, 0, 510, 600, red)
    drawfillbox (250, 0, 255, 600, blue)
    drawfillbox (750, 0, 755, 600, blue)
    drawfilloval (paddleX, paddleY, paddleRadius, paddleRadius, blue)
    drawfilloval (ballX, ballY, ballRadius, ballRadius, black)

end graphics

procedure ballMove

    ballX := ballX - vx
    ballY := ballY - vy

    if Math.Distance (paddleX, paddleY, ballX, ballY) < paddleRadius * 2 then
        vx := -vx
        vy := -vy
    end if

    if ballX + ballRadius >= maxx then
        vx := -vx
    elsif ballX - ballRadius <= 0 then
        vx := -vx
    end if
    if ballY + ballRadius >= maxy then
        vy := -vy
    elsif ballY - ballRadius <= 0 then
        vy := -vy
    end if

end ballMove

loop
    mousewhere (paddleX, paddleY, button)
    graphics
    ballMove
    View.Update
    cls
end loop
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Mon Jun 18, 2012 7:09 pm   Post subject: RE:Air Hockey collision problem

> Plus sometimes the puck will get stuck inside of the paddle.

That's because you move your position before checking if being in that position collides with something.

> bounce off of the paddle in the right direction.

What's happening is that, no matter the angle the two objects collide at, the puck will inverse it's direction. This is similar to shooting at a wall at any weird angle, and the bullet rebounding right back at you.

To quote this dude, "For perfect particles (& light) the angle of reflection is equal to the angle of incidence, as illustrated by this diagram (from commons.wikimedia.org)."


http://upload.wikimedia.org/wikipedia/commons/2/24/Angle_of_Reflection_(PSF).png

So, since the walls are along the planes, getting the resulting vector is fairly easy. To get a proper reflection of the two circles, we first need to know what we're bouncing off of.

Posted Image, might have been reduced in size. Click Image to view fullscreen.

This shows the process a bit. We're bouncing off the line perpendicular to the vector from one center of a circle to the next.

As for the math of what you need to do... Well, I'll leave that to someone who's actually done that before.
Raknarg




PostPosted: Mon Jun 18, 2012 7:25 pm   Post subject: RE:Air Hockey collision problem

That doesnt seem right to me. What your'e saying is that at any given angle, they will reflect perpendicularly? That's wrong.
poll262




PostPosted: Mon Jun 18, 2012 7:34 pm   Post subject: Re: Air Hockey collision problem

Also how can I use two different radius's to for the puck and the paddle. Whenever I use different radius's, Math.Distance will no longer work to detect when they are touching.
Raknarg




PostPosted: Mon Jun 18, 2012 7:47 pm   Post subject: RE:Air Hockey collision problem

if Math.Distance (x1, y1, x2, y2) <= radius1 + radius 2 then ...
poll262




PostPosted: Mon Jun 18, 2012 8:07 pm   Post subject: Re: Air Hockey collision problem

Thanks that works. Now can someone explain to me what vectors are and how I can use then to get the puck to bounce off the paddle in the proper direction.
Raknarg




PostPosted: Mon Jun 18, 2012 8:11 pm   Post subject: RE:Air Hockey collision problem

Ever worked with trigonometry before? Cause if you haven't, I'm going to let someone else explain it
poll262




PostPosted: Mon Jun 18, 2012 8:14 pm   Post subject: Re: Air Hockey collision problem

I have done trig before I just do not know what a vector is.
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: