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

2016/6/16

Collision issues

grizzly1 grizzly1

2016/6/16

#
Hey guys that is how far I got with my collision, the problem is just that if i jump on a block from underneath it my charakter gets stuck some kind of in the block. Does anyone spot the mistake?

?
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
public class Rainer extends Objects
{
    private int speed = 7;    
    private int vSpeed = 0;
    private int acceleration = 1;
    private int jumpPower = 10; //Variable negativ*
     
    public void act() 
    {
        move(4);
        checkFall();
    }
     
    public void move(int moveRange)
    {
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("right")) 
        {
            dx++;
        }
         
        if (Greenfoot.isKeyDown("left"))
        {
            dx--;
        }
         
        if (Greenfoot.isKeyDown("space"))
        {
            dy = dy - 10;
             
             
             
        }
         
        for (int i = 0; i < moveRange; i++)
        {
            setLocation(getX() + dx, getY());
            if (getOneIntersectingObject(Block.class) != null) setLocation(getX() - dx, getY());
            setLocation(getX(), getY() + dy);
            if (getOneIntersectingObject(Block.class) != null) setLocation(getX(), getY() + dy); 
             
        }
    }
     
    public void fall()
    {
        setLocation (getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }
     
    public boolean onGround()
    {
        Actor under = getOneIntersectingObject(Block.class);
        return under != null;   
    }
        
     
    public void checkFall()
    {
        if(onGround()) 
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
     
    public void jump()
    {
        vSpeed = -jumpPower;
        fall();
    }
     
 
 
     
}
danpost danpost

2016/6/16

#
I think the problem stems from the order that you are doing things. You are increase the speed after moving. If it is intersecting a block, you set the speed back to zero and no movement will occur.
grizzly1 grizzly1

2016/6/16

#
Ok I try to fix that, but I do think that my problem is the hitboxes. I don't know how to define them, or how they are defined? Can you help me with that?
danpost danpost

2016/6/16

#
grizzly1 wrote...
Ok I try to fix that, but I do think that my problem is the hitboxes. I don't know how to define them, or how they are defined? Can you help me with that?
It really does not matter what the hitboxes are at the moment. First, no matter what they are, you need to prevent getting stuck at an intersecting blocker. You can refine the code later. Once you make the adjustment I mentioned above, your actor should, at minimum, move out of it (even if just slowly).
grizzly1 grizzly1

2016/6/17

#
could u maybe give me a little code on how to fix that, I just can't figure it out myself
danpost danpost

2016/6/17

#
grizzly1 wrote...
could u maybe give me a little code on how to fix that, I just can't figure it out myself
danpost wrote...
I think the problem stems from the order that you are doing things. You are increase the speed after moving. If it is intersecting a block, you set the speed back to zero and no movement will occur.
What I said here suggests that you first increment the speed and then move. Switch lines 127 an 128 above.
You need to login to post a reply.