Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

this is how i was chaning ball postion

if(...) //if ball goes to left of screen
  dx = -dx
if(...) //if ball goes right of screen
  dx = -dx;

i found some people using math.abs to move ball postion.

if(...)  //if ball goes to left of screen
    dx = Math.abs(dx);
if(...) //if ball goes right of screen
    dx = -Math.abs(dx);

which is better? if 2nd one than plz explain why.

share|improve this question
    
What does this have to do with Gimbal-lock? –  Nicol Bolas Apr 28 '13 at 5:06
    
It has nothing to do with gimbal lock. @newbi Please try to find appropriate tags for your questions. This one has no more to do with the question than point-clouds had to do with your last question about a breakout clone. –  Seth Battin Apr 28 '13 at 5:10
add comment

closed as too localized by Sam Hocevar, Byte56, Anko, msell, Trevor Powell May 1 '13 at 14:24

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

Neither is better. They are different functions. Their appropriateness is determined by what you are trying to do.

//negate a variable
dx = -dx;

//force a variable to be positive or negative
dx = Math.Abs(dx);
dx = -Math.Abs(dx);
share|improve this answer
add comment

Depending on the circumstances in some physics engines it may happen that two objects that overlap in one frame, and has their collision resolved, still overlap in the next frame. If they therefore have their collision resolved again it will typically reverse the original collision, resulting in the objects getting stuck together as they spend every second frame moving towards one another and every other frame moving equally away from one another.

The abs code is a simple fix for this, ensuring that the collision will only happen if the ball is moving towards the wall.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.