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

2018/11/13

Making an object spawn randomly within a certain area

DanB1995 DanB1995

2018/11/13

#
I have a scenario where a car is required to drive round a racetrack. I am required to add objects within the racetrack and when the car hits an object, it stops the game. I'm stuck on adding random objects within the racetrack, I've figured out how to add random objects but sometimes they spawn outside of the racetrack, how can I combat this? I'm guessing I need to check what the coordinates are for the racetrack and if it's found a coord within the track it needs to spawn the object and if it isn't, then it should loop back to the check, but I'm not sure how to add this in Java. Any help would be great.
danpost danpost

2018/11/13

#
DanB1995 wrote...
I'm guessing I need to check what the coordinates are for the racetrack and if it's found a coord within the track it needs to spawn the object and if it isn't, then it should loop back to the check, but I'm not sure how to add this in Java.
Better would be to determine the x and y ranges where a car can be added first. Then the random choice can be made within the required area. (1) compute x range, giving { x1, x2 }; (2) compute y range, giving { y1, y2 }; (3) get random x value; (4) get random y value; (5) add car at location (x, y) Step 3 example:
1
int x = x1 + Greenfoot.getRandomNumber(1+(x2-x1));
DanB1995 DanB1995

2018/11/13

#
danpost wrote...
DanB1995 wrote...
I'm guessing I need to check what the coordinates are for the racetrack and if it's found a coord within the track it needs to spawn the object and if it isn't, then it should loop back to the check, but I'm not sure how to add this in Java.
Better would be to determine the x and y ranges where a car can be added first. Then the random choice can be made within the required area. (1) compute x range, giving { x1, x2 }; (2) compute y range, giving { y1, y2 }; (3) get random x value; (4) get random y value; (5) add car at location (x, y) Step 3 example:
1
int x = x1 + Greenfoot.getRandomNumber(1+(x2-x1));
Thanks for the input, The car has a set start position, it's the random objects it can potentially collide into which need to be randomly added within a specified area.
1
2
3
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
addObject(new wall1(), x, y);
This is the current code which I use to randomly spawn an object, I'm not sure what to add/change to make it so it spawns randomly within the racetrack.
danpost danpost

2018/11/14

#
DanB1995 wrote...
I'm not sure what to add/change to make it so it spawns randomly within the racetrack.
Just follow the steps outlined above.
DanB1995 DanB1995

2018/11/21

#
danpost wrote...
DanB1995 wrote...
I'm not sure what to add/change to make it so it spawns randomly within the racetrack.
Just follow the steps outlined above.
I honestly don't understand it, from my perspective it seems like you're just talking about the car. I only want the car to appear in one place therefore there is no "range" to where it can be placed. Also, I don't see how the car relates to the randomly placed object.
danpost danpost

2018/11/21

#
DanB1995 wrote...
I honestly don't understand it, from my perspective it seems like you're just talking about the car..
Don't fret -- the steps are the same for any object.
You need to login to post a reply.