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

2013/8/26

Only 8 Directions?

olli28 olli28

2013/8/26

#
Hey there, I'm not very experienced in greenfoot so please excuse me if it's a very easy fault. In my program I wanted the actor to start off in a random direction (but not directly north, west, east and south), but it came out quite strange. Every value between 0 - 31 on int newRotation is making my Object visually turn, but anyway it still moves straight to the right side when I call this.move(1) . As soon as the value of newRotation is between 32 - xx (Haven't tried any further) the object moves with a 45 degree angle. So did I forget to import any class or anything else, or are there only 8 directions? This is a part of my code: public void act() { if(i==0){ int newRotation; newRotation = Greenfoot.getRandomNumber(360); while( (newRotation >= 85 && newRotation <= 95) || (newRotation >= 265 && newRotation <= 275) || (newRotation >= 355 || newRotation <= 5) || (newRotation >= 175 && newRotation <= 185)) { newRotation = Greenfoot.getRandomNumber(360); } i=1; this.setRotation(newRotation); } this.move(1); } Cheers & Thanks for your help.
danpost danpost

2013/8/26

#
That is a result of moving only 1 pixel at a time. You cannot land between pixels, so the resultant move will be in one of 8 directions. You can either increase the rate of movement (which will, in turn, increase the number of directions possible to move in) or you can track with double type fields the x and y location of the actor and use 'setLocation' instead of 'move' for repositioning. The current double values of x and y will need cast back to ints.
// with fields
double x, y;
// to set new location, use
setLocation((int) x, (int) y);
You will also have to calculate the change in position along both the vertical and horizontal axes using 'Math.cos (getRotation()*Math.PI/180' and 'Math.sin' of the same and add the calculated results to x and y before repositioning your actor.
davmac davmac

2013/8/27

#
See this discussion if you still don't understand.
You need to login to post a reply.