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

Username:   Password: 
 RegisterRegister   
 Mouse co-ordinate detection (Air Hockey)
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
richcash




PostPosted: Mon May 15, 2006 5:52 pm   Post subject: Mouse co-ordinate detection (Air Hockey)

Not every movement of the mouse can be detected by the computer especially when you move the mouse really fast. This is a problem considering that I have a mouse-controlled paddle in the Air Hockey game I am making. Are there any suggestions on how I can work around this. Here's my code so far.
code:

setscreen ("graphics:700;max,position:center;center,nobuttonbar,offscreenonly")
var x, y, button, paddlex, paddley, lastballx, lastbally := 100
var ballx, bally : real := 100
var power := 5
var dx, dy : real := 1
proc drawscreen
    drawfillbox (0, 0, maxx, maxy, red) % Border
    drawfillbox (20, 20, maxx - 20, maxy - 20, 1) %Background
    drawfillbox (20, maxy div 2 - 150, maxx - 20, maxy div 2 - 155, red) % bottom line from center
    drawfillbox (20, maxy div 2 + 150, maxx - 20, maxy div 2 + 155, red) % top line from center
    drawfillbox (20, 20, maxx - 20, 30, grey) % bottom
    drawfillbox (maxx div 2 - 70, 20, maxx div 2 + 70, 40, black) % net
    drawfillbox (20, maxy - 20, maxx - 20, maxy - 30, grey) % top
    drawfillbox (maxx div 2 - 70, maxy - 20, maxx div 2 + 70, maxy - 40, black) % net
    drawfillbox (20, maxy div 2, maxx - 20, maxy div 2 + 5, grey) %center line
    %Center Circle
    drawfilloval (maxx div 2, maxy div 2, 80, 80, grey)
    drawfilloval (maxx div 2, maxy div 2, 70, 70, 1)
    drawfilloval (maxx div 2, maxy div 2, 10, 10, grey)
    % Left top circle
    drawfilloval (maxx div 2 - 200, maxy div 2 + 250, 70, 70, grey)
    drawfilloval (maxx div 2 - 200, maxy div 2 + 250, 60, 60, 1)
    % Right top circle
    drawfilloval (maxx div 2 + 200, maxy div 2 + 250, 70, 70, grey)
    drawfilloval (maxx div 2 + 200, maxy div 2 + 250, 60, 60, 1)
    % Right bottom
    drawfilloval (maxx div 2 + 200, maxy div 2 - 250, 70, 70, grey)
    drawfilloval (maxx div 2 + 200, maxy div 2 - 250, 60, 60, 1)
    % Left bottom
    drawfilloval (maxx div 2 - 200, maxy div 2 - 250, 70, 70, grey)
    drawfilloval (maxx div 2 - 200, maxy div 2 - 250, 60, 60, 1)
    %ball
    drawfilloval (round (ballx), round (bally), 15, 15, 7)
    %paddle
    drawfilloval (paddlex, paddley, 35, 35, 12)
    View.Update
    cls
end drawscreen
proc detection
    if ballx > maxx or ballx < 0 then
        dx *= -1
    end if
    if bally > maxy or bally < 0 then
        dy *= -1
    end if
    if abs (sqrt ((ballx - paddlex) ** 2 + (bally - paddley) ** 2)) < 35 then
        dx := (ballx - paddlex) / 10
        dy := (bally - paddley) / 10
    end if
end detection
loop
    mousewhere (x, y, button)
    if y > maxy div 2 then
        paddley := maxy div 2
    else
        paddley := y
    end if
    if x > maxx then
        paddlex := maxx
    elsif x < 0 then
        paddlex := 0
    else
        paddlex := x
    end if
    ballx += dx
    bally += dy
    detection
    if abs (ballx - lastballx) > power or abs (bally - lastbally) > power then
        lastballx := round (ballx)
        lastbally := round (bally)
        drawscreen
    end if
end loop

**As of now, when you move the mouse too fast, the collision detection is incorrect between puck and paddle or it might not even pick up the collision at all. Thanks for any help!
Sponsor
Sponsor
Sponsor
sponsor
TheOneTrueGod




PostPosted: Mon May 15, 2006 6:03 pm   Post subject: (No subject)

Well, since code moves linearly, the mouse's position will only be recorded at the *instant* that you use the command "mousewhere". Therefore, the only way to increase the mouse's accuracy is to put it right before the collision detection part, or to have multiple "mousewhere"s.

The second option might get kinda laggy if you have too many, or if you have them in a for loop, but its your choice. Either way, there is only a maximum degree of accuracy that you are going to get because of linear code.
Cervantes




PostPosted: Mon May 15, 2006 6:04 pm   Post subject: (No subject)

This is a common problem, though more often found in detecting collisions between two fast moving objects, such as a bullet and a car, for instance.

One solution is to do some math to determine whether there is a collision. You know the start and end points of two shapes. If you do a little bit of math, you can determine whether there was a collision. This is harder than just determining whether the line segments that represent the motion of the two shapes intersect. It's not that easy because there is the element of time in question. By the time shape A has moved almost to its end point, shape B coould cross the path that shape A was on, but there is no collision because shape A is long gone.

You should read the "Circle-Circle Collisions - Clipping problem" thread.
richcash




PostPosted: Mon May 15, 2006 9:50 pm   Post subject: (No subject)

Thanks for the help. I guess I can never get it perfectly accurate, but I will use some trig to identify the probable first point of collision (assuming that consecutive points of the mouse picked up by the computer that are not side-by-side are linearly connected).
[Gandalf]




PostPosted: Tue May 16, 2006 3:33 am   Post subject: (No subject)

Another approach you can take is to add the factor of realism into your game. When you are moving really fast from one point to another the paddle must be on the table in order for there to be a collision. If it's not on the table, you can assume no collision occured, but in your game the paddle is always on the table. Anyway, getting to the point, there is a limitation to how fast someone can drag the paddle on the table in reality, so you can incorporate this into your game. Instead of having the paddle pop to the new location on the screen, you can make it move there at whatever you choose to be the maximum velocity. This will allow you to easily keep track of the paddle at all times, and also have some realistic paddle movement.
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  [ 5 Posts ]
Jump to:   


Style:  
Search: