Object collision

Hi everyone,

I have been working with a pong game in xojo but unfortunately I lost my USB causing me to forget some of the codes I wrote on Tuesday;

I have the keyboard working for ‘paddle’ and ‘paddle1’, and have a ‘pongball’ that bounces off of four walls, I also have put the game into a canvas because I need a scoreboard on the outside which I am trying to work out but what I am focussing most on is having the ‘pongball’ collide with both paddles.

I have a code going for ‘paddle’ but it doesn’t seem to work, I’d appreciate it if you can help me figure out what the problem is. This is my code:

 if (pongsquare.left < Paddle.width) and (Pongsquare.top + pongsquare.height > Paddle.height) and (Pongsquare.left > Paddle.left) then
Pongsquare.left = Paddle.width

end if

Which I have put into a timer with a period of 30 that goes right, I have the same exact code on the left timer for the ‘paddle1’ that is located on the left side of the canvas window. (I think that the first line of the code is good but the second line of the code is definitely wrong, or maybe they’re both wrong?)

Have a look here:
http://www.tinrocket.com/superspritesurface/
I have no idea if this still works with the latest versions of Xojo, but if it does, you get automatic collision detection (among other benefits).
Also you might profit from a visit here:
http://www.real3dtutorials.com/
Collision detection is a difficult nut to crack on your own. Seek help where you can.

[quote=167439:@Roger Clary]Have a look here:
http://www.tinrocket.com/superspritesurface/
I have no idea if this still works with the latest versions of Xojo, but if it does, you get automatic collision detection (among other benefits).
Also you might profit from a visit here:
http://www.real3dtutorials.com/
Collision detection is a difficult nut to crack on your own. Seek help where you can.[/quote]

Thank you so very much for the response, I will look into both links and see where they take me.

Have a look here:
http://www.tinrocket.com/superspritesurface/

The only problem with this is that I don’t use a sprite surface.

for simple objects

if the objects are rectangles… there is a standard “overlap” calcuation for this (google it :slight_smile: )
if objects are circles, measure the distance between center points-the sum of the radius of each object

I’m digging very deep into my memory here, but the way we used to do it a long time ago on 8 bit computers (before the CBM64 had hardware sprites) was to have a mask as well as the picture info. So a simple monochrome 8x8 sprite for a ‘circle’ might have a mask like:

00011000
00111100
01111110
11111111
11111111
01111110
00111100
00011000

To see if they’d collided you basically used logical ANDs on the bits of the two objects. Apologies for the nostalgic ramble…

[quote=167458:@Dave S]for simple objects

if the objects are rectangles… there is a standard “overlap” calcuation for this (google it :slight_smile: )
if objects are circles, measure the distance between center points-the sum of the radius of each object[/quote]

this sort of helped me only I still don’t know the code that i’m looking for but I do know that I need the ‘pongsquare’ to return

how would I do it?

I have it like;

if (…<…) and (…+…>…) and (…<…) then
ponsquare.left return = true
end if

it gives me a bug, how do I debug it to have it work?.

[quote=167471:@Richard Brown]I’m digging very deep into my memory here, but the way we used to do it a long time ago on 8 bit computers (before the CBM64 had hardware sprites) was to have a mask as well as the picture info. So a simple monochrome 8x8 sprite for a ‘circle’ might have a mask like:

00011000
00111100
01111110
11111111
11111111
01111110
00111100
00011000

To see if they’d collided you basically used logical ANDs on the bits of the two objects. Apologies for the nostalgic ramble…[/quote]

Sorry,but this kind of confuses me…where did the numbers come in from and what do they mean? (I am using xojo which uses words and not numbers like this.) Sorry…

You were headed in the right direction. It might help you to draw it out on paper. You want

  1. The right side of the pongsquare >= the left side of the paddle
  2. The left side of the pongsquare <= the right side of the paddle
  3. The bottom of the pongsquare >= the top of the paddle
  4. The top of the pongsquare <= the bottom the the paddle

If all those conditions are true, you have a collision.

[quote=167498:@Tim Hare]You were headed in the right direction. It might help you to draw it out on paper. You want

  1. The right side of the pongsquare >= the left side of the paddle
  2. The left side of the pongsquare <= the right side of the paddle
  3. The bottom of the pongsquare >= the top of the paddle
  4. The top of the pongsquare <= the bottom the the paddle

If all those conditions are true, you have a collision.[/quote]

I might be getting the code wrong, but it still doesn’t work; this is the code I tried:

if (pongsquare.left > paddle.left) and (Pongsquare.top +Pongsquare.height > Paddle.top) and (Pongsquare.top < Pongsquare.top) then
MoveLeft.mode = 2
MoveRight.mode = 0
end if

(Does it have anything to do with the timer? If not, then what do I write after the ‘then’?)

[quote=167498:@Tim Hare]You were headed in the right direction. It might help you to draw it out on paper. You want

  1. The right side of the pongsquare >= the left side of the paddle
  2. The left side of the pongsquare <= the right side of the paddle
  3. The bottom of the pongsquare >= the top of the paddle
  4. The top of the pongsquare <= the bottom the the paddle

If all those conditions are true, you have a collision.[/quote]

Would it be:
(For right-sided paddle)
The right side of the pongsquare >= the left side of the paddle

Pongsquare.left + Pongsquare.width > Paddle.left = true
The left side of the pongsquare <= the right side of the paddle
Pongsquare.left < Paddle.left + Paddle.width = true
The bottom of the pongsquare >= the top of the paddle
Pongsquare.top + Pongsquare.height > Paddle.top
The top of the pongsquare <= the bottom the the paddle
Pongsquare.top < Paddle.top + Paddle.height = true

                      (For left-sided paddle)
The left side of the pongsquare <= the right side of the paddle

Pongsquare.left < Paddle.left + Paddle.width = true
The right side of the pongsquare >= the leftt side of the paddle
Pongsquare.left + Paddle.width > Paddle.left = true
The top of the pongsquare <= the bottom of the paddle
Pongsquare.top < Paddle.top + Paddle.height
The top of the pongsquare >= the bottom the the paddle
Pongsquare.top + Pongsquare.height > Paddle.top = true

  • I tried it but it gave me the message ’ There is more than one item with this name, and it’s not clear as to which item it is’ so I tried to place and/or amongst each code but it left me with the same bug thus I got no clue what to do.*