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

2017/11/30

Help with vector movement

HelpMe123 HelpMe123

2017/11/30

#
I am trying to get my Rocket class to move with vector movement. I have no smooth mover class and am using vector version 2.1. My rocket will inch forward when the up arrow is pressed but nothing more than move(x) happens. My code is below, any help would be great!
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * Write a description of class Rocket here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Rocket extends Actor
{
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int rotation;
    private int gunDelay = 0;
    public static int minGunDelay = 25;
    private Vector movement;
    private double x;
    private double y;
    public static boolean dead = false;
    public Rocket() {
       double j = .3;
       Vector drift = new Vector(0,.3);
       drift.add(drift);
       minGunDelay=25;
       dead = false;
    }
     
    public void act() {
        Shoot();
        rotation = getRotation();
        move();
        borderTravel();
        gunDelay++;
        kill();
    }
 
    public void Shoot() { //spawns a new Gun object
        if (gunDelay >= minGunDelay) {
            if ("space".equals(Greenfoot.getKey())) {
                getWorld().addObject(new Gun(rotation), getX(), getY());
                gunDelay = 0;
            }
        }
    }
     
    public void move() { //makes the wocket turn or boost when a certain key is pressed
        if(Greenfoot.isKeyDown("left"))
        {
            setRotation( getRotation() - 5 );
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setRotation( getRotation() + 5 );
        }
        if(Greenfoot.isKeyDown("up"))
        {
            Vector drift = new Vector(getRotation(),.3);
            drift.add(drift);
            x = getX() + drift.getX();
            y = getY() + drift.getY();
            setLocation((int) x, (int) y);
        }
    }
 
    public void borderTravel() { //makes the enemies go through the border and spawn on the other side
        int x = getX();
        int y = getY();
        int width = getWorld().getWidth();
        int height = getWorld().getHeight();
        if (x == 0) {
            setLocation(width-2, y);
        }
        if (x == width - 1) {
            setLocation(1,y);
        }
        if (y == 0) {
            setLocation(x, height-2);
        }
        if (y == height- 1) {
            setLocation(x,1);
        }
    }
 
    public void kill() { //detects when the gun is close to an asteroid and shoots removes them both
        List<Enemy1> enemies = this.getObjectsInRange(25, Enemy1.class);            
        for (Enemy1 e : enemies) {
            if (enemies.size()>0) {
                dead = true;
                getWorld().removeObject(enemies.get(0));
                getWorld().removeObject(this);
            }
        }       
    }
}
danpost danpost

2017/12/1

#
On lines 61 and 62, you use 'getX' and 'getY', which return 'int' values. So, your accuracy in location by using vectors is compromised. You not only need to save the speeds as 'double' values, but also the location coordinates.
HelpMe123 HelpMe123

2017/12/1

#
@danpost thanks! Quick question, how do I access the double values of X and Y? I now have this, but it immediately moves me to the bottom left/right of the screen.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * Write a description of class Rocket here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Rocket extends Actor
{
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int rotation;
    private int gunDelay = 0;
    public static int minGunDelay = 25;
    private Vector movement;
    private double x;
    private double y;
    public static boolean dead = false;
    private double exactX;
    private double exactY;
    public Rocket() {
       Vector drift = new Vector(0,.3);
       drift.add(drift);
       minGunDelay=25;
       dead = false;
    }
     
    public void act() {
        Shoot();
        rotation = getRotation();
        move();
        borderTravel();
        gunDelay++;
        kill();
    }
 
    public void Shoot() { //spawns a new Gun object
        if (gunDelay >= minGunDelay) {
            if ("space".equals(Greenfoot.getKey())) {
                getWorld().addObject(new Gun(rotation), getX(), getY());
                gunDelay = 0;
            }
        }
    }
     
    public void move() { //makes the wocket turn or boost when a certain key is pressed
        if(Greenfoot.isKeyDown("left"))
        {
            setRotation( getRotation() - 5 );
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setRotation( getRotation() + 5 );
        }
        if(Greenfoot.isKeyDown("up"))
        {
            Vector drift = new Vector(getRotation(),.3);
            drift.add(drift);
            exactX = exactX + drift.getX();
            exactY = exactY + drift.getY();
            setLocation((int) exactX, (int) exactY);
        }
    }
 
     
    public void borderTravel() { //makes the enemies go through the border and spawn on the other side
        int x = getX();
        int y = getY();
        int width = getWorld().getWidth();
        int height = getWorld().getHeight();
        if (x <= 0) {
            setLocation(width-2, y);
        }
        if (x >= width - 1) {
            setLocation(1,y);
        }
        if (y <= 0) {
            setLocation(x, height-2);
        }
        if (y >= height- 1) {
            setLocation(x,1);
        }
    }
 
    public void kill() { //detects when the gun is close to an asteroid and shoots removes them both
        List<Enemy1> enemies = this.getObjectsInRange(25, Enemy1.class);            
        for (Enemy1 e : enemies) {
            if (enemies.size()>0) {
                dead = true;
                getWorld().removeObject(enemies.get(0));
                getWorld().removeObject(this);
            }
        }       
    }
     
    public void setLocation(double x, double y)
    {
        exactX = x;
        exactY = y;
        super.setLocation((int) x, (int) y);
    }
 
}
danpost danpost

2017/12/1

#
The class java.util.Vector is not a (double rotation, double magnitude) type vector class. If you look at the documentation of the class, it extends the java.util.AbstractList class and creates an object that that is a collection (meaning elements -- objects -- can be added to it to create that collection. It has nothing to do with direction and force.
You need to login to post a reply.