collision help
Author |
Message |
shoobyman
|
Posted: Wed Nov 08, 2006 9:20 pm Post subject: collision help |
|
|
code: |
View.Set ("offscreenonly")
var ballx, bally, destx, desty : int := 20
var velx, vely : int := 0
var x, y, button : int
var boxx, boxy : array 1 .. 10 of int
var start : boolean := true
boxx (1) := 200
boxy (1) := 200
loop
View.Update
delay (10)
cls
mousewhere (x, y, button)
%Start is when the ball is not yet moving - click to make it move
if start = true then
ballx := 20
bally := maxy div 2
if button = 1 then
start := false
destx := x
desty := y
velx := (destx - ballx) div 20
vely := (desty - bally) div 20
end if
end if
drawfilloval (ballx, bally, 5, 5, 7)
ballx += velx
bally += vely
if ballx > maxx then
velx *= -1
elsif ballx <= 0 then
velx *= -1
end if
if bally > maxy then
vely *= -1
elsif bally <= 0 then
vely *= -1
end if
%Collision, main problem is here
if ballx > boxx (1) and ballx + 3 < boxx (1) + 100 and bally > boxy (1) and bally + 3 < boxy (1) + 80 then
vely *= -1
end if
drawfillbox (boxx (1), boxy (1), boxx (1) + 100, boxy (1) + 80, 7)
end loop
|
alright. run the program, click somewhere and the ball will move. you will notice that collision is not happening properly with the box in the middle. Anyone have any suggestions as to how i could do that? as you can see i have already tried. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonZ
|
Posted: Wed Nov 08, 2006 9:54 pm Post subject: (No subject) |
|
|
ok this looks like a tough one, but I might have a solution, your collision is between a box and an oval, this is the problem: you are trying to use a rectangular collision formula for a circle, which I highly doubt will be accurate, and turns out it is as I said, inaccurate. I think the only way you could solve this problem is if instead of using this collision formula :
code: |
if ballx > boxx (1) and ballx + 3 < boxx (1) + 100 and bally > boxy (1) and bally + 3 < boxy (1) + 80 then
|
you should try to look into colliding irregular objects together(there is actually a tutorial for collision in general here:)http://www.compsci.ca/v2/viewtopic.php?t=13661
but, there is another form of collision that comes into mind: whatdotcolor. whatdotcolor is essentially a single dot (or pixel) that checks the current color of the portion of the screen, so what I propose you do is draw a dot and position the dot according to your circle, that way the single pixel will check what color it is on, then make an if statement like so:
if whatdotcolor (x, y) = black then
% collision
end if
Now im a little tired so I will post another couple of solutions that come into mind tommorow, hopefully I will be able to think straight. |
|
|
|
|
|
richcash
|
Posted: Sat Nov 11, 2006 1:41 pm Post subject: (No subject) |
|
|
The problem is you need to also make the velx *= -1 (or make the ball bounce off the vertical parts of the box). Here's a temporary solution :
Change the collision detection if statement at the bottom to this :
code: | if ballx + 5 > boxx (1) and ballx - 5 < boxx (1) + 100 and bally + 5 > boxy (1) and bally - 5 < boxy (1) + 80 then
if ballx > boxx (1) and ballx < boxx (1) + 100 then
vely *= -1
else
velx *= -1
end if
end if |
This may look right, but that's because the ball radius is only 5. To make it correct, you have to also check if the distance from the center of the ball to any corner is less than the radius of the ball. When this collision happens, you may want to keep it simple and just have both velocities multiplied by -1.
Ask any questions if you don't understand what I did! |
|
|
|
|
|
|
|