need help with sprite collision and field location

Just starting out? Need help? Post your questions and find answers here.
SirCus
User
User
Posts: 12
Joined: Sat May 10, 2003 7:29 pm
Location: Germany
Contact:

need help with sprite collision and field location

Post by SirCus »

hello everone

I got PB 2 days ago and currently writing a Bomberman Clone

check this
for screenshot and code download if you want
http://de.geocities.com/mfurt2002/

my problem is as follows
at the moment the players can run trough walls
I need to code a collision detection.
At the moment I test with spritecollision()
if it collides it should write a message "kollision" at the screen.
but it does not

can anyone help me?

the other problem is

the player should be able to put a Bomb at the middle of a field
the current field should be the one the player is at least 50% on.
how can I detect this

thanks a lot for your ideas

SirCus
SirCus
User
User
Posts: 12
Joined: Sat May 10, 2003 7:29 pm
Location: Germany
Contact:

Post by SirCus »

just a short part of my code, feel free to check the whole one by clicking the above posted link. :wink:

this is how I detect if player collides with left upper wall.
PlayerSprite ID is 30
Wall ID is 10
the Sprites are all 50x50 pixels, so the coordinates of the left upper Wall are x-50, y-50 of Players coordinates.
... so far my thoughts.
the code looks like this:

Code: Select all

If SpriteCollision(30,PlayerAx,PlayerAy,10,PlayerAx-50,PlayerAy-50)
     StartDrawing(ScreenOutput())
     DrawText("Kollision")
     StopDrawing()
     EndIf
... and it does not work.
does anyone know why?
If I put the "real" existing coordinates for this wall into the code, (lets say x=0 and y=0 what is the first Wall in upper left corner) then it works fine.
but I dont want to hardcode all coordinates for walls of course.
Only with variables depending on Players Position it does not work.

thanks for your ideas and comments
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

Hi SirCur,

If you take a closer look at the description for SpriteCollision in the docs it says:
Tests to see if the two sprites are overlapping. If not, the result is 0. This routines compares the rectangular areas around the sprites, giving a very fast but not very accurate command (depending on the shapes of your sprites). Very useful for fast arcade games.
This means you must supply the coordinates for your player sprite as well as the coordinates for EVERY wall sprite you wish to test.

Ex: If SpriteCollision(30,PlayerAx,PlayerAy,10,WallX,WallY)


That is why 0,0 for your wall sprite works, becaus you have a wall sprite positioned at 0,0.

You must write a loop to test the coordinates of each wall sprite with the coordinates of your player sprite to see if they collide.

Make sense now? :)
SirCus
User
User
Posts: 12
Joined: Sat May 10, 2003 7:29 pm
Location: Germany
Contact:

Post by SirCus »

Hi Paul,
thanks a lot for your answer. This makes sense and is what I've assumed.

Then my Problem is: I have to write a routine to check 8 fields. (A player can collidate 8 fields at one point maximum)
8 Fields have 8x50x50=20000*2(for x and y)=40000 possible coordinates to test for every player and every PC-Antagonist.

If I have lets say 20 Antis, and 1 players and I check 21times 40000 =
840 000 coordinates PER Frame, this can make the code quite slow.

How do you guys solve this problem?
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

840000 coordinates per frame ?!!??!

I think you are still misunderstanding things.
You only need to check sprites. I'm quite sure you don't mean you will have 840000 sprites on the screen ;)

Why don't you look at the code for PB-Invaders or PB-Pong or even Lady's Garden to see examples of storing x/y coordinates for sprites and then checking for collision? Maybe that will give you a better understanding.

Good luck :)

PureBasic Resources Site:
http://www.reelmediaproductions.com/pb
SirCus
User
User
Posts: 12
Joined: Sat May 10, 2003 7:29 pm
Location: Germany
Contact:

Post by SirCus »

I know I have to check sprites :)

but let me explain how I (mis)understand this thing:

Lets say my first wall sprite is on coordinates x=0, y=0
the bitmap is 50x50 pixels.

our players coordinates (left upper coordinates) are at 50,70 so it does not yet collide since the wall ends at its right below end at 49,49, right?

What I tried to check is: check all pixels from 50,70 to 50-50, 70-50 -> 0,20 if there is a wall.
since the wall-sprites coordinates are at 0,0 it does not show as collide
If I move the sprite to 50,50 it should show as collide right?
anyway I need to check the walls correct coordinates what I dont know and therefore I need to check all 50x50 pixels if there is a sprite and if it is a wall.

true or wrong?

but I will look at ladys garden it looks similar to my game.

however, the problem with my code is, I dont "store" the coordinates for every wall bitmap. I just calculate the first ones coordinates, then paint it, then use the same variables to check the second one. the coordinates for first one are no longer available.
I guess I need to change my code so it stores the coordinates for all 192 backgroundimages. however I still have at least check 192 times for a collision. - hm...
User avatar
waffle
Enthusiast
Enthusiast
Posts: 129
Joined: Mon May 12, 2003 1:34 pm
Location: USA
Contact:

Post by waffle »

this is a good time to learn linked lists and structures.
I found the best example on collisions was Freds Weaponz game.
I loved it so much i modified it and played it for 3 months.

basically you define a structure...
WARNING THIS IS PSUDOCODE, NOT REAL CODE
structure MySprite
Bitmap.l
Left.l
Top.l
Width.l
Height.l
Class.l
endstructure

then define the list as
Newlist Walls.MySprite()

then later when you make the walls...

For Wall=1 to 30 .... 30 walls, left side of screen
Addelement Walls()
Walls.Bitmap=1 .... this is the image ID that you wish to use for a wall
Walls.Left=0
Walls.Width=50 .... width of all sprites
Walls.Top=50*Wall-50 ..... each wall is below the previous
Walls.Height=50 ... height of all sprites
next wall

Now, in your main game loop, you must draw all walls....

resetlist Walls()
while NextElement Walls()
pasteimageopaque Walls.Bitmap,Walls.Left,Walls.Top
wend


then when you check for collision against walls
collidewall=0
resetlist Walls()
while NextElement Walls()

perform you collision test here for each Wall

wend
Post Reply