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 / Problem with my collision code

Author
Message
hookkshot
17
Years of Service
User Offline
Joined: 12th Apr 2007
Location: Adelaide, Aus
Posted: 18th Jan 2012 15:14
ok so the problem is

when i run this code the game runs fine i can move units every thing but once i get to the section where it checks for distance between entities (called beDistance) it returns false and doesnt move the units.

can anyone see a problem with this code

for(int i = 0;i<entities.size();i++)
{
if(entities.at(i)->GetType() == 1)
{

if(logicTimer < GetTickCount())
{
///COLLISION SYSTEM///
int ix = entities.at(i)->GetX();
int iy = entities.at(i)->GetY();
for(int c=0;c<entities.size();c++)
{
//testI = beDistance(entities.at(0)->GetSprite(),entities.at(1)->GetSprite());
if(beDistance(entities.at(c)->GetSprite(),entities.at(i)->GetSprite()) < 225 && c != i)
{
testI++;
int cx = entities.at(c)->GetX();
int cy = entities.at(c)->GetY();

if(ix > cx)
{
ix++;
}
if(iy > cy)
{
iy++;
}
if(ix < cx)
{
ix--;
}
if(iy < cy)
{
iy--;
}

}
}
entities.at(i)->SetPos(ix,iy);
logicTimer = GetTickCount()+500;
///END COLLISION///
}
entities.at(i)->UpdateX();
entities.at(i)->UpdateY();
entities.at(i)->Update();
}
}


int beDistance(int a,int b)
{
int x = 0;
if(dbSpriteX(a) < dbSpriteX(b))
{
x = dbSpriteX(b) - dbSpriteX(a);
}else{
x = dbSpriteX(a) - dbSpriteX(b);
}
int y = 0;
if(dbSpriteY(a) < dbSpriteY(b))
{
y = dbSpriteY(b) - dbSpriteY(a);
}else{
y = dbSpriteY(a) - dbSpriteY(b);
}

int distance = (x*x)+(y*y);

return distance;
}

Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 18th Jan 2012 19:52
Show the stuct for "entities".
Seeing stuff like this
Quote: "if(beDistance(entities.at(c)->GetSprite(),entities.at(i)->GetSprite()) < 225 && c != i)
"
always bothers me. It may be functionally correct, but I don't trust the compiler to know what comparison to do first. I would break it up like
Quote: "if((beDistance(entities.at(c)->GetSprite(),entities.at(i)->GetSprite()) < 225) && (c != i))
"


The fastest code is the code never written.

Login to post a reply

Server time is: 2024-11-19 04:33:25
Your offset time is: 2024-11-19 04:33:25