Tilemap Scrolling problem- please help!

Started by
20 comments, last by ols 19 years, 3 months ago
OK, I am really looking for ideas to try rather than an absolute answer as I think that will be a bit complicated. Here is my problem. I have created a tilemap (its for a top-down driving game). The car drives on it fine and the map scrolls fine. The problems start when I add a second vehicle to the game. This is not under user control and so should remain in the same place on the map. I originally implemented this by simply subtracting the starting coordinates for screen drawing from the x and y coordinates of the second car. However this doesn ot seem to work, and when the cars are both on screen, the supposedly stationary car moves around the map - well, the map moves around it as the cars coordinates are stationary. My probelm therefore is that I dont seem to be able to convert accurately from map coordinates to screen space coordinates. The simple idea I had does not work, for what reason I cannot fathom. Each car has an x and y coordinate, an angle of rotation, a normalized vector of direction and a velocity - for the second car this is always 0. Map scrolling is calcuated using the vector and vecloity of the player controlled car. I have a feeling that the problem is to do with the vector and the velocity differences between the two cars, but really cant find the answer AT ALL. I hope I have given enough detail and that someone can give me a push in the right direction. Any help much appreciated, Ols
Advertisement
If you could give some code or an example of your program, I might be able to help you better.
Thanks for replying.

The code is quite split-up as parts are taken from files for the Player, the other cars and the map. Below is some relevant code from each file. The most important bits would seem to be for the movement and rendering...

//This is how the player moves and it works finevoid CPlayerCar::Move(){	//create vector of current direction by retrieving the cos(angle) and sin(angle)	//from the previously constructed rotation matrix	_vecDirection = D3DXVECTOR2(_matRot._11, _matRot._12);	//normalize the vector	D3DXVec2Normalize(&_vecDirection, &_vecDirection);	//increase the offset of the map i.e. scroll the map appropriately	_pMap->AddToOffsetX(_fVelocity*_vecDirection.x);	_pMap->AddToOffsetY(_fVelocity*_vecDirection.y);	//then increase the position of the car onscreen appropriately	_fPosX += (_vecDirection.x * _fVelocity);	_fPosY += (_vecDirection.y * _fVelocity);}//player rendering//renders the carbool CPlayerCar::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;	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;}


//These are the two functions that control the scrolling of the map//adds 'x' to current offset value and ensures that offset does not exceed its maxvoid CMap::AddToOffsetX(float x){	_fOldOffsetX = _fOffsetX;	if((_fOffsetX + x) < 0.0f)		_fOffsetX = 0.000001f;	else if((_fOffsetX + x) > MAXOFFSET_X)		_fOffsetX = MAXOFFSET_X;	else		_fOffsetX += x;}void CMap::AddToOffsetY(float y){	_fOldOffsetY = _fOffsetY;	if((_fOffsetY + y) < 0.0f)		_fOffsetY = 0.000001f;	else if((_fOffsetY + y) > MAXOFFSET_Y)		_fOffsetY = MAXOFFSET_Y;	else		_fOffsetY += y;}


//this is the AI movement functionvoid CAICar::Move(float TimeScale){	if(_fVelocity < _fMaxVelocity)		_fVelocity += (TimeScale * ACCELERATION);	Rotate();	_vecDirection = D3DXVECTOR2(_matRot._11, _matRot._12);	D3DXVec2Normalize(&_vecDirection, &_vecDirection);	_fPosX += (_vecDirection.x * _fVelocity);	_fPosY += (_vecDirection.y * _fVelocity);}//and its rendering functionbool CAICar::Render(LPDIRECT3DDEVICE9 pDevice){	if(!pDevice)		return false;	//if car is onscreen, then draw it	if((_pMap->GetOffsetX()-_pTextureInfo.Width) < _fPosX &&		((_pMap->GetOffsetX()+_fViewWidth)+_pTextureInfo.Width) > _fPosX &&		(_pMap->GetOffsetY()-_pTextureInfo.Height) < _fPosY &&		((_pMap->GetOffsetY()+_fViewHeight)+_pTextureInfo.Height) > _fPosY)	{		RECT srcRect;		srcRect.top = 0;		srcRect.left = 0;		srcRect.bottom = srcRect.top + _pTextureInfo.Height;		srcRect.right = srcRect.left + _pTextureInfo.Width;		tempX = _fPosX-_pMap->GetOffsetX();		tempY = _fPosY-_pMap->GetOffsetY();		D3DXVECTOR3 vCentre(0.0f, 0.0f, 0.0f);		D3DXVECTOR3 vPosition(tempX, tempY, 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;	}	return false;}


The final section of the code is presumably where the problem occurs, but I just cant get it right!

Thanks for any help you can give
I can't seem to find any issue with your code.
Quote:Original post by Tarviathun
I can't seem to find any issue with your code.


THATS THE PROBLEM!!

The code runs fine but the car moves at times it is not supposed to and i have NO idea why. Would it be possible to send you the current build so you can see the problem in action?
Yeah, sure. Upload it to some webspace and post a link. Also, if you want(but I don't know if I have time) you could post your full source and I might be able to go over that. However, I'm don't have a lot of time to learn and figure out how a new game works.
Thanks for your help thus far.

Current build is available from: http://members.gamedev.net/ols/zip/tiles.zip

The AI car is set on a straight course. Controlling the other car with arrow keys will quite quickly make the problem evident. Any help is so appreciated.

Thanks again for having a look

Ols
Difficult to see exactly what might be going wrong, but it seems as if you are drawing the AI car relative to the center of the screen, so that when the center of the screen changes, the AI car slides. The AI car should be drawn at an absolute position that does not take the center of the screen into account at all, so that changing the center doesn't affect the drawing position. It's hard to say exactly, though, without seeing more code.
Hi,

The third of the code segments above shows that the centre is being taken as (0,0,0). Is this then wrong?

I can upload all the code if it will help - but be warned that it is probably quite messy!
The car went right off the screen for me without pushing any buttons. Has the build been changed since you made the last post?? Also you need to put some sort of "time-based" delay in the render loop so that the frame rate isn't hardware dependent. I have 1370fps as an average on my computer. I would like to help so let me know.

-Nightbird

This topic is closed to new replies.

Advertisement