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

2015/3/30

How can I detect the upcoming object infront of my actor ?

VivekRajagopal VivekRajagopal

2015/3/30

#
Please help me to detect and stop my actor from collision. My actor is a CAR. There are 5 cars in a city, they should not interfere with each other. So, I used traffic lights. Now, I need to know how to stop the cars when the signal goes RED (Stop). Also, the other cars which are following the first car should also stop with out any collision.
Super_Hippo Super_Hippo

2015/3/30

#
Since your issues are connected to each other, you don't have to open two discussion threads for that. If your crossroad has exactly horizontal and vertical streets, you can check for the lines without having the lines as an object. If you want to do it as an object, you can check for collision with the line like you do with other cars (check the other discussion thread). If not, it will be something like this: if, after moving the additional dx, the x-position of the car which comes from the left side is greater than the x-position of the left line, or the x-position of the car which comes from the right side is smaller than the x-position of the right line, or the y-position of the car which comes from the top is greater than the y-position of the top line, or the y-position of the car which comes from the bottom is smaller than the y-position of the bottom line... In my code, this looks quite difficult:
//in the world "Kreuzung"
public static int x = 600, y=600; //size of the world
public static int[] haltelinie = //{y0,x1,y2,x3}
        {y/2+140, x/2-141, y/2-141, x/2+140},
        //similar for the traffic light for the bicycle riders
the car checking for an upcoming traffic light or its line: explanations:
  • 'warten' is false if the car has a clear line to drive and isn't affected by other cars/traffic lights. The word 'warten' means 'waiting'
  • 'kreuzungPassiert' turns true when the car passed the traffic light
  • 'Kreuzung' means crossroad and is the world
  • 'ampel' is an array of all traffic lights
  • 'halteline' is the array of the lines - see above
  • 'seite' is a number between 0 (coming from bottom) and 3 (coming from right)
  • 'getState' returns 0 if the traffic light is red
  • the 'factor' method returns either -1 or 1 depending from which side the car is coming
if (!warten && !kreuzungPassiert && Kreuzung.ampel[seite].getState()==0 && (seite%2==0 && faktor()*getY()<faktor()*(Kreuzung.haltelinie[seite]+faktor()*50) || seite%2==1 && faktor()*getX()<faktor()*(Kreuzung.haltelinie[seite]+faktor()*50)))
{
    //... breaking and other things
}

private int faktor()
{
    return -((((seite+1)/2)%2)*2-1);
}
On the other hand, if the car is finally standing in front on the line, it should check for the traffic light to turn green to start accelerating again. To be honest, you probably won't be able to use this code in your scenario, but hopefully you understand how I did it, so you can find a way to do it.
You need to login to post a reply.