This site requires JavaScript, please enable it in your browser!
Greenfoot back
Mr_Potato
Mr_Potato wrote ...

2024/2/1

Help make gravity.

Mr_Potato Mr_Potato

2024/2/1

#
hello, I am trying to make a bouncing ball that I want to make it bounce, and every bounce it loses speed(bounces less). This is the bounce code:
while(moving)
        {
            checkX = getX();
            checkY = getY();
            speed += acc;
            setLocation(x, y + (speed));
            acc += fric;
            //x += xAcc;
            if(xAcc > 0)
            {
                xAcc -= fric;
            }
            
            if(checkY > 386)
            {
                accel += fric;
                acc = accel;
                speed = 0;
        
                if(accel == 0)
                {
                    moving = false;
                }
            }
            
            Greenfoot.delay(1);
        }
x and y are aquired useing getX() and getY() at the beggining, acc is -10, accel is the acceleration after each bounce. My main issue right now is that it bounces too high after each ground hit, and the hit itself is kind of weird. BTW, I think there is an infinite loop somewhere because sometimes it just asks me to terminate the thing.
Mr_Potato Mr_Potato

2024/2/1

#
I did a separate accel variable because acc is being changed during the loop
Mr_Potato Mr_Potato

2024/2/1

#
Also, if you could tell me how I could change my current code, not make a whole new one would be great
Mr_Potato Mr_Potato

2024/2/1

#
another issue I found is that when the ball hits the ground a few times, the ball is lower but on the next bounce it goes higher, to the same height it started at.
nccb nccb

2024/2/1

#
To give a couple of pointers... The reason it sometimes says it's frozen is that Greenfoot is intended to just do one movement per act() call, then Greenfoot redraws between act() calls (as well as doing things like updating the status of the keys, mouse). By using a loop and Greenfoot.delay you are taking over the whole execution and if it lasts a long time then Greenfoot thinks your code must be stuck because the single act() call is taking forever. As for the acceleration I don't fully understand what your code is doing, but one way to do bouncing is to first implement gravity (which I think you roughly have) then when you bounce, set speed to be equal to negative speed, so the ball bounces up with the same force, and then from there gravity will continue to act. You will probably also want some kind of check if speed is very low to just become zero, rather than endlessly bouncing a tiny amount.
Mr_Potato Mr_Potato

2024/2/1

#
so if I understand correctly, I should make speed negative... ? and it's just bouncing higher. Not sure if I did exactly as you way of thought tho.
Mr_Potato Mr_Potato

2024/2/1

#
So, I changed the code a little, and It kinda works? it's just that when it bounces, the speed gets slower, and then it rises to a certain point, and just continues to be like that. This is the new code:
if(checkY > 386)
            {
                accel += fric;
                acc = accel;
                speed = speed + acc;
        
                if(acc == 0 || speed > 1)
                {
                    moving = false;
                }
            }
Mr_Potato Mr_Potato

2024/2/1

#
small change: I move speed = speed + acc; below the inner if, now it just falls, and bounces to the same height.
Mr_Potato Mr_Potato

2024/2/1

#
nope. I keep having the same problem. after a few bounces it just changes a lot.
danpost danpost

2024/2/2

#
Mr_Potato wrote...
nope. I keep having the same problem. after a few bounces it just changes a lot.
I think your problem is that you need the change in motion of the ball to be smaller than a single pixel, which impairs on the smoothness of the motion. Solution is to use floats or doubles to track the current location of the actor. Cast the values back to ints when setting location.
You need to login to post a reply.