Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Author
Message
Twisted Demon
20
Years of Service
User Offline
Joined: 4th Apr 2004
Location:
Posted: 9th Apr 2004 14:55 Edited at: 9th Apr 2004 14:56
Ive been working on a pong game, but i don't know how to check for collision between an object and a sprite or is it even possible.

here is my code. i have all the images and stuff drawn, and the ball moves, but bounces off the edge regardless of wether or not the bat is there, can someone please give me a few pointers? i tried do it, using a combination of co-ordinates, the x and y from the bat is in pixels i believe tho, so that can be like 400 whereas the x and y for my ball, is like 1.3232323232. i don't know if that will make any sense to you tho, lol.

i was thinking it may be possible to turn a sprite into an object, so i can check for collision easier????


`Settings
sync on
set display mode 1024, 768, 16
py = 334
cy = 334
bx = 100
by = 100


`Load image of players bat
load image "bat.bmp", 1, 1
sprite 1, 996, py, 1

`Load image of computers bat
load image "bat.bmp", 2, 1
sprite 2, 24, cy, 2

`Load image of ball
make object sphere 4, 0.1
yrotate object 4, 90

do
`Set player movement and limitations
if py < 24 then py = py + 5
if py > 640 then py = py - 5
if upkey() = 1 then py = py - 5
if downkey() = 1 then py = py + 5
sprite 1, 1000, py, 1

`Set Player 2 movement and limitations
if cy < 24 then cy = cy + 5
if cy > 640 then cy = cy - 5
if keystate(17) = 1 then cy = cy - 5
if keystate(31) = 1 then cy = cy + 5
sprite 2,24,cy,2

`Draw boundaries
line 17, 19, 1011, 19
line 17, 744, 1011, 744
line 17, 19, 17, 744
line 1011, 19, 1011, 744

`Ball movement
if object position x(4) => 1.45 then ballangle = 180 `needs paddle position adding
if object position x(4) =< -1.45 then ballangle = 360 ` needs paddle postion adding
xrotate object 4, ballangle
move object 4 , 0.03


sync

loop
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 9th Apr 2004 22:06
mixing 2d and 3d is gonna burn your brain out, for a start they use totaly different co-ordinate systems, one is in pixels and one in world units, make the ball a sprite (c`mmon! anyone can draw a circle ) and just use sprites, or go the other way and make the game with 3D objects (boxes for the bats etc) but don`t try to mix the two, for reflection just bounce the ball when it hits something, and check for it going off the screen to lose a player a life, to bounce the ball, move it up, if it hits something then move it back to the old position and then reverse the verticle direction, then do the same for moving across, that will handle the bounce for all conditions and work for top/bottom and left/right, then do a sync, remember that until a sync is done (assumeing you have sync on) you can move the ball all over the place to check for collision and the final position will be the only one seen at the sync, cheers


Mentor.

PC1: P4 hyperthreading 3ghz, 1gig mem, 2x160gig hd`s, ATI radeon 9800 pro gfx, 6 way surround sound, PC2: AMD 1.2ghz, 512mb ram, FX5200 ultra gfx, stereo 16 bit soundblaster, ups.
Twisted Demon
20
Years of Service
User Offline
Joined: 4th Apr 2004
Location:
Posted: 9th Apr 2004 22:50
thanks, i took your advice, and choose to use all sprites, but i still dont get the collsiosn detection, ive got it so everything is sprites, even the boundaries, so i cna use the command "sprite hit()"

when i use this command to check if it has hit the bat though, i jus minus 5 from the x direction, but this means the ball jus gets stuck, i need a way for it to change direction, until it hits another object, i can only make it change direction whilst its hitting the object.

heres the code


id appreciate any help
thanks
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 9th Apr 2004 22:58
have two values, for example

BallXDirection=1
BallYDirection=1

then in the main loop do this

BallXPosition=BallXPosition+BallXDirection
BallYPosition=BallYPosition+BallYDirection

all you need to do then is to check for a collision and change the correct value eg

if object collision(batnumber,ballnumber) then BallXDirection=-1

will make the ball go the other way for as long as nothing hits it since you keep adding minus one to the position (or whatever number you use), just add the directions to the position and make the directions negative or positive as needed, cheers.

Mentor.

PC1: P4 hyperthreading 3ghz, 1gig mem, 2x160gig hd`s, ATI radeon 9800 pro gfx, 6 way surround sound, PC2: AMD 1.2ghz, 512mb ram, FX5200 ultra gfx, stereo 16 bit soundblaster, ups.
Twisted Demon
20
Years of Service
User Offline
Joined: 4th Apr 2004
Location:
Posted: 9th Apr 2004 23:16
thanks alot man, it wokrs now, ill probably be asking some more questions soon tho lol
Twisted Demon
20
Years of Service
User Offline
Joined: 4th Apr 2004
Location:
Posted: 9th Apr 2004 23:47
lol, im back
ive done some very basic ai, baty tries to equal the bally. but the computer still often shoots past the ball, the paddle wont stop where the ball is gonna hit, it just keeps on moving, also why does my rnd starting position for the bal not work.

heres the code



thanks again anyone who replies
Sparda
20
Years of Service
User Offline
Joined: 13th Jan 2004
Location: Pacifica
Posted: 10th Apr 2004 07:45
When you have the computer's paddle move, have it move only a portion of the distance between the paddle and ball's position. I'm guessing the ball moves horizontally across the screen with paddles on the left and right. So have the paddle move (abs(sprite position y(ball) - sprite position y(paddle))/5. This way the paddle won't move past the ball. (That wasn't code either, just a verbal way of putting it hope it works

There 10 kinds of people, those who understand binary and those who don't
Blue Shadow
20
Years of Service
User Offline
Joined: 6th Feb 2004
Location:
Posted: 11th Apr 2004 23:50
You could use this code for increasing the direction of the ball:



Cheers,

Michael

Visit the Code Monkey's website at http://www.freewebs.com/codemonkeystudios

Login to post a reply

Server time is: 2024-09-22 04:26:15
Your offset time is: 2024-09-22 04:26:15