I'm going to admit- yes this is for school- but no I'm not asking for anyone to do this for me, I just need some help as I am lost completely. The assignment was to use StdDraw to create a 1000 by 1000 canvas, and then load 300 plants on to the canvas and 20 herbivores on to the canvas. We were supposed to create a plant class and an herbivore class. A plant was a simple black circle, with a radius of 1 meter (1 unit of the canvas). They don't move but they are generated with random locations. Herbivores are similar, but they are a circle with a radius of 5 meters and they move- each individual Herbivore was only able to see in a 50 meter radius around itself, and IF there were any plants in that 50 m radius, it was supposed to slowly move towards the nearest one (not in a straight line, we haven't learned that yet, but like to the left/right then up/down) and once it had reached the plant the Herbivore would eat the plant and the plant would dissapear. If there ARENT any plants in the 50m radius outside of a herbivore, the herbivore should move in a random direction until there is a plant within 50 m around the herbivore. Then, it should do the same thing and eat it. This would continue until all of the plants were gone, at which point the simulation would end and the console would print out a report of how many plants each herbivore had eaten. I managed to get a good deal of it done- I made it so the plants could randomly generate and so the herbivores generated, but I can't seem to make them move correctly. Please take a look at my code, and if you have any ideas please advise. I'm totally lost.
Code is here: https://gist.github.com/anonymous/ff7c83e67c4570dae20d
I might try to run your code and change it later if I have time. I probably won't! The best approach would be to have one herbivore to start and get him moving right. After that's working you create some structure like an array or list that contains all the herbivores on the screen and information about each one like its position and velocity and whatever else is pertinent about it. That would be a herbivore instance and you could have an arraylist for example that contaned all the instances and their fields, etc. It occurs to me that this is like a collision game. In that, your would have a redraw handler that was connected to a system timer. In the redraw handler bur before the actual redraw you would calculate the new position of all the herbivores (sprites) based on their velocities and then, still before the actual redraw, take the center point of the sprite and add the radius. The center point would be the actual location that you maintain for it. Add the radius to that and see if it overlaps the radius of anything else and if it does you have a collision. The velocity will be an X coordinate with a value and a Y coordinate with a value. The value is the number of pixels to move it in that direction on the next redraw and therefore its speed. Setting the values to 0 would make it stop moving.
Collision detection can be an elaborate thing, you can look it up in wikipedia for example. Basically it works like this. You have the x,y position of two different things. If x and y is the same for both of them then they have collided. But - the things can then be only one pixel big! So what you do instead is add a radius to the x,y of one thing and subtact the radius from the x,y of the other thing and if it's >= then you have a collision. You do this for every possibility. You could figure out an algorithm from that info.
I just had a look at your code. It looks rather weird in places, but I did not spot any
What I did notice however, is that when you move a herbivore, then the x and y are
changed by 0.000001, if I'm right. Now, on a area size of 1000 x 1000 that isn't much.
It would take many redraws to let a herbivore move only one pixel. Am I correct on this one?
(because there's also scaling going on, but I could not quite follow it).
Another thing I noticed is that you test whether a herbivore is 'on a plant' by comparing
doubles with '=='. Now, that is notoriously unreliable!
I see a strange thing in your 'main' method. First you create 20 herbivores, then
you remove the first 10 again. What's the meaning of that?
But my main question is: what testing have you done so far? Did you check, after each update,
if the herbivores moved correctly, for instance? That a 'nearest plant' was indeed found? And if
so, did you set the instance members correct?
Start with only one herbivore, for testing purposes, like Guillermo suggested. Make sure to
start without any plants, then with one plant that is within distance of the herbivore, et cetera.
Let us know what your findings are.
There are three kinds of actuaries: those who can count, and those who can't.