Skip to main content
GameDev.net gamedev.net
🔒 Locked

Simple Code Problem :(

Started by Bezzant Apr 12, 2001 at 12:31 PM 5 replies 500+ views
Original Post
Bezzant
Bezzant
Hi, This is a very simple code problem i just can''t seem to get my head around it heres my code:
  
float speedx = 0.2f;

x+=speedx;

if(x<=-5.0) //left side

{
speedx=+speedx; //invert the speed to + value

x+=speedx;
}

if(x>=40.0) //right side

{
speedx=-speedx  //invert the speed to - value

x+=speedx;
}
  
well this works, sort off the ball moves to the right of the screen then tries to move back but just bounces in a small area, it gets stuck. I understand why this is, its because the xspeed is only inverted while x is >= 40.0 so as soon as it is < 40.0 then the speedx goes back to a positive value. How can i get around this problem? I have tried numorous things but to no anvil Any help would be great my head is thumping... Thanks Alan
JonnyQuest
JonnyQuest
      
(...)
speedx=+speedx; //invert the speed to + value

(...)


that line is most likely the culprit.

what you're doing here is accomplishing exactly nothing

you know, to invert the sign, you have to multiply by (-1), NOT by (+1)

I think it should be:
speedx = -speedx


- JQ
Infiltration: Losing Ground
"Simpson, eh?" -Mr. Burns
"Excellent!" -the above

Edited by - JonnyQuest on April 12, 2001 1:53:40 PM
~phil
pwd
pwd
You should multiply speedx by (-1) in both situations...

0.2 * (-1) = (-0.2) and
(-0.2) * (-1) = 0.2

so...


float speedx = 0.2f;
x+=speedx;
if(x<=-5.0) //left side
{
//speedx=+speedx; //invert the speed to + value -does not work
speedx=-speedx; //This line does work
x+=speedx;
}
if(x>=40.0) //right side
{
speedx=-speedx //invert the speed to - value
x+=speedx;
}

Will do what you want it to

pwd

---
What makes you think I''m not going to like a goatse pic on my window? - nes8bit



E.A.R.E. To keep your children away from elephants!
Bezzant
Bezzant
Thanks, the code works, but still theres a problem:

if(ballx<=-5.0) //left side
{
speedx=-speedx; //********************
ballx+=speedx;
}
if(ballx>=40.0) //right side
{
speedx=-speedx; //**********************
ballx+=speedx;
}

where i have done loads of ************''s thats the problem bits the speedx is only inverted when the ballx is <=-5.0 or >=40.0 so as soon as it moves the opposite way it goes back, thus getting stuck if you get what i mean

I need a way to get around that ^ problem,

Thanks

Alan
pwd
pwd
Two ways that you could fix this...

if(ballx<=-5.0) //left side
{
speedx=-speedx; //********************
//ballx+=speedx; //Change me
ballx = -5.0 + speedx;
}
if(ballx>=40.0) //right side
{
speedx=-speedx; //**********************
//ballx+=speedx; //Change me
ballx = 40.0 + speedx;
}

You might get a little popping when the ball changes direction using that method.

The alternative would be to change the ifs...

if((ballx>=40.0) && (speedx > 0))

and

if((ballx<= -5.0) && (speedx < 0))

Will only change the direction of the ball if it is going the wrong way. Might be a little cleaner, but you may have the ball being drawn longer where it should not be... So either pop, or collide, your choice

pwd

---
What makes you think I''m not going to like a goatse pic on my window? - nes8bit



E.A.R.E. To keep your children away from elephants!
yb legal
yb legal
Top Of Loop
...
...
float speedx = 0.2f;
....
if(ballx<=-5.0) //left side
{
speedx=-speedx; //********************
ballx+=speedx;
}
if(ballx>=40.0) //right side
{
speedx=-speedx; //**********************
ballx+=speedx;
}

When you go back to the top of the loop speedx is initialized with 0.2 again.
This variable either needs to be static or global

"society prepares the crime, the criminal commits it" - FunLovinCriminals





Edited by - yb legal on April 12, 2001 4:02:54 PM
Twenty years from now you will be more disappointed by the things you didn't do than by the ones you did. So throw off the bowlines, Sail away from the safe harbour. Catch the trade winds in your sails. Explore. Dream. -Mark Twain
Bezzant
Bezzant
Thanks guys.. pwd it worked great! that little pop makes it look quite good, the ball hits the wall, then it looks compressed then it bursts away adds to the effect and thanks yb legal i''ve been trying to sort that code out for a day!!! it was giving me a headache but now it works great

Thanks again

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.