• Create Account

### #ActualSuperVGA

Posted 22 November 2012 - 10:54 AM

I guess if you really intend to use "manhattan units" how about using
   if( ( abs(ballposition.x-balltarget.x) + abs(ballposition.y-balltarget.y) ) < (ballspeed * 2) )
{
balltarget.x = rand() % SCREEN_WIDTH;
balltarget.y = rand() % SCREEN_HEIGHT;
}


But as it seems to be about a ball, I feel inclined to suggest that you just use an actual velocity vector,
the root of the squares of dX and dY added. (Or don't do the root but work in squared units, if you can/want to.)

Even better, you could use a 2d vector struct
struct 2dmotionvector
{
float dx;
float dy;
float len;
float angle;
}

To define your velocity, and work with that instead of having only independent x and y speeds to deal with.
-You can use len and angle primarily, and then set dx and dy if you need it in later statements.
(What if you apply spin to the ball, or it bounces off a surface? Way easier with 2d vector math on composite types in my opinion)

### #3SuperVGA

Posted 22 November 2012 - 10:14 AM

I guess if you really intend to use "manhattan units" how about using
   if( ( abs(ballposition.x-balltarget.x) + abs(ballposition.y-balltarget.y) ) < (ballspeed * 2) )
{
balltarget.x = rand() % SCREEN_WIDTH;
balltarget.y = rand() % SCREEN_HEIGHT;
}


But as it seems to be about a ball, I feel inclined to suggest that you just use an actual velocity vector,
the root of the squares of dX and dY added. (Or don't do the root but work in squared units, if you can/want to.)

Even better, you could use a 2d vector struct
struct 2dmotionvector
{
float dx;
float dy;
float len;
float angle;
}

To define your velocity, and work with that instead of having only independent x and y speeds to deal with.
-You can use len and angle primarily, and then set dx and dy if you need it later.
(What if you apply spin to the ball, or it bounces off a surface? Way easier with 2d vector math on composite types in my opinion)

### #2SuperVGA

Posted 22 November 2012 - 09:53 AM

I guess if you really intend to use "manhattan units" how about using
   if( ( abs(ballposition.x-balltarget.x) + abs(ballposition.y-balltarget.y) ) < (ballspeed * 2) )
{
balltarget.x = rand() % SCREEN_WIDTH;
balltarget.y = rand() % SCREEN_HEIGHT;
}


But as it seems to be about a ball, I feel inclined to suggest that you just use an actual velocity vector,
the root of the squares of dX and dY added. (Or don't do the root but work in squared units, if you can/want to.)

Even better, you could use a 2d vector struct
struct 2dvector
{
float x;
float y;
}

To define your velocity, and work with that instead of having independent x and y speeds. (What if you apply spin to the ball, or it bounces off a surface? Way easier with 2d vector math on composite types in my opinion)

### #1SuperVGA

Posted 22 November 2012 - 09:53 AM

I guess if you really intend to use "manhattan units" how about using
   if( ( abs(ballposition.x-balltarget.x) + abs(ballposition.y-balltarget.y) ) < (ballspeed * 2) )
{
balltarget.x = rand() % SCREEN_WIDTH;
balltarget.y = rand() % SCREEN_HEIGHT;
}


But as it seems to be about a ball, I feel inclined to suggest that you just use an actual velocity vector,
the root of the squares of dX and dY added. Or don't do the root but work in squared units, if you can/want to.

Even better, you could use a 2d vector struct
struct 2dvector
{
float x;
float y;
}

To define your velocity, and work with that instead of having independent x and y speeds. (What if you apply spin to the ball, or it bounces off a surface? Way easier with 2d vector math on composite types in my opinion)

PARTNERS