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

2019/4/28

Minor improvement to SmoothMover: rotation as double

TheGoldenProof TheGoldenProof

2019/4/28

#

An update suggestion for the SmoothMover class:

Using a double for rotation for even more precision. I just had to do this in a game I'm working on and I thought it was simple enough and important enough to be default. All I did was add a method to set the exact rotation, add a method to get the exact rotation, and changed the move(double) method to use the precise rotation. (I also changed setLocation(int, int) to call setRotation(double , double) just because it bugged me that move did that and it didn't, and it's shorter.)

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
 
/**
 * A variation of an actor that maintains a precise location and rotation (using doubles for the co-ordinates
 * instead of ints).  This allows small precise movements (e.g. movements of 1 pixel or less)
 * that do not lose precision.
 *
 * @author Poul Henriksen
 * @author Michael Kolling
 * @author Neil Brown
 *
 * @author Exact rotation: TheGoldenProof
 *
 * @version 3.0
 */
public abstract class SmoothMover extends Actor
{
    private double exactX;
    private double exactY;
     
    private double exactRot;
 
    /**
     * Move forward by the specified distance.
     * (Overrides the method in Actor).
     */
    @Override
    public void move(int distance)
    {
        move((double)distance);
    }
     
    /**
     * Move forward by the specified exact distance.
     */
    public void move(double distance)
    {
        double radians = Math.toRadians(getExactRotation());
        double dx = Math.cos(radians) * distance;
        double dy = Math.sin(radians) * distance;
        setLocation(exactX + dx, exactY + dy);
    }
     
    /**
     * Set the location using exact coordinates.
     */
    public void setLocation(double x, double y)
    {
        exactX = x;
        exactY = y;
        super.setLocation((int) (x + 0.5), (int) (y + 0.5));
    }
     
    /**
     * Set the location using integer coordinates.
     * (Overrides the method in Actor.)
     */
    @Override
    public void setLocation(int x, int y)
    {
        setLocation((double)x, (double)y);
    }
     
    /**
     * Set the rotation using exact degrees.
     *
     * Added by TheGoldenProof
     */
    public void setRotation(double degrees) {
        exactRot = degrees;
        super.setRotation((int)(Math.round(degrees)%360));
    }
     
    /**
     * Set the rotation using integer degrees.
     * (Overrides the method in Actor.)
     *
     * Added by TheGoldenProof
     */
    @Override
    public void setRotation(int degrees) {
        setRotation((double)degrees);
    }
 
    /**
     * Return the exact x-coordinate (as a double).
     */
    public double getExactX()
    {
        return exactX;
    }
 
    /**
     * Return the exact y-coordinate (as a double).
     */
    public double getExactY()
    {
        return exactY;
    }
     
    /**
     * Return the exact rotation (as a double).
     *
     * Added by TheGoldenProof
     */
    public double getExactRotation() {
        return exactRot;
    }
}
danpost danpost

2019/4/29

#
My QActor class, which provides smooth moving by allowing fractional moving has fractional rotation as well. See my Smooth Mover Demo scenario.
You need to login to post a reply.