Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Game Mechanics / Re: Using Perlin Noise to generate an infinite world on: 2010-12-14 13:15:55
I'll repeat what SimonH said.  You have to feed coordinates, otherwise "edges" of your precomputed regions will not match.  Since you're combining multiple "frequencies" (or zoom/scale levels) of noise, the coordinates must match at all levels.

Ok, I see what was wrong. I was not using a real Perlin noise algorithm, but a function that creates noise by re-scaling a low-res noise bitmap... It's working fine now.
2  Game Development / Game Mechanics / Re: Using Perlin Noise to generate an infinite world on: 2010-12-13 15:47:31
Can't seem to get it to work perfectly.

I have this: (not the complete Perlin code here)

1  
2  
public PerlinNoise(int width, int height, int startFreqX, int endFreqX, int startFreqY, int endFreqY, float persistency,
            int density, float cloudSharpness, int detail, float amplitude, long seed)


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  
public void run()
      {
                        Random r = new Random();
                        r.setSeed(seed);
         BufferedImage temp = new BufferedImage(endFreqX, endFreqY ,
               BufferedImage.TYPE_4BYTE_ABGR);
         Graphics2D g = temp.createGraphics();
         // generate a low-res random image
         for (int i = 0; i < endFreqX ; i++)
         {
            for (int j = 0; j < endFreqY; j++)
            {
                                        int val = r.nextInt(255);
                               
               g.setColor(new Color(val, val, val, (int) (alpha * 0xFF)));
               g.fillRect(i, j, 1, 1);
            }
         }
         g.dispose();
         // re-scale the image up using interpolation (in this case, linear)
         image = new BufferedImage(width, height,
               BufferedImage.TYPE_4BYTE_ABGR);
         g = image.createGraphics();
         g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
               RenderingHints.VALUE_INTERPOLATION_BILINEAR);

         g.drawImage(temp, 0, 0, width, height, startFreqX, startFreqY, endFreqX , endFreqY , null);
         g.dispose();
      }
   }


With these parameters
(256, 128, 0, 24, 0, 12, 8.0f, 120, 0.10f, 5, 0.99f, 1l) i get a 256x128 b&w map.

Now, i want to work in 128x128 blocks. To get the first block, i enter:
(128,128, 0, 12, 0, 12, 8.0f, 120, 0.10f, 5, 0.99f, 1l)

And to get the second block to the right, i enter:
(128,128, 12, 24, 0, 12, 8.0f, 120, 0.10f, 5, 0.99f, 1l)

I almost works, you can clearly see the resemblance, but there's always a few pixels different near the edge. It's not acceptable for map generation.

What is wrong ?
3  Game Development / Game Mechanics / Using Perlin Noise to generate an infinite world on: 2010-12-13 15:09:55
So i have this function :

1  
2  
public PerlinNoise(int width, int height, int freq, float persistency,
            int density, float sharpness, int detail, float amplitude, long seed)


It generates me perfectly good Perlin Noise. I'm using it to create a 2d height map.

I just can't seem to figure out how to get an infinite world out of it. Say i generate a 1024x1024 map with it. That's cool, i have a 1024x1024 grid. Now, how do i get past that? If every pixel is a 3d block in-game, how can i calculate past the initial first 1024 ?

4  Games Center / Cube World Projects / Re: Infinite world architecture, how do you do it? on: 2010-12-10 21:08:32
Streaming in and out to disk isn't too bad, it can be done in a background thread so it won't interrupt your framerate and walking speed is slow enough that you have plenty of time to load things before you actually get near to them.

Yeah, but what about multi-player? If you have even 16 players going in different directions at the same time... Cheesy
5  Games Center / Cube World Projects / Re: Infinite world architecture, how do you do it? on: 2010-12-10 20:32:47
Ah, i see. Thanks for the link!

So he's using a procedural algorithm to generate the world whenever you reach an undiscovered chunk, which makes the world seem seamless.

Similar to a perlin noise, i guess, but in 3D?

It is interesting. Also, i didn't think of writing chunks to disk.

I'll have to test it, but it does mean that the program is constantly accessing the disk since a 16x16 chunk of land is really really small, maybe there's some optimization to do there. Although, the problem he has is that if you look up (or down for that matter), there's always a chance of seeing the top 128th cube of the sky, so you must have it in memory...
6  Games Center / Cube World Projects / Infinite world architecture, how do you do it? on: 2010-12-10 20:07:26

I was thinking of Minecraft the other day and i wondered how he did it. So, i tried coding something really fast using simple arrays, but i soon encountered a wall.

Say the center of the world is at (0,0,0) and the world is supposed to be infinite in the X and Y direction, you can't have negative arrays, even though the X and Y will have to be negative somehow.

For example, the center is at world[0][0] if the player moves left, you can't access the array with, say, world[-1][0];

So is the solution to start creating the world really far like at world[1073741823][1073741823], and just hope the player never reaches the end of the infinite... finite world? (of course, the "world" array would contain "chunks" of, says 16*16*16 cubes) Or you could have an even bigger array on top of "world", say, "universe" which would contain other "world" arrays?

Also, i read the old thread by Markus Persson, and he was talking about octrees and chunked rendering. He dismissed the former as being too slow in favor of chunked rendering (which i have no idea what it is exactly, besides what the name implies). I looked a bit into it, but it seems i lack the mathematical knowledge to understand the reasoning behind it's choice. Could someone explain it in plain english for me? Smiley

Well, where do i start? Is there a good read somewhere about this topic?
7  Game Development / Newbie & Debugging Questions / Re: Best way to do a "hover" mouse picking? on: 2010-12-07 14:27:19
You can use WakeupOnAWTEvent to wake up on mouse moved or dragged. Or you can use WakeupOnElapsedFrames(0) to wake up every frame.

Wow, thanks a lot.

I really thought i would have been harder than that. Smiley
8  Game Development / Newbie & Debugging Questions / Best way to do a "hover" mouse picking? on: 2010-12-07 14:00:42
Hello,

I need a way to get my program to do the mouse picking while the mouse is hovering over something else. It needs to be done, well... always, all the time. This is for my 3D map, which is composed of tiles, and i'd like the tile under the cursor to be highlighted in some way.

Currently, the picking only works when the mouse is clicked, left or right button, how do i change this?

I guess it would probably be really cpu-intensive to do it all the time, what's the best approach?

Here's my picking 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  
public class TileSelectionBehavior extends PickMouseBehavior{
   
   private DebugPanel debug;
   private GamePanel game;
   private ArrayList<Button2D> button2D;
     
   //orientation of selection vector
   private static final Vector3d SELVEC = new Vector3d(0.0, 0.0, 1.0);
   
   public TileSelectionBehavior( Canvas3D canvas, BranchGroup root, Bounds bounds, DebugPanel debug ){
      super(canvas, root, bounds);
      setSchedulingBounds(bounds);
      this.debug = debug;
      this.game = null;
           
      pickCanvas.setMode(PickCanvas.GEOMETRY_INTERSECT_INFO);
         
   }
   
   public TileSelectionBehavior( Canvas3D canvas, BranchGroup root, Bounds bounds, GamePanel game,
                          ArrayList<Button2D> button2D){
      super(canvas, root, bounds);
      setSchedulingBounds(bounds);
      this.game = game;
      this.debug = null;
      this.button2D = button2D;
     
      pickCanvas.setMode(PickCanvas.GEOMETRY_INTERSECT_INFO);
           
   }

   public void updateScene(int xpos, int ypos) {
     
      // testing button2D from top to bottom
      // for screen hierarchy
      if (debug == null){
      if (button2D.size() > 0 ){
      for(int i = 0; i < button2D.size(); i++){
         Button2D b2D = button2D.get(i);
            if (xpos > b2D.getPosX() & xpos < b2D.getPosX() + b2D.getWidth()){
               if (ypos > b2D.getPosY() & ypos < b2D.getPosY()+ b2D.getHeight()){
                  //within a button bound
                  game.clicked2D(b2D.getId());
                  return;
               }
            }
         }
      //return;
      }
      }
     
      //3D picking
      pickCanvas.setShapeLocation(xpos,ypos);
     
      Point3d eyePos = pickCanvas.getStartPosition();  //viewer location
     
      PickResult pickResult = null;
      pickResult = pickCanvas.pickClosest();
                 
      if (pickResult != null){
         
         //pickResultInfo(pickResult);
         if (mevent.getButton() == mevent.BUTTON1){      //left click
            if (debug != null){
               debug.clickedTile(pickResult);
            }else if (game != null){
               game.clickedTile(pickResult);
            }
         }else if (mevent.getButton() == mevent.BUTTON3){ //right click
            if (debug != null){
               //debug.clickedTile(pickResult);
            }else if (game != null){
               game.clickedRTile(pickResult);
            }
         }
      }
   }

}

9  Game Development / Newbie & Debugging Questions / Re: Weird Xp/Win7 bug on: 2010-11-29 16:20:42
If it's not multithreaded, what is

1  
while(  (selectedWorld == -1)  &  (editSelectedWorld == -1)  ){}


supposed to do then?  Tongue

Nothing Smiley  As you said, it's a Swing menu, so it's just waiting for the player to press either the world to play in, or select the editor to edit a a world. Once pressed, a JButton returns selectedWorld == 3 for example, if the user wishes to play in world 3.

Both values start a -1 since neither a world or an editor is selected.

Yeah, i know it's not the best approach, but at the time it seemed like a quick and working way of doing it...

Quote
Not Windows, but java if you use a Swing/AWT, because the event handling runs on it's own thread, the AWT Event Thread or EDT (Event Dispatcher Thread)

Hum... i'll have to check that now, thanks.
10  Game Development / Newbie & Debugging Questions / Re: Weird Xp/Win7 bug on: 2010-11-29 15:40:19
It's not a bug, it's your own fault...

I guess this is a multithreaded application and the selectedWorld and editSelectedWorld variables are updated from another thread. Since you do not synchronize the access to the variables, you might just read out the values from the cpu-cache and not the actual ones (maybe because they are updated from the other cpu on your laptop - I bet you have a dual-core laptop...).

Anyway thread safe access to ints is afaik (at least theoretically) platform dependent and should be avoided. You could get away with declaring them volatile, but I wouldn't bet on it. Learn about thread safe programming using the synchronized keyword and you will be fine.

On a sidenote: the logical AND operator in java is &&. A single & is a binary AND, which might do something completely different than you expect...

Well, no, it's not a multithreaded application. My laptop is dual-core (win7) and my other pc is quad core (but it has winXP installed), but nothing is actually multithreaded in my application. Unless you mean windows decides to multi-thread things without me knowing it?

About the '&', the way i understood it, a binary AND would test both values before continuing where a logical AND &&  will only test the first one?
11  Game Development / Newbie & Debugging Questions / Weird Xp/Win7 bug on: 2010-11-29 13:54:53

In my MainMenu class, i have this line, which waits for a player selection:

while(  (selectedWorld == -1)  &  (editSelectedWorld == -1)  ){}

In windows Xp, it works. Now, i've tried it on my laptop at home, using Win7, 64 bits, and there's no way it'll work. It simply doesn't seem to test either values.

But, if i add:

while(  (selectedWorld == -1)  &  (editSelectedWorld == -1)  ){

     System.out.println( selectedWorld + "," + editSelectedWorld );

}

It works.  Huh Huh
Is there any reason for that?
12  Game Development / Newbie & Debugging Questions / Re: 2D over 3D mouse question on: 2010-11-29 13:49:25
You can add it to the view transform group. Then it will follow the camera. However placing the object at the correct position can be tricky. Especially if you want to support the ultra complex Java3D view model. Also you will have problems with the hud being rendered behind the geometry in world. You'd have to make the hud elements transparent and disable depth test. Even then it can be drawn behind transparent objects in the world. That can be fixed by creating your own transparency sorter (com.sun.j3d.utils.scenegraph.transparency).

The problem with the way you're doing it now is that:
1) it is slow
2) the size of the hud your rendering is fixed to the size of the window when it started. If you try to resize the window you will see that it stretces. This makes it impossible to create pixel perfect huds.

Creating a hud in Java3D is impossible without it having some negative side effects.

Ouch, yeah i saw what you meant. Maybe i'll just set it to non-resizable for now, though. OTOH, is post-rendering that slow? Is is slower to draw lots of pixels each frame than calculating a few vertices + material of the billboard each frame (should still be the same number of pixels...) ?  Currently, i'm not noticing any FPS drop, but i guess that's due to not having an FPS counter, which i'll be adding right away. Smiley

Honnestly, i'll probably switch to openGL for my next project, but right now, i guess it's just too late.
13  Game Development / Newbie & Debugging Questions / Re: 2D over 3D mouse question on: 2010-11-26 18:30:39
That would work. Another option would be to place your 2D images in the 3D scene as planes facing the camera (so they appear 2D). That way the poly-picker could find them.

Oh yeah, i thought about that, but it would have been complicated to keep the plane at exactly the right position since you can move, rotate and zoom in every directions...
14  Game Development / Newbie & Debugging Questions / 2D over 3D mouse question on: 2010-11-26 17:57:05
Hello,

I have this Canvas3D (using Java3D) with my 3D scene in it, with a PickMouseBehavior, and then a JPanel on the side for debugging. Thing is, i also have some 2D images on top of my canvas3D (using postRender(), think of it as an overlay menu) which also has to be able to read the mouse input, but only if the mouse is over a 2D image, so that it won't go through the 3D picking instead.

How do you do this?

I figured i could somehow hack the picking behavior, but i'm not sure it's the best way. Any help or suggestion appreciated!

Thanks!

edit: I'm thinking of creating a class called Button2D containing the position and rectangle of each buttons in my overlay, add them in an ArrayList, pass it to the mouse picking and test it against the cursor's position before the actual 3D picking. If the cursor is withing bounds, return the Button2D's ID.

I guess it could work?

edit2: Well, it's working fine Smiley I guess i just needed to think "aloud", heh.  Anyway, if there's better solutions, i'd still like to hear them. Tongue
15  Game Development / Newbie & Debugging Questions / Re: Having multiples of the same mesh (3D) on: 2010-11-23 15:25:38
The cloning code looks correct. However I don't see you adding frestMesh to forestTemplateBG.

omg.... Thanks a lot!  It's working fine now Wink)

16  Game Development / Newbie & Debugging Questions / Re: Having multiples of the same mesh (3D) on: 2010-11-23 15:11:30
Ok, so this is simple:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
private void load3DAssets(){
      forestTemplateBG = new BranchGroup();
      String filename = "props\\forest.obj";
      ObjectFile f = new ObjectFile(ObjectFile.RESIZE);
       forestMesh = null;
      try {
         forestMesh = f.load(filename);
          } catch errors...
         
   }


But then, i'm not sure how to cloneTree it ?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
private void addTree(float posx, float posy, float posz){               
                 
      TransformGroup tg = new TransformGroup();
     
      tg.addChild(forestTemplateBG.cloneTree());                   <-------????
      tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
      Transform3D tr = new Transform3D();    
      tr.rotX( Math.PI / 2.0);  

      Vector3f translation = new Vector3f(posx, posy, posz + 0.1f);
      tr.setTranslation(translation);
      Vector3d scale = new Vector3d(0.5d, 0.5d, 0.5d);
      tr.setScale(scale);
     
      tg.setTransform(tr);
             
       forestBG.addChild(tg);
       forestBG.setPickable(false);
       forest.add( forestBG );
       
   }


"forest" at the end is an ArrayList of BranchGroup that i use to add to the scene with:

sceneBG.addChild( forest.get( forest.size()-1 ) );

each time i'm adding a forest to the game. (Working on the level editor right now.)

It might be clumsy but, but i'm still learning Wink

17  Game Development / Newbie & Debugging Questions / Re: Having multiples of the same mesh (3D) on: 2010-11-23 13:41:11
Or you can use cloneTree to shallow copies of the loaded mesh. This is what I do. Link + SharedGroups makes a lot of things like picking more complicated.

How do you use cloneTree exactly?

this is what i get: javax.media.j3d.RestrictedAccessException: Node: Cannot clone a live or compiled scenegraph

Does it change anything if i need to clone it at runtime?
18  Game Development / Newbie & Debugging Questions / Having multiples of the same mesh (3D) on: 2010-11-22 23:33:44
So i'm loading an .OBJ mesh (using the loader that comes with java) each time i need it but i'm sure there's a more efficient way of doing things, especially since i'll be needing a lot of the same mesh.

What's the more efficient way of doing this? Btw, i'm using Java3D. And to put into context, i'm building a 3D strategy game, the mesh in question being a "forest". So the only thing that would need to be different is the X,Y and Z position, (and maybe the texture and the animation, but im not there yet).

I thought of using the same solution i found with my textures, ie load them once at the first creation of the object as a static variable and re-using them over and over, but it doesn't seem to work with 3D mesh, it gives me errors when i try to attach it to the scene node. I don't have the code right now but if it can help, i'll post something tomorrow.

Thanks!
19  Game Development / Newbie & Debugging Questions / How to design a MTG-style card game? on: 2010-10-21 12:44:01
Hello, i have another rather basic question. While learning Java, i was coding a card-base game, somewhat similar to Magic: The Gathering. For those who don't know, MTG uses a very simple basic mechanic at it's root (you have lands - resources- and creatures and spells that can be played using these resources). The complex thing is that almost *every* card can bend, tweak or completely alter these basic rules while it is in play (one card could say, for example, that my opponent's creatures comes in play tapped).

So, while learning, my first try was to create a Card class that would contain all the basic information of the card - it's name, cost, power, toughness, flavor text, etc, and create these cards by hand. For example:

Card dwarf = new Card("Dwarf", "2RR", 2 , 2, "The dwarf is cool.", etc);

But while reading about design patterns, it became quite clear that this approach was completely wrong. It could work, but i would have to hard-code each card's ability, losing a lot of flexibility in my code, and it would be really hard to alter the card's basic rules dynamically.

Then i read about the "Strategy design pattern". Each card could have a set of Behaviors interfaces (EnterPlayBehavior, DieBehavior, AttackBehavior, and so on) that could be implemented by some specific abilities (for example, EnterPlayTapped could implement EnterPlayBehavior, making the card enter the game tapped). The problem is that each card would need to be a class in it's own.

public class Dwarf(){
   EnterPlayBehavior enterPlayBehavior;
   OtherBehaviors otherBehaviors;

public Dwarf(){
   EnterPlayBehavior = new EnterPlayTapped();
   OtherBehaviors = new SomeBehavior();

public void setEnterPlayBehavior(EnterPlayBehavior b){
   enterPlayBehavior = b;
}

//and so on for each behavior
}

It would be feasible and rather flexible (and good OO from what i've seen) but how can you manage to have over 500 classes that are only for your cards in your code? And then another 20 or so Behaviors interfaces, with around 5 or so different variations on these behaviors?

While, i'll continue reading on design patterns, i'm quite puzzled by how anyone would do this. Any input is greatly appreciated Smiley
20  Game Development / Newbie & Debugging Questions / Re: Gui and actionListener question... (solved) on: 2010-10-21 12:21:28
Thanks! Exactly what i was looking for Smiley
21  Game Development / Newbie & Debugging Questions / Gui and actionListener question... on: 2010-10-15 18:57:53
Hello, i figured this should be pretty simple, but i can't seem to get my head around it... I'm making a somewhat simple card-base game.

So, i have this class Gui() that draws a swing gui and also a method UpdateGui(Player p1, Player p2) inside the Gui class that updates the Gui depending on the players' cards and board position.

There is a button "Draw" and "Discard" that, i hope, should be self-explanatory... The problem is that my Draw and Discard listeners are in the Gui class, although when they are clicked, they should call, say, p1.DrawCard() or p1.Discard(), but my Gui class has no access to the players' methods.

So, i'm not sure where i messed up in my design, but how should i have done the thing so that the listeners are independent from the Gui? What the best way of doing something like this?
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

TehJavaDev (31 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (40 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (26 views)
2014-10-26 10:14:04

theagentd (32 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!