Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 182 times)
0 Members and 1 Guest are viewing this topic.
Offline Cannonball7171

Senior Newbie


Exp: 1 year


Semi-Beginner Coder


« Posted 2014-05-15 07: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
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2014-05-15 08: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 08: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  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (24 views)
2014-07-24 01:59:36

Riven (22 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (54 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!