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

2016/11/19

How to apply gravity the simpliest way?

1
2
Astralman Astralman

2016/11/19

#
This is what I have, but it's not working. What else do I need?
Public class 'actor'
{
    public double x=0;
    public double y=0;
    public double xspeed=0;
    public double yspeed=0;
}

public void gravity()
    {
        x += xspeed;
        y += yspeed;
        xspeed += .01;
        yspeed += .02;
}
danpost danpost

2016/11/19

#
Either your fields are named wrong or you are not applying gravity as a constant. What you should start with, basically, is:
public class ActorClassName extends Actor
{
    private static final int GRAVITY = 1;
    
    private int vSpeed;

    public void act()
    {
        vSpeed += GRAVITY; // add constant acceleration due to gravity
        setLocation(getX(), getY()+vSpeed); // fall (or rise)
        // if collision, adjust location and zero vSpeed
    }
}
Astralman Astralman

2016/11/19

#
What is vSpeed? Is that the same as velocity?
danpost danpost

2016/11/19

#
Astralman wrote...
What is vSpeed? Is that the same as velocity?
Sorry -- you used 'yspeed'. 'vSpeed' is speed for movement along the vertical axis.
Astralman Astralman

2016/11/19

#
Yeah, I'm trying to make the object fall when I click "Run."
danpost danpost

2016/11/20

#
danpost wrote...
Sorry -- you used 'yspeed'. 'vSpeed' is speed for movement along the vertical axis.
I was just trying to point out that the two were basically the same.
Astralman wrote...
Yeah, I'm trying to make the object fall when I click "Run."
The code I gave above will do that.
Astralman Astralman

2016/11/20

#
It's too fast. I tried 0.5 but it's not moving. Also, would hSpeed make it go to the right or left?
private static final int GRAVITY = 1;
private int vSpeed;
private int hSpeed;
vSpeed += GRAVITY;
hSpeed += GRAVITY;
setLocation(getX(), getY()+vSpeed);
setLocation(getX(), getY()+hSpeed);
Would this make it go down and right?
danpost danpost

2016/11/20

#
There is no gravity along the horizontal (remove line 2 in the second set of code above).
Astralman Astralman

2016/11/21

#
OK But how do I slow down gravity?
Super_Hippo Super_Hippo

2016/11/21

#
Import the 'SmoothMover' class, then make your class subclass the SmoothMover class and finally, you can use for example '0.5' instead of '1' to slow it down.
Astralman Astralman

2016/11/27

#
I am on a SmoothMover class but it's not working. Is there a code I should add to Smoothmover?
Super_Hippo Super_Hippo

2016/11/27

#
I don't think you have to change anything in the SmoothMover class. Is the SmoothMover class the superclass of the class of your actor?
Astralman Astralman

2016/11/27

#
yes
Astralman Astralman

2016/11/27

#
When I changed it to 0.5 it doesn't fall; But "1" falls. I tried adding "double" but it's not working either
private static final double GRAVITY = 1;
Super_Hippo Super_Hippo

2016/11/27

#
How is it "not working" then? Maybe show the code of the class again.
There are more replies on the next page.
1
2