Ols (Away)

Sign in to follow this  
  • entries
    18
  • comments
    17
  • views
    5587

Its fixed

Sign in to follow this  
ols

176 views

I have now solved the biggest problem I have had since starting the programming; the errant AI car!

The car was actively moving away from the Player car, for a reason that I was struggling to fathom. After a number of forum posts and a LOT of trial-and-error coding as well as an even greater amount of paper-and-pen maths, it became apparent that the problem was due to the vector that the AI car was pointing in i.e. its direction.

Any movement was always made relative to this vector and so was causing large problems. It finally came to me, potentially in a state of mild insanity from nothing else working, that I should try to translate the vector as well as making the other car movements. Therein was my solution.

Once the idea was found, the coding was pretty simple, and the code as it currently stands is:


bool CAICar::Render(LPDIRECT3DDEVICE9 pDevice)
{
if(!pDevice)
return false;

RECT srcRect;
srcRect.top = 0;
srcRect.left = 0;
srcRect.bottom = srcRect.top + _pTextureInfo.Height;
srcRect.right = srcRect.left + _pTextureInfo.Width;

D3DXVECTOR4 tempVec;
D3DXMATRIX mtxTranslate;
D3DXMatrixIdentity(&mtxTranslate);
mtxTranslate._41 = _pMap->GetChangeInOffsetX();
mtxTranslate._42 = _pMap->GetChangeInOffsetY();
D3DXVec2Transform(&tempVec, &_vecDirection, &mtxTranslate);
_vecDirection.x = tempVec.x;
_vecDirection.y = tempVec.y;
D3DXVec2Normalize(&_vecDirection, &_vecDirection);

_fPosX-=(_Player.GetVelocity()*_vecDirection.x) + _pMap->GetChangeInOffsetX();
_fPosY-=(_Player.GetVelocity()*_vecDirection.y) + _pMap->GetChangeInOffsetY();

D3DXVECTOR3 vCentre(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vPosition(_fPosX, _fPosY, 0.0f);

_pSprite->Begin(D3DXSPRITE_ALPHABLEND);
_pSprite->Draw(_pTexture, &srcRect, &vCentre, &vPosition, D3DCOLOR_COLORVALUE(1.0f, 1.0f, 1.0f, 1.0f));
_pSprite->End();

return true;
}




I leave this original code here to look back-on in future days, and also to help any other poor bastard who is having similar problems!

At last, I can rest easy......until the next problem
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now