Jump to content
  • Advertisement
Sign in to follow this  
deEnumerator

C++ function for reversing signed/unsigned variable?

This topic is 4523 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
What if the variable's value isn't known like below? Im working on the bottom 2 if's.


[source lang=cpp]
void Ball::mBounceOffPaddle
{
if (BALLy > 743 && BALLy < 760)//detect whether ball is in paddles Y region, if not-> dont test
{
if (BALLx >= PADDLEx - 57) || BALLx <= (PADDLEx + 57))//test for collision with paddle
{
if (BALLy <= (PADDLEy - 3) //test if collision took place on the side of the paddle
{
ballVelx = //change x velocity if the ball hit the side of the paddle
}
}
else ballvely =
}
}


Share this post


Link to post
Share on other sites
First of all, signed and unsigned variables are different to variables that have the value 1 and -1.

With signed variables, it means that the most significant bit is used to keep track of whether the variable is positive or negative. If the variable is unsigned, is means that the variable value is always positive and can hold a larger range of positive values that the signed variables can.

If you're working with signed integers you can do something like this:

int a = 23;
a = ~a + 1; // boolean NOT operation on 'a' and add 1


This performs 2's complement on the variable. I believe that this is how signed values are stored inside the computer. Anyway, try it out. It works.

Edit:: Why is it I always forget the simple things? "-a" <smacks self on head>

Share this post


Link to post
Share on other sites
Quote:
Original post by deEnumerator
What if the variable's value isn't known like below? Im working on the bottom 2 if's.


*** Source Snippet Removed ***


Then Madhed's method would still work and be the best way to go about it.

Share this post


Link to post
Share on other sites
well, seems you're doing some kind of arcanoid clone there. =)

Maybe you should rephrase your question... because to switch the sign
all you have to do really is i *= -1;

so if the ball hits the pedal in the center just do ballvely *= -1.
if it hits at the side do like above but change the x velocity too.

Share this post


Link to post
Share on other sites
Quote:
Original post by jfclavette
Uh... guys.. what's with multiplying by -1 ?

i = -i;


If at any chance, the OP wanted to make the velocity larger or small, he can easily do i *= -1.1 (for a 10% increase) or i *= .9 (for a 10% decrease) [wink]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!