Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved] [Libgdx] Vector2 won't set  (Read 2452 times)
0 Members and 1 Guest are viewing this topic.
Offline Cannonball7171

Senior Newbie


Exp: 1 year


Semi-Beginner Coder


« Posted 2014-05-15 05:23:56 »

So I have this code right here:

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  
//...
//put package and imports here
//...

public class Map implements Disposable {
    //...
    //put other variables here
    //...

    private Vector2[] cellAroundPlayer;
    private float offset;
 
    public Map(String mapTmx, float tileSize, float camTileSize, Player player1) {
        //...
        //other stuff here
        //...
        cellAroundPlayer = new Vector2[4];
        Arrays.fill(cellAroundPlayer, new Vector2());
        offset = 1f;
    }
   
    public void render() {
        //...
        System.out.println("Player Pos: " + player.getMiddleOriginX() + ", " + player.getMiddleOriginY());
        cellAroundPlayer[0].set((int)player.getMiddleOriginX(), (int)(player.getMiddleOriginY() + offset));
        cellAroundPlayer[1].set((int)player.getMiddleOriginX(), (int)(player.getMiddleOriginY() - offset));
        cellAroundPlayer[2].set((int)(player.getMiddleOriginX() + offset), (int)player.getMiddleOriginY());
        cellAroundPlayer[3].set((int)(player.getMiddleOriginX() - offset), (int)player.getMiddleOriginY());
       
        System.out.println("Cells around player: " + cellAroundPlayer[0] + ", " + cellAroundPlayer[1] + ", " + cellAroundPlayer[2] + ", " + cellAroundPlayer[3]);
       
        //...
    }
   
    //put other methods here
}


So you probably think: hmm a Vector2[4] which contains four Vector2()'s and they get set to the positions of the cells around the player.

The first println() says:
1  
Player Pos: 0.5, 3.5


So this means that (int)player.getMiddleOriginX() = 0 and (int)player.getMiddleOriginY() = 3 right?
And since offset = 1, this means that:

1  
2  
3  
4  
cellAroundPlayer[0] //0, 4
cellAroundPlayer[1] //0, 2
cellAroundPlayer[2] //1, 3
cellAroundPlayer[3] //-1, 3


(Note: 0, 1, 2, 3 means North, South, East and West)

But that's not what the second println() says!

1  
Cells around player: [0.0:3.0], [0.0:3.0], [0.0:3.0], [0.0:3.0]


What?

I could be doing something wrong, and that the answer is really obvious Clueless, but I couldn't seem to find it. Any help would be appreciated  Smiley

"If you're going through Hell, keep going." -Winston Churchill
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2014-05-15 06:00:31 »

1  
2  
cellAroundPlayer = new Vector2[4];
Arrays.fill(cellAroundPlayer, new Vector2());


You may think that you're filling the array slots with 4 distinct vector instances, but this is what actually happens:
1  
2  
3  
cellAroundPlayer = new Vector2[4];
Vector2 tmp = new Vector2();
Arrays.fill(cellAroundPlayer, tmp);
All your array slots refer to 1 Vector2 instance.

Do this instead:
1  
2  
3  
cellAroundPlayer = new Vector2[4];
for(int i=0; i<4; i++)
   cellAroundPlayer[i] = new Vector2();

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Cannonball7171

Senior Newbie


Exp: 1 year


Semi-Beginner Coder


« Reply #2 - Posted 2014-05-15 06:10:35 »

1  
2  
cellAroundPlayer = new Vector2[4];
Arrays.fill(cellAroundPlayer, new Vector2());


You may think that you're filling the array slots with 4 distinct vector instances, but this is what actually happens:
1  
2  
3  
cellAroundPlayer = new Vector2[4];
Vector2 tmp = new Vector2();
Arrays.fill(cellAroundPlayer, tmp);
All your array slots refer to 1 Vector2 instance.

Do this instead:
1  
2  
3  
cellAroundPlayer = new Vector2[4];
for(int i=0; i<4; i++)
   cellAroundPlayer[i] = new Vector2();


Thanks! Grin I guess the way I used Arrays.fill before was probably different for what I am doing now...

"If you're going through Hell, keep going." -Winston Churchill
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1668 views)
2018-06-10 19:43:48

EgonOlsen (1705 views)
2018-06-10 19:43:44

EgonOlsen (1156 views)
2018-06-10 19:43:20

DesertCoockie (1584 views)
2018-05-13 18:23:11

nelsongames (1184 views)
2018-04-24 18:15:36

nelsongames (1708 views)
2018-04-24 18:14:32

ivj94 (2532 views)
2018-03-24 14:47:39

ivj94 (1757 views)
2018-03-24 14:46:31

ivj94 (2834 views)
2018-03-24 14:43:53

Solater (970 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!