• Create Account

Banner advertising on our site currently available from just \$5!

### #ActualTom KQT

Posted 18 August 2013 - 03:15 AM

static D3DXVECTOR3 dir(1.0f, 0.0f, 0.0f);
pos = pos + dir * moveSpeed * dt;

if(pos.x > maxX)
{
dir.x = -1.0f
}
else if(pos.x < minX)
{
dir.x = 1.0f
}


That works perfectly, i wouldnt of thought it would be such simple code

It can be even simpler with less redundant data:

static D3DXVECTOR3 dir(1.0f, 0.0f, 0.0f);
pos = pos + dir * moveSpeed * dt;

if(pos.x > maxX || pos.x < minX)
{
dir.x = -dir.x;
}

This way you are not repeating the number 1.0f (for the velocity value) three times in the code.

Btw, the code has one problem. Pos.x can go behind the maxX and minX values, because you modify it, then check whether it is behind the threshold and if it is you change the velocity. But position already is behind and you render it this way.

### #2Tom KQT

Posted 18 August 2013 - 03:14 AM

static D3DXVECTOR3 dir(1.0f, 0.0f, 0.0f);
pos = pos + dir * moveSpeed * dt;

if(pos.x > maxX)
{
dir.x = -1.0f
}
else if(pos.x < minX)
{
dir.x = 1.0f
}


That works perfectly, i wouldnt of thought it would be such simple code

It can be even simpler with less redundant data:

static D3DXVECTOR3 dir(1.0f, 0.0f, 0.0f);
pos = pos + dir * moveSpeed * dt;

if(pos.x > maxX || pos.x < minX)
{
dir.x = -dir.x;
}

This way you are not repeating the number 1.0f (for the velocity value) three times in the code.

Btw, the code was one problem. Pos.x can go behind the maxX and minX values, because you modify it, then check whether it is behind the threshold and if it is you change the velocity. But position already is behind and you render it this way.

### #1Tom KQT

Posted 18 August 2013 - 03:11 AM

static D3DXVECTOR3 dir(1.0f, 0.0f, 0.0f);
pos = pos + dir * moveSpeed * dt;

if(pos.x > maxX)
{
dir.x = -1.0f
}
else if(pos.x < minX)
{
dir.x = 1.0f
}


That works perfectly, i wouldnt of thought it would be such simple code

It can be even simpler with less redundant data:

static D3DXVECTOR3 dir(1.0f, 0.0f, 0.0f);
pos = pos + dir * moveSpeed * dt;

if(pos.x > maxX || pos.x < minX)
{
dir.x = -dir.x;
}

This way you are not repeating the number 1.0f (for the velocity value) three times in the code.

PARTNERS