Need Help with Collision
Author |
Message |
18729
|
Posted: Mon Sep 25, 2006 9:43 pm Post subject: Need Help with Collision |
|
|
can someone help me with my collision in my asteroids game. the rocks explode even when its not touching the ship. it explodes a few pixels away only on the left side thouhg....not on the right
code: |
setscreen ("graphics:640;480,nocursor")
var shippic : int := Pic.FileNew ("Spaceship.bmp")
var rock1pic : int := Pic.FileNew ("Rock1.bmp")
var rock2pic : int := Pic.FileNew ("Rock2.bmp")
var rock3pic : int := Pic.FileNew ("Rock3.bmp")
var rock4pic : int := Pic.FileNew("Rock4.bmp")
var rock5pic : int := Pic.FileNew("Rock5.bmp")
var boompic : int := Pic.FileNew("Boom.bmp")
var laserpic : int := Pic.FileNew ("Laser.bmp")
var shipxcenter : int := 290
var shipycenter : int := 0
var rock1xcenter : int := Rand.Int (10, 630)
var rock1ycenter : int := maxy + 20
var rock2xcenter : int := Rand.Int (10, 630)
var rock2ycenter : int := maxy + 20
var rock3xcenter : int := Rand.Int (10, 630)
var rock3ycenter : int := maxy + 20
var rock4xcenter : int := Rand.Int (10,630)
var rock4ycenter : int := maxy + 20
var rock5xcenter : int := Rand.Int (10,630)
var rock5ycenter : int := maxy + 20
var boomxcenter : int := 0
var boomycenter : int := 0
var laserxcenter : int :=-10
var laserycenter : int :=-10
var shoot: boolean :=false
var chars : array char of boolean
var key : string(1):=""
var speedofship : int := 10
var font1 : int := Font.New ("comicsans:12")
var score : int := 0
var lives : int := 3
procedure DrawShip (x, y : int)
Pic.Draw (shippic, x, y, picMerge)
end DrawShip
procedure DrawRock (x, y ,rockpic: int)
Pic.Draw (rockpic, x, y, picMerge)
end DrawRock
procedure DrawBoom(x,y :int)
Pic.Draw(boompic,x,y,picMerge)
end DrawBoom
procedure DrawLaser (x, y : int)
Pic.Draw (laserpic, x, y, picMerge)
end DrawLaser
DrawShip (shipxcenter, shipycenter)
procedure Collision(X1:int,Y1:int,var X2:int,var Y2:int,pic1:int,pic2:int)
if X1+Pic.Width(pic1)/2>X2-Pic.Width(pic2)/2 and X1-Pic.Width(pic1)/2<X2+Pic.Width(pic2)/2 and Y1+Pic.Width(pic1)>Y2-Pic.Width(pic2)/2 and Y1-Pic.Width(pic1)/2<Y2+Pic.Width(pic2)/2 then
boomxcenter:=X2
boomycenter:=Y2
DrawBoom(boomxcenter,boomycenter)
X2 := Rand.Int (10,630)
Y2 := maxy + 20
lives:=lives-1
end if
end Collision
loop
Input.KeyDown (chars)
View.Update
View.Set ("offscreenonly")
cls
Font.Draw ("Score: ", 10, 465, font1, 7)
Font.Draw (intstr (score), 70, 465, font1, 7)
Font.Draw ("Lives: ", 500, 465, font1, 7)
Font.Draw (intstr (lives), 560, 465, font1, 7)
if hasch then
getch(key)
end if
if shoot=true then
laserycenter:=laserycenter+20
end if
if chars (chr(ORD_SPACE)) and laserxcenter=-10 and laserycenter=-10 then
laserxcenter:=shipxcenter+27
laserycenter:=shipycenter+97
shoot:=true
end if
DrawLaser(laserxcenter,laserycenter)
if laserycenter>=maxy+10 then
laserxcenter:=-10
laserycenter:=-10
shoot:=false
end if
if chars (KEY_UP_ARROW) then
shipycenter := shipycenter + speedofship
end if
if chars (KEY_RIGHT_ARROW) then
shipxcenter := shipxcenter + speedofship
end if
if chars (KEY_LEFT_ARROW) then
shipxcenter := shipxcenter - speedofship
end if
if chars (KEY_DOWN_ARROW) then
shipycenter := shipycenter - speedofship
end if
DrawShip (shipxcenter, shipycenter)
if shipxcenter <= 1 then
shipxcenter := 1
end if
if shipxcenter >= maxx - Pic.Width (shippic) then
shipxcenter := maxx - Pic.Width (shippic)
end if
if shipycenter <= 1 then
shipycenter := 1
end if
if shipycenter >= maxy - Pic.Height (shippic) then
shipycenter := maxy - Pic.Height (shippic)
end if
DrawRock (rock1xcenter, rock1ycenter,rock1pic)
rock1ycenter := rock1ycenter - 1
if rock1ycenter <= 0 then
rock1xcenter := Rand.Int (10, 630)
rock1ycenter := maxy + 20
end if
DrawRock (rock2xcenter, rock2ycenter,rock2pic)
rock2ycenter := rock2ycenter - 2
if rock2ycenter <= 0 then
rock2xcenter := Rand.Int (10, 630)
rock2ycenter := maxy + 20
end if
DrawRock (rock3xcenter, rock3ycenter,rock3pic)
rock3ycenter := rock3ycenter - 3
if rock3ycenter <= 0 then
rock3xcenter := Rand.Int (10, 630)
rock3ycenter := maxy + 20
end if
DrawRock (rock4xcenter, rock4ycenter,rock4pic)
rock4ycenter := rock4ycenter - 4
if rock4ycenter <= 0 then
rock4xcenter := Rand.Int (10, 630)
rock4ycenter := maxy + 20
end if
DrawRock (rock5xcenter, rock5ycenter,rock5pic)
rock5ycenter := rock5ycenter - 5
if rock5ycenter <= 0 then
rock5xcenter := Rand.Int (10, 630)
rock5ycenter := maxy + 20
end if
Collision(shipxcenter,shipycenter,rock1xcenter,rock1ycenter,shippic,rock1pic)
Collision(shipxcenter,shipycenter,rock2xcenter,rock2ycenter,shippic,rock2pic)
Collision(shipxcenter,shipycenter,rock3xcenter,rock3ycenter,shippic,rock3pic)
Collision(shipxcenter,shipycenter,rock4xcenter,rock4ycenter,shippic,rock4pic)
Collision(shipxcenter,shipycenter,rock5xcenter,rock5ycenter,shippic,rock5pic)
if laserxcenter+Pic.Width(laserpic)/2>rock1xcenter-Pic.Width(rock1pic)/2 and laserxcenter-Pic.Width(laserpic)/2<rock1xcenter+Pic.Width(rock1pic)/2 and laserycenter+Pic.Width(laserpic)>rock1ycenter-Pic.Width(rock1pic)/2 and laserycenter-Pic.Width(laserpic)/2<rock1ycenter+Pic.Width(rock1pic)/2 then
boomxcenter:=rock1xcenter
boomycenter:=rock1ycenter
DrawBoom(boomxcenter,boomycenter)
rock1xcenter := Rand.Int (10, 630)
rock1ycenter := maxy + 20
score:=score+1
end if
if laserxcenter+Pic.Width(laserpic)/2>rock2xcenter-Pic.Width(rock2pic)/2 and laserxcenter-Pic.Width(laserpic)/2<rock2xcenter+Pic.Width(rock2pic)/2 and laserycenter+Pic.Width(laserpic)>rock2ycenter-Pic.Width(rock2pic)/2 and laserycenter-Pic.Width(laserpic)/2<rock2ycenter+Pic.Width(rock2pic)/2 then
boomxcenter:=rock2xcenter
boomycenter:=rock2ycenter
DrawBoom(boomxcenter,boomycenter)
rock2xcenter := Rand.Int (10, 630)
rock2ycenter := maxy + 20
score:=score+1
end if
if laserxcenter+Pic.Width(laserpic)/2>rock3xcenter-Pic.Width(rock3pic)/2 and laserxcenter-Pic.Width(laserpic)/2<rock3xcenter+Pic.Width(rock3pic)/2 and laserycenter+Pic.Width(laserpic)>rock3ycenter-Pic.Width(rock3pic)/2 and laserycenter-Pic.Width(laserpic)/2<rock3ycenter+Pic.Width(rock3pic)/2 then
boomxcenter:=rock3xcenter
boomycenter:=rock3ycenter
DrawBoom(boomxcenter,boomycenter)
rock3xcenter := Rand.Int (10, 630)
rock3ycenter := maxy + 20
score:=score+1
end if
if laserxcenter+Pic.Width(laserpic)/2>rock4xcenter-Pic.Width(rock4pic)/2 and laserxcenter-Pic.Width(laserpic)/2<rock4xcenter+Pic.Width(rock4pic)/2 and laserycenter+Pic.Width(laserpic)>rock4ycenter-Pic.Width(rock4pic)/2 and laserycenter-Pic.Width(laserpic)/2<rock4ycenter+Pic.Width(rock4pic)/2 then
boomxcenter:=rock4xcenter
boomycenter:=rock4ycenter
DrawBoom(boomxcenter,boomycenter)
rock4xcenter := Rand.Int (10, 630)
rock4ycenter := maxy + 20
score:=score+1
end if
if laserxcenter+Pic.Width(laserpic)/2>rock5xcenter-Pic.Width(rock5pic)/2 and laserxcenter-Pic.Width(laserpic)/2<rock5xcenter+Pic.Width(rock5pic)/2 and laserycenter+Pic.Width(laserpic)>rock5ycenter-Pic.Width(rock5pic)/2 and laserycenter-Pic.Width(laserpic)/2<rock5ycenter+Pic.Width(rock5pic)/2 then
boomxcenter:=rock5xcenter
boomycenter:=rock5ycenter
DrawBoom(boomxcenter,boomycenter)
rock5xcenter := Rand.Int (10, 630)
rock5ycenter := maxy + 20
score:=score+1
end if
exit when lives=0 or score=20
end loop
cls
if lives<=0 then
Font.Draw ("Game Over! ", 320, 240, font1, 7)
elsif score>=20 then
Font.Draw ("You Win! ", 320, 240, font1, 7)
end if
|
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Silent Avenger

|
Posted: Mon Sep 25, 2006 9:53 pm Post subject: (No subject) |
|
|
Well here's a link to an example lower down on the list. There should be some code in there. I'm not sure if this is what you're looking for but I hope it helps.
Link: http://www.compsci.ca/v2/viewtopic.php?t=13379 |
|
|
|
|
 |
TokenHerbz

|
Posted: Tue Sep 26, 2006 1:30 am Post subject: (No subject) |
|
|
Alright, befor we go to your prblem, you should try to minimize all that reapeated code you have, make it much nicer, and understandable.
i cant follow your flawd math to help solve this right now, as you have
var shipxcenter : int := 290 (that really the center of your ship? or your x chords for the ship)
anyways, i think you problem lies in the start of the collide codes as they all are: laserxcenter+Pic.Width(laserpic)/2, note that "+"... i think that should be a "-" on say, collition with the left/bottom?
I really cant understand your coding, and the lack of comments don't help me at all...
**tears** i tried? |
|
|
|
|
 |
TheOneTrueGod

|
Posted: Tue Sep 26, 2006 8:19 am Post subject: (No subject) |
|
|
Ya, this is most probably a calculation error. One way that I try to use when debugging square-based collision is to ONLY write the up/down collision, and then test it. Once I have done this, then I comment it out, and ONLY write the left/right collision, and test that. Once I have that working, I uncomment up/down collision, and test the two together. All should go smoothly . |
|
|
|
|
 |
BenLi

|
Posted: Thu Sep 28, 2006 6:32 pm Post subject: (No subject) |
|
|
several things that might make your life easier:
- get rid of picMErge if possible (it slows you down crop picture?)
- code: | if chars (chr(ORD_SPACE)) |
is the same as
- code: | var rock1xcenter : int := Rand.Int (10, 630)
var rock1ycenter : int := maxy + 20
var rock2xcenter : int := Rand.Int (10, 630)
var rock2ycenter : int := maxy + 20
var rock3xcenter : int := Rand.Int (10, 630)
var rock3ycenter : int := maxy + 20
var rock4xcenter : int := Rand.Int (10,630)
var rock4ycenter : int := maxy + 20
var rock5xcenter : int := Rand.Int (10,630)
var rock5ycenter : int := maxy + 20 |
this is fairly repetitive. Use arrays for something like this
code: |
var rockxcenter : array 1..5 of int
var rockycenter : array 1..5 of int
for i:1..5
rockxcenter (i):= Rand.Int (10,630)
rockycenter (i):= maxy + 20
end for
|
same with your rock pictures
also, i noticed several silmilar procedures
code: |
procedure DrawShip (x, y : int)
Pic.Draw (shippic, x, y, picMerge)
end DrawShip
procedure DrawRock (x, y ,rockpic: int)
Pic.Draw (rockpic, x, y, picMerge)
end DrawRock
procedure DrawBoom(x,y :int)
Pic.Draw(boompic,x,y,picMerge)
end DrawBoom
procedure DrawLaser (x, y : int)
Pic.Draw (laserpic, x, y, picMerge)
end DrawLaser
|
it should be
code: |
proc drawstuff (x,y,pic:int)
Pic.Draw (laserpic, x, y, picMerge)
end drawstuff
|
and to use the array:
code: | Collision(shipxcenter,shipycenter,rock1xcenter,rock1ycenter,shippic,rock1pic)
Collision(shipxcenter,shipycenter,rock2xcenter,rock2ycenter,shippic,rock2pic)
Collision(shipxcenter,shipycenter,rock3xcenter,rock3ycenter,shippic,rock3pic)
Collision(shipxcenter,shipycenter,rock4xcenter,rock4ycenter,shippic,rock4pic)
Collision(shipxcenter,shipycenter,rock5xcenter,rock5ycenter,shippic,rock5pic)
|
code: |
for i:1..5
Collision(shipxcenter,shipycenter,rockxcenter(i),rockycenter(i),shippic,rockpic (i))
end for
|
Good job, these are very minor stuff that you'll easily be able to fix up. Anyways, these are just efficiency things, pretty minor.
If you want help you should post the pictures you are using |
|
|
|
|
 |
|
|