Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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] Using minecraft ideas for code, and It doesn't work :(  (Read 1658 times)
0 Members and 1 Guest are viewing this topic.
Online wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


« Posted 2013-09-18 01:23:18 »

 Clueless
So... I looked into what minecraft did for blocks, and it was cool sounding to me. In Block.class, he defined a whole bunch of blocks, their traits, and used a seperate class for all the block-specific methods needed. Like this:

EXERPT FROM Minecraft's Block.java
1  
2  
3  
4  
5  
6  
7  
8  
public static final Block stone = (new BlockStone(1)).setHardness(1.5F).setResistance(10.0F).setStepSound(soundStoneFootstep).setUnlocalizedName("stone").setTextureName("stone");
    public static final BlockGrass grass = (BlockGrass)(new BlockGrass(2)).setHardness(0.6F).setStepSound(soundGrassFootstep).setUnlocalizedName("grass").setTextureName("grass");
    public static final Block dirt = (new BlockDirt(3)).setHardness(0.5F).setStepSound(soundGravelFootstep).setUnlocalizedName("dirt").setTextureName("dirt");
    public static final Block cobblestone = (new Block(4, Material.rock)).setHardness(2.0F).setResistance(10.0F).setStepSound(soundStoneFootstep).setUnlocalizedName("stonebrick").setCreativeTab(CreativeTabs.tabBlock).setTextureName("cobblestone");
    public static final Block planks = (new BlockWood(5)).setHardness(2.0F).setResistance(5.0F).setStepSound(soundWoodFootstep).setUnlocalizedName("wood").setTextureName("planks");
    public static final Block sapling = (new BlockSapling(6)).setHardness(0.0F).setStepSound(soundGrassFootstep).setUnlocalizedName("sapling").setTextureName("sapling");
    public static final Block bedrock = (new Block(7, Material.rock)).setBlockUnbreakable().setResistance(6000000.0F).setStepSound(soundStoneFootstep).setUnlocalizedName("bedrock").disableStats().setCreativeTab(CreativeTabs.tabBlock).setTextureName("bedrock");
    public static final BlockFluid waterMoving = (BlockFluid)(new BlockFlowing(8, Material.water)).setHardness(100.0F).setLightOpacity(3).setUnlocalizedName("water").disableStats().setTextureName("water_flow");


So every time you wanted to reference a block, you would do (I assume, since all the obfuscation for making the blocks is too hard to decipher):

1  
blocksinworld[0][1][1000] = Block.planks;


So I did that with my blocks:
1  
2  
3  
4  
5  
6  
7  
8  
9  
      Voxel[][] returning = new Voxel[2][2];
     
      returning[0][0] = (Voxel) Voxel.dirt.setPos(4, 0, 4);
      returning[0][1] = (Voxel) Voxel.dirt.setPos(4, 0, 5);
      returning[1][0] = (Voxel) Voxel.dirt.setPos(5, 0, 4);
      returning[1][1] = (Voxel) Voxel.dirt.setPos(5, 0, 5);


      return returning;


And my Voxel.java:
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  
public class Voxel extends Entity {
   Texture texture;
   
   public static final Voxel dirt = new VoxelDirt();
   
   
   public Voxel(String name) {
      super(name);
      try {
         texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/default_vox.png"), GL_NEAREST);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public void update() {
     
   }

   public void render() {
      glPushMatrix();
         glTranslatef(x, y, z);
         
      //renders all the vertices using vertex array objects. Don't judge.
     TVAO(24, 3, 2, new float[]{
            1,0,0, 1,1,0, 1,1,1, 1,0,1,
            0,1,0, 1,1,0, 1,1,1, 0,1,1,
            0,0,1, 1,0,1, 1,1,1, 0,1,1,
           
            0,1,1, 0,0,1, 0,0,0, 0,1,0,
            1,0,1, 0,0,1, 0,0,0, 1,0,0,
            1,1,0, 0,1,0, 0,0,0, 1,0,0,
            }, new float[] {
            0,0, 1,0, 1,1, 0,1,
            0,0, 1,0, 1,1, 0,1,
            0,0, 1,0, 1,1, 0,1,
            0,0, 1,0, 1,1, 0,1,
            0,0, 1,0, 1,1, 0,1,
            0,0, 1,0, 1,1, 0,1,
      }, texture);
     
      glPopMatrix();
   }
}


But that just produces this:


Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2013-09-18 02:21:59 »

I don't really understand.. Whats the problem here?

Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2013-09-18 02:30:21 »

If Voxel.dirt is 1 final object, then you are calling setPos on the same object over and over.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #3 - Posted 2013-09-18 02:35:38 »

Oh...
1 line condensing problems, we meet again...
Is there A way to do this in one line? Just wondering.

Online wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #4 - Posted 2013-09-18 02:41:53 »

Oh no! It does not work! [Btw, sorry if I am asking newbie questions. This is my first time I actually am using static]

1  
2  
3  
4  
5  
6  
7  
8  
      Voxel[][] returning = new Voxel[2][2];
     
      for(int x = 0; x < 2; x++) {
         for(int z = 0; z < 2; z++) {
            returning[x][z] = Voxel.dirt;
            returning[x][z].setPos(x, 0, z);
         }
      }


It just does the same thing!

EDIT: It is just rendering the last voxel! Perhaps I am still just setting the position of Voxel.dirt? But how?!

Offline Slyth2727
« Reply #5 - Posted 2013-09-18 02:45:56 »

With that code essentially what it (seems) to be doing is creating a stagger of blocks like this:

  ##
##

I may be incorrect but, as I don't fully understand your problem, I don't know how I can help. Would you mind explaining what your problem is more in depth?

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Online wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #6 - Posted 2013-09-18 02:47:50 »

All of the voxels are rendering in one spot. And it only looks like it is just one, but it is multiple ones into one place.

Offline Slyth2727
« Reply #7 - Posted 2013-09-18 02:51:28 »

Hm. Well in that case I suggest you go through and just recheck your loop and how you are using translate. You may just be pulling coordinates that equal zero. Are you setting the blocks x and y when you call the method setPosition? It may be an empty method Tongue. And if I was you I would create a chunk manager that renders all the blocks in that chunk. That way it is alot easier to render and load blocks that are in range.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Online wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #8 - Posted 2013-09-18 02:55:44 »

With that code essentially what it (seems) to be doing is creating a stagger of blocks like this:

  ##
##

I may be incorrect but, as I don't fully understand your problem, I don't know how I can help. Would you mind explaining what your problem is more in depth?


Oh, and that is the texture you are seeing. It is checkered.

Offline Slyth2727
« Reply #9 - Posted 2013-09-18 02:57:57 »

I didn't mean the little purple and black block, I meant the grass looking texture in the background which I am guessing now is a skybox.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #10 - Posted 2013-09-18 02:58:27 »

He's obviously setting the x and y. Thats just silly... Anyway. I have never used a final static instance of this code, I've always just used static. Basically - public static Tile Grass and then I call that and when I go to render the tile, I loop through the list of tiles and render using the loops x and y values. I don't believe I even stored the x and y values in the tile class.

Offline Slyth2727
« Reply #11 - Posted 2013-09-18 03:00:07 »

You'd be surprised how many times one can forget to set the values Wink

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Online wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #12 - Posted 2013-09-18 03:01:13 »

Trust me. I am setting the values. Otherwise, all the tiles wouldn't go to the same place. It would throw a NullPointerException, and close the game.

Offline Slyth2727
« Reply #13 - Posted 2013-09-18 03:02:51 »

Unless you initialize them to zero when you create them. I shouldn't have suggested that, my bad. I suggest you take any of opiops suggestions over mine though, he is more experience in voxels from what I have seen.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Online wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #14 - Posted 2013-09-18 03:04:19 »

Well, the question still remains! What is going on?! I am really suspicious of doing Voxel vox = Voxel.dirt;.

Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #15 - Posted 2013-09-18 03:04:38 »

I've never forgotten to set my values... No offense. And Wesley I'd recommend looking up the cherno project on YouTube and seeing how he does his tile systems. I basically stole his, and it works great. Its also exactly what you're trying to do here.

Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2013-09-18 03:14:14 »

Can you seriously not clone Voxel.dirt? Do you not understand OOP? Do learn how to use objects and polymorphism before you do any graphics programming at all.

Just because I'm benevolent, add a "copy()" or a "clone()" method so your 1-line-code stays:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
//Voxel.copy()
public Voxel copy() {
    return new Voxel(this);
}


//Add this construct to Voxel
public Voxel(Voxel other) {
    //copy all the variables
}


//magic!
returning[0][0] = (Voxel) Voxel.dirt.copy().setPos(4, 0, 4);
returning[0][1] = (Voxel) Voxel.dirt.copy().setPos(4, 0, 5);
returning[1][0] = (Voxel) Voxel.dirt.copy().setPos(5, 0, 4);
returning[1][1] = (Voxel) Voxel.dirt.copy().setPos(5, 0, 5);

Online wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #17 - Posted 2013-09-18 03:29:11 »

#told you that '.copy()' did not exist, but then you added it to the post

Oh, I see you added the code. Now it works. Thanks!

Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2013-09-18 03:34:51 »

#told you that '.copy()' did not exist, but then you added it to the post

Oh, I see you added the code. Now it works. Thanks!

Quote
add a "copy()" or a "clone()" method

I told you to *add* it, I just edited my post to supply it because I expected you to say it didn't exist Cranky

And either way, clone() does exist, however it exists in the Object class, requires you to implement the Cloneable interface, and is protected, so it cannot be called outside the Voxel class unless you override it and set it to public.

Online wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #19 - Posted 2013-09-18 03:42:48 »

Well, it worked! Thanks!
Here is the happy ending:

Online HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #20 - Posted 2013-09-18 07:17:09 »

The point of the Minecraft code was to have one instance of the object that could be in many places at once. What you should do when rendering is pass in the block's coordinates, and render based off that.

The block should not know where it is except when told to do something at a certain location.

You're using two very different design patterns together, and using bad methods to make them work together.

Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #21 - Posted 2013-09-18 08:09:46 »

Yup, the object's coordinates are already at its array location anyway, no need to create a new object for each location.

Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #22 - Posted 2013-09-18 10:46:54 »

Aha, thats what I do too. I alway knew it was a good idea to use that system... I really like it. Its flexible and pretty easy to use. thechernoproject on YouTube has a great tutorial on this system, I'd recommend checking it out wesley.

Offline lcass
« Reply #23 - Posted 2013-09-18 19:28:01 »

Right just to run through some stuff , one of the most efficient ways to render stuff like this is to store a new object(dirt) in each area of your matrix that you require it for example (x,y,z)  map[3][4][5] = blocks.getnew("dirt"); But have all the graphics and collision detection in a class such as dirtstatic() so you are not having to store a new value and then just use simple detection methods to find out which block to draw.
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  
public class staticdirt {
   private sprite icon;//setup the sprite object
  private String name = "dirt";//makes checking your objects much easier by comparing names not the actual objects
  private collisionbox col = new collisionbox(2,2,3,3);//if you need one
  private boolean loaded = false;//stops errors
  public staticdirt(core){//you only need to instantiate this once.
     icon = core.getsprite("location");//load the sprite
     loaded = true;
   
   }
   public sprite getsprite(){//use basic encapsulation
     if(loaded){
         return icon;
      }
   }
   public String getname(){
      return name;
   }
   public collisionbox getcolbox(){
      return col;
   }
}


public class dirt() extends block{
   //vars
  public dirt(){
      super();//constructor
  }
   //method overrides
}
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #24 - Posted 2013-09-18 19:47:37 »

Quick tip: class names always being with a capital letter. Class names, method names, and variable names all use CamelCase.

Online wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #25 - Posted 2013-09-18 22:30:01 »

Well, I will definetely use that system. Sorry if I didn't see the code too well in minecraft. I went to world-creator, and could not find the right thing! Well now I know why!
Anyways, this project only requires a small amount of voxels, so I won't need to implement any of that... yet.

Thanks everyone!

P.S.
Aha, thats what I do too. I alway knew it was a good idea to use that system... I really like it. Its flexible and pretty easy to use. thechernoproject on YouTube has a great tutorial on this system, I'd recommend checking it out wesley.

I did look at it, and it helped. Again, I am just making a VERY small amount of voxels. But this certainly taught me some things!

1  
this.setSolvedState(true);

Offline lcass
« Reply #26 - Posted 2013-09-19 22:29:02 »

Quick tip: class names always being with a capital letter. Class names, method names, and variable names all use CamelCase.
Yup sorry about that sloppy naming was in a rush with my maths homework!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

E.R. Fleming (10 views)
2014-07-29 03:07:13

E.R. Fleming (4 views)
2014-07-29 03:06:25

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

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

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

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

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

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

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

MustardPeter (44 views)
2014-07-16 23:30:00
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!