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.

Dark GDK / Collision with 2D sprites. Tile Based. PROBLEM>< pls help.

Author
Message
haliop
User Banned
Posted: 26th Dec 2008 23:27
hello all
i'm having some problem with collision.

i'm using a Tile Based



// Dark GDK - The Game Creators - www.thegamecreators.com

// the wizard has created a very simple project that uses Dark GDK
// it contains the basic code for a GDK application

// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"

// the main entry point for the application is this function
void DarkGDK ( void )
{
// turn on sync rate and set maximum rate to 60 fps
int Tiles[10][10] ={
4,4,4,4,4,4,4,4,4,4,
4,3,3,3,3,3,2,2,2,4,
4,3,3,3,2,2,2,2,2,4,
4,3,3,2,2,2,2,2,2,4,
4,4,4,3,2,2,3,3,2,4,
4,4,2,2,3,3,2,2,2,4,
4,3,2,2,3,3,3,2,2,4,
4,2,2,2,2,2,3,3,4,4,
4,3,3,2,2,3,2,2,2,4,
4,4,4,4,4,4,4,4,4,4};

int TileEndX = 0;
int TileNM = 0;

dbSyncOn ( );
dbSyncRate ( 60 );

dbSetDir("Tiles");
dbCreateAnimatedSprite(200,"soldier.png",3,4,1);
dbLoadImage("sand.jpg",2);
dbLoadImage("bush.jpg",3);
dbLoadImage("trees.jpg",4);
dbSetSpriteFrame(200,1);
dbSprite(200,dbScreenWidth()/2,300,1);
dbSetSpritePriority(200,1);
// our main loop

for (int y = 0; y < 10; y++)
{
TileEndX = 0;
for (int x = 0; x < 10; x++)
{
TileNM++;
dbSprite(TileNM,TileEndX,y*50,Tiles[y][x]);
TileEndX = dbSpriteX(TileNM) + dbSpriteWidth(TileNM);
}
}


while ( LoopGDK ( ) )
{
if (dbRightKey() && dbSpriteImage (dbSpriteCollision(200,0) != 4 ))
dbSprite(200,dbSpriteX(200) + 4,dbSpriteY(200),1);
// update the screen
dbSync ( );

}
for (int i = 0 ; i < 257; i++)
{
dbDeleteSprite(i);
dbDeleteImage(i);
}
// return back to windows
return;


as you can see in the code
i tried all kind of collision but with no succses ..
so eventually , i came up with the idea of
instead finding the Tile number i want the player to colide with

i want the Number of the Image therefor i don't care for what Number is the sprite i'm colliding with i just want its number..
i think i wrote it twice.. but well.. i'm pretty tired.............


so , well this for me dosen't work , but still i think its an awesome idea on how to make collide simple.. when we come to Borders of the tile map , or even when we have an active tile.

if we getting its Image number and not its Tile number(Sprite Number) we can do the same calc and if we want an extra we just add another if or else.. instead of going trough all tiles in a loop to check..

ok i write too much..
pls help with this , i'm not sure its correct but if it is i will be very happy.. for now i go to sleep.

ty for your time.
nadav.
haliop
User Banned
Posted: 26th Dec 2008 23:28
ohh.. sorry i wanted to do a code snippet..
i guess i don't know how sorry bout that
haliop
User Banned
Posted: 27th Dec 2008 10:31
i fixed the problem
which is very "stupid" problem...



as you can see in code in the IF i ask for SpriteIMAGE (col..

what i did is , ive calced the value for a varible meaning

setting..
int ImageNum = 0;

and in the while loop
ImageNum = dbSpriteImage(dbSpriteCollision(200,0) // 200 is our player.

this way i get the image number in the value
and since "4" is for trees .
if ImageNum != 4
if dbRightKey
if dbUpKey.. etc

now it works

so , for everyone who want a tile based sprite collision system.
this is a pretty simple one , with no For X
For Y .. less CPU time.

if i'm wrong, pls tell me if theres an easier way to achive this.

ty for your time.
nadav.
chanchan
15
Years of Service
User Offline
Joined: 17th Dec 2008
Location:
Posted: 30th Dec 2008 11:11 Edited at: 30th Dec 2008 11:12
wrap your code with [ code ] [ /code ] tag

(remove spaces in tag)

Always learning

Login to post a reply

Server time is: 2024-09-30 13:31:57
Your offset time is: 2024-09-30 13:31:57