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

2017/4/19

Help with jumping, gravity, and cliffs.

jcboz jcboz

2017/4/19

#
This is kind of a strange problem, however in my code I have made it so that there is a physics system where my actor can jump, and when he does he is brought back down until he hits the ledge which is another actor, its pretty simple. However, whenever my actor falls from a high enough height and the veloctiy is high enough, the actor falls somewhat into the ledge (image/actor). This is kind of hard to explain but below is the coding I have for the gravity and jumping. I can also make a video of the problem if that would help. Thanks guys.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public boolean onGround()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2-2 , Ledge.class);
        return under != null;
    }
     
    public void setVSpeed(int speed)
    {
        vSpeed = speed;
    }
     
   public void fall()
   {
       setLocation ( getX(), getY() + vSpeed);
       vSpeed += acceleration;
        
   }
    
   public void checkFall()
   {
       if (onGround())
       {
           vSpeed = 0;   
       }
       else
       {
           fall();
        }
   }
and this is the code for jumping
1
2
3
4
if (Greenfoot.isKeyDown("space"))
   {
        setLocation (getX(), getY()-17);  
   }
Yehuda Yehuda

2017/4/19

#
Since your actor is moving by many pixels at a time it's jumping into the Ledge. The following code will make your actor stay on top of your ledge.
1
2
3
while (isTouching(Ledge.class)) {
    setLocation(getX(), getY() - 1);
}
jcboz jcboz

2017/4/19

#
I tried adding that code into mine, however when I did it kept me from leaving the ground at all. I tried playing around with it and I kept getting similar results but none fixed the issue.
danpost danpost

2017/4/19

#
jcboz wrote...
I tried adding that code into mine, however when I did it kept me from leaving the ground at all. I tried playing around with it and I kept getting similar results but none fixed the issue.
You need to show how you tried to implement the code. Post your revised class codes.
Yehuda Yehuda

2017/4/19

#
jcboz wrote...
I tried adding that code into mine, however when I did it kept me from leaving the ground at all. I tried playing around with it and I kept getting similar results but none fixed the issue.
That might be simply because your onGround() method isn't detecting anything (I can't see your jumping code, but I'm assuming you do 'if onGround()'). If you do '- 2' then you're checking two pixels into your image, it should either not be there at all or be a '+ 1' (something like that).
danpost danpost

2017/4/19

#
BTW, when jumping, you are just setting the location of the actor to a higher position. This does not do anything for the vertical speed of the actor. Line 3 in your jump code should be:
1
vSpeed = -17;
jcboz jcboz

2017/4/19

#
danpost, I changed the jump code and that fixed my initial issue of falling into the ground, however now when I jump, if the space key is held in my actor does not stop increasing in height. How can I make it so that after jumping a certain height, the actor comes back down? Heres all my code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Juno here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Juno extends Actor
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    private GreenfootImage image5;
    private GreenfootImage image6;
    private int imageTimer;
    private static final int acceleration = 1;
    private static final int speed = 3;
    private static final int jumpStrength = 14;
    private int vSpeed = 0;
     
     
    /**
     * Act - do whatever the Juno wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        checkFall();
        move();
    }   
     
    public void move()
    {
       if (Greenfoot.isKeyDown("right"))
       {
           move(4);
           int span = 4;
        int imgCt = 3;
        imageTimer++;
        if (imageTimer == span)
        {
            setImage (image2);
        }
        if (imageTimer == 2*span)
        {
            setImage(image3);
        }
        if (imageTimer == 3 * span)
        {
            setImage (image1);
            imageTimer = 0;
        }}
       
        
        
         
       else
       {
           if (Greenfoot.isKeyDown("left"))
       {
           move(-4);
           int span = 4;
           int imgCt = 3;
           imageTimer++;
           if (imageTimer == span)
           {
               setImage (image5);
           }
           if (imageTimer == 2*span)
           {
               setImage(image6);   
           }
           if (imageTimer == 3 * span)
           {
              setImage (image4);
              imageTimer = 0;
           }
      }
       
      else
      {
          if (getImage() == image5 || getImage() == image6)
          {
              setImage (image4);
          }
          if (getImage() == image2 || getImage() == image3)
          {
              setImage (image1);
          }
      }
   }
    
   if (Greenfoot.isKeyDown("space"))
   {
        vSpeed = -14;
         
   }
    
   while (isTouching (Ledge.class))
   {
        setLocation(getX(), getY() - 1);   
   }
   }
     
    public Juno()
    {
        image1 = new GreenfootImage("1C(RA).png");
        image2 = new GreenfootImage("2C(RA).png");
        image3 = new GreenfootImage("3C(RA).png");
        image4 = new GreenfootImage("1C(LRA) 2.png");
        image5 = new GreenfootImage("2C(LRA) 2.png");
        image6 = new GreenfootImage("3C(LRA) 2.png");
        setImage(image1);
         
        }
         
    public boolean onGround()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2-2 , Ledge.class);
        return under != null;
    }
     
    public void setVSpeed(int speed)
    {
        vSpeed = speed;
    }
     
   public void fall()
   {
       setLocation ( getX(), getY() + vSpeed);
       vSpeed = vSpeed + acceleration;
        
   }
    
   public void checkFall()
   {
       if (onGround())
       {
           vSpeed = 0;   
       }
       else
       {
           fall();
        }
   }
    }
danpost danpost

2017/4/19

#
The 'acceleration' should slow the rise of the actor and bring it back down. You just need to add another prerequisite to jumping. The actor must be on the some ground object. Change line 95 to:
1
if (Greenfoot.isKeyDown("space") && onGround())
Yehuda Yehuda

2017/4/19

#
I assumed that was already done since the onGround method was shown originally.
jcboz jcboz

2017/4/19

#
Once again, thank you so much for your help, I really appreciate it!
Yehuda Yehuda

2017/4/19

#
If you reset/re-upload/reinitialize/re-choose your user image then even the little one should show your image not just the big one.
jcboz jcboz

2017/4/19

#
Oh okay, thank you! I wasn't sure why it was doing that haha.
Yehuda Yehuda

2017/4/19

#
jcboz wrote...
Oh okay, thank you! I wasn't sure why it was doing that haha.
It was doing that because there was an error, now it was fixed so you have to re-(whatever) your image.
You need to login to post a reply.