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

2015/1/28

Actors not turning correctly

pypenko pypenko

2015/1/28

#
I have a problem with a traffic simulator I am trying to build where the cars that spawn at the edges of the world all turn to the right side of the world. My code appears to be wrong in some way though I can't seem to figure out what is wrong. This is my code to turn left.
private void turnLeft(Actor i) {	
		if(this.getRotation() == RIGHT && this.getX() == i.getX()+15){
			this.setRotation(UP);
		}
		else if(this.getRotation() == DOWN && this.getY() == i.getY()+15){
			this.setRotation(RIGHT);
		}
		else if(this.getRotation() == LEFT && this.getX() == i.getX()-15){
			this.setRotation(DOWN);
		}
		else if(this.getRotation() == UP && this.getY() == i.getY()-15){
			this.setRotation(LEFT);
		}
	}
This is my code to turn right.
private void turnRight(Actor i) {
		if(this.getRotation() == RIGHT && this.getX() == i.getX()-15){
			this.setRotation(DOWN);
		}
		else if(this.getRotation() == DOWN && this.getY() == i.getY()-15){
			this.setRotation(LEFT);
		}
		else if(this.getRotation() == LEFT && this.getX() == i.getX()+15){
			this.setRotation(UP);
		}
		else if(this.getRotation() == UP && this.getY() == i.getY()+15){
			this.setRotation(RIGHT);
		}
	}
Would anyone with experience please tell me what I have wrong?
Super_Hippo Super_Hippo

2015/1/29

#
The initial rotation is 0 (to the right), if that is your problem. Then you should set the correct rotation either in the constructor or when creating the object. I don't really understand your methods though. In the 'turnRight' method you turn right if the other car which you passed to this method (I guess) is in front of this car. In the 'turnLeft' method you check if the other car is behind this car.
danpost danpost

2015/1/29

#
These two methods alone is not enough to figure out why it is not behaving like you want. Also, it is unclear as to what the actor 'i' refers to.
pypenko pypenko

2015/1/30

#
My apologies. The i Actor is an intersection and I have managed to figure out what I did wrong. The RIGHT, LEFT, UP and DOWN are rotation values, RIGHT = 0, LEFT = 180, UP = 270, DOWN =90. It helps me to keep things in perspective. Anyway, thanks for replying even when I didn't make things clear.
You need to login to post a reply.