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

2013/12/10

Collision detection help

BobbyBorn2l8 BobbyBorn2l8

2013/12/10

#
I have been trying to create a simple collision detection with a bounding box So far I have got the X collision to work but it only works when moving to the right and when moving to the left it detects to the left of the x coordinate of the object
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
    int speed = 5;
    int value = 0;
    public void act()
    {
        checkKeys();
         
    }
    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("w"))
        {
            setLocation(getX(), getY() - speed);
        }
        if (Greenfoot.isKeyDown("s"))
        {
            setLocation(getX(), getY() + speed);
        }
        if (Greenfoot.isKeyDown("a"))
        {
            if (collisionDetection() != 2)
            {
                setLocation(getX() - speed, getY());
            }
        }
        if (Greenfoot.isKeyDown("d"))
        {
            if (collisionDetection() != 1)
            {
                 setLocation(getX() + speed, getY());
            }
        }
    }
    public int collisionDetection()
    {
        value = 0;
        if (isTouching(null))
        {
            Actor touching = getOneIntersectingObject(barrel.class);
            if ( (getX() + getImage().getWidth()) >= touching.getX())
            {
                value = 1;
            }
            else if ( (getX()) <= (touching.getX() + 50))
            {
                value = 2;
            }
            else
            {
                value = 0;
            }
        }
        return value;
    }
}
BobbyBorn2l8 BobbyBorn2l8

2013/12/10

#
Also where it says "(touching.getX() + 50)" I had the code "(touching.getX() + touching.getImage().getWidth()) This was to check to see if the left handside of the player is to the left of the barrels right side.
Entity1037 Entity1037

2013/12/12

#
Try taking some tips from existing scenarios. I've made one that prevents objects from moving into eachother. http://www.greenfoot.org/scenarios/9172
davmac davmac

2013/12/12

#
First, I suspect you don't realise that getX() returns the center of an object, not the left-hand side. Can you explain in what situations you want the collisionDetection() method to return values 2, 1 and 0?
BobbyBorn2l8 BobbyBorn2l8

2013/12/16

#
Sorry for the late response, Value = 1 when the right hand side of the player is greater than or equal to the left hand side of the barrel Value = 2 when the left hand side of the player is less than or equal to the right hand side of the barrel Value = 0 when it isn't colliding
davmac davmac

2013/12/16

#
Value = 1 when the right hand side of the player is greater than or equal to the left hand side of the barrel Value = 2 when the left hand side of the player is less than or equal to the right hand side of the barrel
Both of these things can be true at the same time - what should be returned then?
BobbyBorn2l8 BobbyBorn2l8

2013/12/17

#
Yes while that is true but for that to occur the "player" be inside the "barrel" my collision detection algorithm should stop the player from moving inside the barrel This is just a simple program in most cases that outcome shouldn't happen
BobbyBorn2l8 BobbyBorn2l8

2013/12/17

#
And anyway I decided to use more crude method Instead of detecting the collision and where it collided from and I have just got the player to adjust itself til there is no more collision. It is a little bit wonky when moving along a object but for the most part it works Thanks for the help anyway
davmac davmac

2013/12/17

#
The two things can be true without the player being inside the barrel. Consider: | player | | barrel | The right hand side of player is greater than the left hand side of the barrel, *and* the left hand side of the player is less than the right hand side of the barrel.
BobbyBorn2l8 BobbyBorn2l8

2013/12/17

#
The point of the collision algorithm I made is to stop it before it can intersect in the way you are describing When it detects |player||barrel| it will stop it moving any further INTO the barrel so I would not need to provide an solution to the outcome you outlined And thanks for the help I just figured out the problem with my collision code thanks to your little diagram
1
2
3
4
5
6
7
8
9
10
11
12
if ( (getX() + getImage().getWidth()) >= touching.getX()) 
            
                value = 1
            
            else if ( (getX()) <= (touching.getX() + 50)) 
            
                value = 2
            
            else  
            
                value = 0
            }
When it is colliding from the right hand side
( (getX() + getImage().getWidth()) >= touching.getX())
is true and because I am using a else if it never gets to test
else if ( (getX()) <= (touching.getX() + 50))
Until
( (getX() + getImage().getWidth()) >= touching.getX())
is false which is why the collision was being detected on the left side
davmac davmac

2013/12/17

#
The point of the collision algorithm I made is to stop it before it can intersect in the way you are describing When it detects |player||barrel| it will stop it moving any further INTO the barrel so I would not need to provide an solution to the outcome you outlined
But your use of isTouching(...) means you will only detect a collision when there is already a (small) overlap between the objects. Also, as I mentioned previously, getX() returns the center of the object, not the left edge. To find the left edge you need to subtract half of the width. To find the right edge you must add half the width (not the full width). Your code needs to be adjusted for this.
BobbyBorn2l8 BobbyBorn2l8

2013/12/19

#
I can give you my current code to test it yourself It works, it prevents the character from moving into the barrel
davmac davmac

2013/12/19

#
If it works, then there's no need for me to see it - good work.
You need to login to post a reply.