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

2013/10/15

Collisions, wall's, borders... I need help!

Aermus Aermus

2013/10/15

#
Hello, I'm new to Greenfoot and i'm trying to make a topdown racing game kinda like GTA 2. My world has streets, buildings etc. How can i stop my car driving trough the buildings? I know it has something to do with getOneIntersectingObject but i cant get it to work. Edit: the buildings are made of objects. http://i44.tinypic.com/23tr3ah.jpg
gdwd3 gdwd3

2013/10/15

#
here is a long way of doing it:
1
2
3
4
5
6
7
8
9
10
11
if(canSee(building.class))
{
    setlocation(getX()-1, getY())
}
 
 
public void canSee(Class clss)
{
    Actor actor = getOneObjectAtOffset(0, 0, clss);
    return actor != null;   
}
you can replace the -1 with +1 or remove it and put it after the Y to set back the vehicle when it connects with the building.
Aermus Aermus

2013/10/16

#
Thanks gdwd3 for your reply, i will test it later today. I will let you know if it works!
mohd95 mohd95

2013/10/16

#
can you upload the game so i can try it out as well
danpost danpost

2013/10/16

#
Normally, walls and borders are not really avoided. The way it is usually done is by allowing a move possibly into an object, then detecting if any object is there. If so, undo the move. This sequence is done in one act cycle and is not viewed in the visible world. Something like the following (as an example):
1
2
3
4
int speed = 2; // previously declared field
// in act
move(speed);
if (getOneIntersectingObject(Building.class) != null) move(-speed);
Aermus Aermus

2013/10/16

#
Thanks for the replys, this is what i made this far. http://www.greenfoot.org/scenarios/9627 danpost, thanks for helping. It's not working just yet because you can drive into the buildings backwards and the car glitches when you hit a building. Il post the code when i figure out how to fix it ;) Also the car glitches when steering, the image of the car turns a bit bu the car itself does not!
danpost danpost

2013/10/16

#
What version of java are you running? Your scenario would not load for me -- something about it being out of date.
Aermus Aermus

2013/10/16

#
Thats weird, i'm getting the same pop-up. Il try updating Java, maybe that will resolve the problem.
Aermus Aermus

2013/10/16

#
danpost wrote...
What version of java are you running? Your scenario would not load for me -- something about it being out of date.
It's a new Java update you need to install, it came out today. I'm not finished making all the obstacles yet, you can still pass trough some buildings :P
danpost danpost

2013/10/16

#
You pass through buildings by going backward into them. You are allowing movement using +speed and -speed, but only avoiding the obstacles by using -speed. So, instead of stopping, like when you go forward into a building, you glide right through them backwards. You need a local field to hold the initial movement and negate that value when trying to avoid a building. It probably would be easier just to combine the two methods ('spelerBesturing' and 'avoid'); but here it is without doing so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// in the Speler class
public void avoid(int d)
{
    if (getOneIntersectingObject(Obstakel.class) != null) move(-d);
}
 
public void spelerBesturing()
{
    int d = 0;
    if ( Greenfoot.isKeyDown("up") ) d += speed;
    if ( Greenfoot.isKeyDown("down") ) d -= speed;
    if (d != 0)
    {
        move(d);
        avoid(d);
    }
    if ( Greenfoot.isKeyDown("left") ) turn(-4);
    if ( Greenfoot.isKeyDown("right") ) turn(4);
}
And since 'avoid' is being called from the 'spelerBesturing' method, it needs to be removed from the 'Autodief' class 'act' method. Combining the two methods and controlling the turning better would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void spelerBesturing()
{
    int d = 0;
    if (Greenfoot.isKeyDown("up")) d += speed;
    if (Greenfoot.isKeyDown("down")) d -= speed;
    if (d != 0)
    {
        move(d);
        if (getOneIntersectingObject(Obstakel.class) != null)
        {
            move(-d);
            d = 0;
        }
    }
    if (Greenfoot.isKeyDown("left")) turn (-2*d);
    if (Greenfoot.isKeyDown("right")) turn(2*d);
}
However, with this, one is liable to get stuck between two obstacles.
Aermus Aermus

2013/10/19

#
danpost wrote...
You pass through buildings by going backward into them. You are allowing movement using +speed and -speed, but only avoiding the obstacles by using -speed. So, instead of stopping, like when you go forward into a building, you glide right through them backwards. You need a local field to hold the initial movement and negate that value when trying to avoid a building. It probably would be easier just to combine the two methods ('spelerBesturing' and 'avoid'); but here it is without doing so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// in the Speler class
public void avoid(int d)
{
    if (getOneIntersectingObject(Obstakel.class) != null) move(-d);
}
 
public void spelerBesturing()
{
    int d = 0;
    if ( Greenfoot.isKeyDown("up") ) d += speed;
    if ( Greenfoot.isKeyDown("down") ) d -= speed;
    if (d != 0)
    {
        move(d);
        avoid(d);
    }
    if ( Greenfoot.isKeyDown("left") ) turn(-4);
    if ( Greenfoot.isKeyDown("right") ) turn(4);
}
And since 'avoid' is being called from the 'spelerBesturing' method, it needs to be removed from the 'Autodief' class 'act' method. Combining the two methods and controlling the turning better would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void spelerBesturing()
{
    int d = 0;
    if (Greenfoot.isKeyDown("up")) d += speed;
    if (Greenfoot.isKeyDown("down")) d -= speed;
    if (d != 0)
    {
        move(d);
        if (getOneIntersectingObject(Obstakel.class) != null)
        {
            move(-d);
            d = 0;
        }
    }
    if (Greenfoot.isKeyDown("left")) turn (-2*d);
    if (Greenfoot.isKeyDown("right")) turn(2*d);
}
However, with this, one is liable to get stuck between two obstacles.
Hey Dan, I used your code, the players will get stuck after collision. Is there a way to make them uhm.. sort of bump off? Thanks!
danpost danpost

2013/10/19

#
You probably need to check for collision while turning also, and negate any turn that a collision occurs on.
You need to login to post a reply.