This site requires JavaScript, please enable it in your browser!
Greenfoot back
Çðæyœn
Çðæyœn wrote ...

2019/4/6

turnTowards method turns objects infinitely fast

Çðæyœn Çðæyœn

2019/4/6

#
I am attempting to make a game where police cars follow a 'player' car that is controlled using arrow keys. I used the turnTowards method to achieve this, but the police cars turns extremely fast. I am trying to make it so the police cars are faster than the player car, but the police cars turn slower than the player car, so that the police cars can be dodged by the player car. Is there any way to make an object turn at a specified speed that is following another object? I have already tried using a timer, but now the turning of the police cars are choppy and not smooth anymore. I am still a beginner, so thanks for any help. Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class Police extends Actor
{
    private int delay;
    private static final int DELAY = 20;
     
    boolean isTouchingEdge() {                              //checks if car object(s) are touching edge of world
        if(this.getX() == 0) {
            this.setLocation(590, this.getY());
        }
 
        if(this.getX() == 599) {
            this.setLocation(10, this.getY());
        }
 
        if(this.getY() == 0) {
            this.setLocation(this.getX(), 390);
        }
 
        if(this.getY() == 399) {
            this.setLocation(this.getX(), 10);
        }
         
        return true;           
    }
     
    /**
     * Act - do whatever the Police wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        move(5);
        isTouchingEdge();
         
        if (delay < DELAY) {
            delay++;
            return;
        } else {
            delay = 0;
            Actor car = (Actor)getWorld().getObjects(Car.class).get(0);
            turnTowards(car.getX(), car.getY());
            isTouchingEdge();
        }
    }   
}
danpost danpost

2019/4/6

#
You need to compare the current rotation to the rotation after turning toward the car to determine which direction to turn. This can be done as follows:
1
2
3
4
5
6
7
int currentRotation = getRotation();
Actor car = (Actor)getWorld().getObjects(Car.class).get(0);
turnTowards(car.getX(), car.getY());
int facingRotation = getRotation();
if (facingRotation - currentRotation > 180) facingRotation -= 360;
if (facingRotation - currentRotation < -180) currentRotation -= 360;
setRotation(currentRotation+(int)Math.signum(facingRotation-currentRotation));
Çðæyœn Çðæyœn

2019/4/6

#
int currentRotation = getRotation(); Actor car = (Actor)getWorld().getObjects(Car.class).get(0); turnTowards(car.getX(), car.getY()); int facingRotation = getRotation(); if (facingRotation - currentRotation > 180) facingRotation -= 360; if (facingRotation - currentRotation < -180) currentRotation -= 360; setRotation(currentRotation+(int)Math.signum(facingRotation-currentRotation));
is there an integer value I can enter that represents the how fast the police car turns in this code? With this current code, the police cars now turn too slow.
danpost danpost

2019/4/7

#
Çðæyœn wrote...
is there an integer value I can enter that represents the how fast the police car turns in this code? With this current code, the police cars now turn too slow.
Let us say you had:
1
int turnSpeed = 2;
then the turn amount can be represented by:
1
int turnAmount = turnSpeed*(int)Math.signum(facingRotation-currentRotation);
and the setting of the new rotation would be coded as:
1
setRotation(currentRotation+turnAmount);
However, to avoid over-turning (like if only a turn of 1 would face the actor, you may not want to turn past facing by turning more than 1), you would have to find the difference in the two rotations and use a minimum value between that and turnSpeed.
You need to login to post a reply.