VOGONS


First post, by thrawn235

User metadata
Rank Newbie
Rank
Newbie

After you all helped me recently (very slow drawing with DJGPP)

I've started to work on a little game. wrote a graphics engine, and some management code for game objects and everything went fine until i encountered an incredibly wired bug.
I've been trying to figure it out for a few days now, but i'm at a loss.

The problem is hard to describe without a video but it only happens when i move up or down. left and right works fine and i use the same vector, and the same math for both directions...
if i go up, the player (and the camera that is attached to it) jumps all over the place, sometimes even outside of the framebuffer. it oscilates wildly for a few seconds, until it segfaults.
sometimes the sprite gets corrupted, sometimes i'm in between frames, and can see how my tiles are drawn in the middle of the screen, even though thats done outside of the viewable area.

the code segements that are responsible are rather simple:

Update() and Draw() are called once per frame, for each game object:

void Player::Update()
{

movement = Vector2D( 0.0f, 0.0f );
if( engine->input->KeyDown( KEY_UP ) )
{
movement = Vector2D( 0.0f, -2.0f );
}
else if( engine->input->KeyDown( KEY_LEFT ) )
{
movement = Vector2D( -2.0f, 0.0f );
}
else if( engine->input->KeyDown( KEY_DOWN ) )
{
movement = Vector2D( 0.0f, 2.0f );
}
else if( engine->input->KeyDown( KEY_RIGHT ) )
{
movement = Vector2D( 2.0f, 0.0f );
}
else
{
movement = Vector2D( 0.0f, 0.0f );
}


AddForce( movement );


Move();

Friction( 0.2f );

Vector2D centerPos = pos + Vector2D( width / 2, height / 2 );
engine->graphics->SetCamCenter( centerPos );

}
void Player::Draw()
{
char str[500];
sprintf(str, "movement=%f:%f\ndirection=%f:%f\npos=%f:%f\n", movement.x, movement.y, direction.x, direction.y, pos.x, pos.y );
engine->graphics->DrawText( Vector2D( 0, 9 ) + engine->graphics->GetCamPos() , 1, 0, str );

engine->graphics->DrawSprite( pos, tileSetID, tileIndex, 16 );
engine->graphics->DrawRect( pos, width-1, height-1, 2);
}

the methods called in update are:

void GameObject::AddForce( Vector2D newForce )
{
direction = direction + newForce;
}

void GameObject::Move()
{
pos = pos + (direction * engine->time->GetDelta());
//
}
void GameObject::Friction( )
{
if(direction.x != 0)
{
direction.x = direction.x * 0.8;
}

if(direction.y != 0)
{
direction.y = direction.y * 0.8; <- thats the evil line!!!!!!!!!!
}
}

If i uncomment direction.y = direction.y * 0.8; or set direction.y to 0. everything works fine,

even more strange is that if i print the values of all the variables involved, the are completely plausible. pos.x for examle stays the same all the time. even though the player jumps from left to right like crazy

i've tried do decouple the camera but the player still flies around like crazy. i've tried to not draw the sprite, just the red box. but that doesn't fix it either.

I dont even really know how to debug it further, since the variables are fine
If you guys can think of something.
or run in on some other platform to see if its my dosbox or something. id appreciate it

The whole project is there:
https://github.com/thrawn235/DosEngine
(ignore the source files in root. the proper source files are in the source directory. i cant delete them on github for some reason)

Reply 1 of 1, by thrawn235

User metadata
Rank Newbie
Rank
Newbie

Ah, nevermind.

I finally found it.
the pos vector is used by the graphics engine to calculate the start address for drawing a sprite.
it cant deal with floats (or decimal points in general) that error is multiplied in the y axix, because it is multiplied by screenwidth to find the starting scanline.

thanks anyway