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

2017/1/1

How do you apply gravity?

new113 new113

2017/1/1

#
I have been looking around for awhile but I'm not making sense of it. When I use the index I can't find the "applyGravity" code.
danpost danpost

2017/1/1

#
Applying gravity is just adding a constant value to the vertical speed of an actor:
1
2
3
4
5
6
7
8
9
// with a class constant
public static final int GRAVITY = 1; // gravity constant
 
// and instance field
private int vSpeed; // for current vertical speed
 
// then, in act or method it calls
vSpeed = vSpeed + GRAVITY; // apply gravity
setLocation(getX(), getY()+vSpeed); // move vertically
Afterwards, collision with objects will be looked for. If 'vSpeed' is positive and an object is hit, then it came down on something and its position will need to be adjusted to place it above that object. If 'vSpeed' is negative and an object is hit, then it bopped it head on something and, again, positional adjustment may be necessary. The vertical speed should be set to zero if anything is hit regardless of the value of 'vSpeed'.
new113 new113

2017/1/1

#
Thank you
new113 new113

2017/1/1

#
Also what I want to do is keep the actor on a platform and if it comes off the gravity effect will happen. I have tried to use a code, if (!getIntersectingObjects(platform.class).isEmpty()) { move(0); } and its not been working (as you can tell I'm quite new to Greenfoot).
new113 new113

2017/1/1

#
Is there any where could I find codes like the code given for gravity? Thank you for your help so far.
danpost danpost

2017/1/2

#
new113 wrote...
Also what I want to do is keep the actor on a platform and if it comes off the gravity effect will happen.
That was what I was saying:
danpost wrote...
Afterwards (after the movement code given), collision with objects will be looked for. If 'vSpeed' is positive and an object is hit, then it came down on something and its position will need to be adjusted to place it above that object.
I have tried to use a code,
1
2
3
4
if (!getIntersectingObjects(platform.class).isEmpty())
{
    move(0);
}
and its not been working (as you can tell I'm quite new to Greenfoot).
Yes. That is a newbee mistake. Telling something NOT to move does not accomplish anything as it will not move until some code has it move. You want the actor to potentially move vertically every act cycle. When standing on something, it will move down one and then move back up one. The combined result will be that the actor will remain at the same vertical height for consecutive act cycles. The individual steps within the execution of the act method are not shown to the user -- only the end result. So, after applying gravity and falling (or rising, depending on the sign of 'vSpeed'), you would do something like this:
1
2
3
4
5
6
int dir = (int)Math.signum(vSpeed); // 1, 0 or -1 (direction of vertical movement)
while(isTouching(platform.class))
{
    setLocation(getX(), getY()-dir); // edging back off the platform
    vSpeed = 0; // kill speed on hitting a platform
}
You need to login to post a reply.