Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
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   
  Show Posts
Pages: [1] 2 3 ... 5
1  Game Development / Game Mechanics / Re: Vectors and Reflection - Ball bouncing messed up by simultaneous collisions? on: 2014-07-16 02:45:59
Process only one object at a time. Move it, check for collisions, move it back if necessary. Make sure the game is in a valid state at the end of that process i.e. nothing overlaps. Only then go on to the next object.
2  Game Development / Newbie & Debugging Questions / Re: Generate random path on a 5x5 on: 2014-07-14 20:35:45
I added some comments to the code.
You could also read this to help you understand backtracking better.
http://en.wikipedia.org/wiki/Backtracking
3  Game Development / Newbie & Debugging Questions / Re: Generate random path on a 5x5 on: 2014-07-14 19:37:54
You definitely need backtracking if the path is much longer then 5.
e.g. Try to find a random path of length 25 on a 5x5 board. If you always discard the entire path once you reach a dead end, it's gonna take forever to get a solution.
Backtracking only discards as much of the path as necessary and is therefore much faster.
Here is the most simple backtracking algorithm I could come up with to solve your problem.

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  
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RandomPath {
  static int[][] matrix = new int[5][5];
  static List<int[]> movements = Arrays.asList(
    new int[] {1, 0}, //right
   new int[] {-1, 0}, //left
   new int[] {0, 1}, //down
   new int[] {0, -1}); //up
 
  public static void main(String[] args) {
    //a list of all the possible start positions
   ArrayList<int[]> startPositions = new ArrayList<>();
    for(int x = 0; x < matrix.length; x++) {
      for(int y = 0; y < matrix[0].length; y++) {
        startPositions.add(new int[] {x, y});
      }
    }
    Collections.shuffle(startPositions);
    for(int[] pos: startPositions) {
      if(findPath(pos[0], pos[1], 25, 1)) break;
    }
    printMatrix();
  }
 
  static void printMatrix() {
    for(int y = 0; y < matrix[0].length; y++) {
      for(int x = 0; x < matrix.length; x++) {
        if(x > 0) System.out.print(",");
        if(matrix[x][y] < 10) System.out.print(" ");
        System.out.print(matrix[x][y]);
      }
      System.out.println();
    }
  }
 
  //recursive method that uses backtracking to find a path
 //x, y: where to go next
 //maxN: target length of path
 //n: current length (actually the length of the path once x,y has been set to n)
 //returns true if a path has been found
 static boolean findPath(int x, int y, int maxN, int n) {
    //if x,y is outside the matrix or it's already part of the path we can return false
   if(x < 0 || x >= matrix.length ||
       y < 0 || y >= matrix[0].length || matrix[x][y] != 0) return false;
    matrix[x][y] = n;
    if(n == maxN) return true; // target length reached
   Collections.shuffle(movements);
    for(int[] move: movements) {
      if(findPath(x + move[0], y + move[1], maxN, n+1)) return true;
    }
    //if no path was found we backtack
   //i.e. we remove x, y from the path and return false
   matrix[x][y] = 0;
    return false;
  }
}
4  Game Development / Newbie & Debugging Questions / Re: Breaking a bullet's velocity into components on: 2014-06-22 01:33:48
Are you sure that angle is in radians?
5  Game Development / Shared Code / Re: json on: 2014-05-23 16:53:21
Here is another JSON parser. It's a recursive descent parser and should be able to handle all standard json 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  
import java.util.*;
import java.io.*;

class JSON {
    static LinkedList<Object> tokenize(Scanner in) {
        LinkedList<Object> list = new LinkedList<>();
        while(true) {
            in.skip("\\s*");
            String token = in.findWithinHorizon(",|:|\\[|\\]|\\{|\\}|\".*?\"|'.*?'|true|false|null|\\d*\\.\\d*|\\d+", 0);
            if(token == null) {
                if(in.hasNext()) throw new RuntimeException();
                else return list;
            }
            char c = token.charAt(0);
            if(c == '"' && token.endsWith("\"") || c == '\'' && token.endsWith("'")) list.add(token.substring(1, token.length()-1));
            else if(c >= '
0' && c <= '9' || c == '.' ) list.add(Double.valueOf(token));
            else if(token.equalsIgnoreCase("true") || token.equalsIgnoreCase("false")) list.add(Boolean.valueOf(token));
            else if(token.equalsIgnoreCase("null")) list.add(null);
            else list.add(token);
        }
    }
   
    static Object parseValue(LinkedList<Object> tokens) {
        Object elem = tokens.pop();
        if(elem.equals("{")) return parseJSON(tokens);
        else if(elem.equals("[")) return parseArray(tokens);
        else return elem;
    }
   
    static List<Object> parseArray(LinkedList<Object> tokens) {
        ArrayList<Object> list = new ArrayList<>();
        if(tokens.peek().equals("]")) {tokens.pop(); return list;}
        while(true) {
            list.add(parseValue(tokens));
            Object elem = tokens.pop();
            if(elem.equals("]")) return list;
            else if(!elem.equals(",")) throw new RuntimeException();
        }
    }
   
    static Map<String, Object> parseJSON(LinkedList<Object> tokens) {
        HashMap<String, Object> map = new HashMap<>();
        if(tokens.peek().equals("}")) {tokens.pop(); return map;}
        while(true) {
            String key = (String)tokens.pop();
            if(!tokens.pop().equals(":")) throw new RuntimeException();
            Object value = parseValue(tokens);
            map.put(key, value);
            Object elem = tokens.pop();
            if(elem.equals("}")) return map;
            else if(!elem.equals(",")) throw new RuntimeException();
        }
    }
   
    static Map<String, Object> parseJSON(Scanner in) {
        LinkedList<Object> tokens = tokenize(in);
        if(!tokens.pop().equals("{")) throw new RuntimeException();
        return parseJSON(tokens);
    }
   
    public static void main(String[] args) throws Exception {
        Scanner in = new Scanner(new File("test.json"));
        Map<String, Object> map = parseJSON(in);
        in.close();
        System.out.println(map);
       
    }
}
6  Game Development / Newbie & Debugging Questions / Re: Calculating probabilities - is this the right way? on: 2014-05-17 19:28:09
If you run the code a few times you'll notice that you don't get a consistent output distribution of 50%. You are just as likely to get 0,0 or 1,1 as you are to get the desired output of 0,1 or 1,0.

Which is exactly the behaviour we want. So no bug here. If you always got 0,1 or 1,0 it wouldn't be very random.
7  Game Development / Newbie & Debugging Questions / Re: Calculating probabilities - is this the right way? on: 2014-05-17 12:43:45
Here is a more flexible solution.
It has two advantages over many other proposals.
You can list the elements in any arbitrary order and you can easily add e.g. some slider controls to change the probabilities while the program is running, because you don't put the numbers directly into the list but instead you put a function in there that can reference e.g. a JSlider or some other source of numbers.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
Supplier[] probabilities = {
    () -> 50, () -> new A(),
    () -> 10, () -> new B(),
    () -> 35, () -> new C(),
    () ->  5, () -> new D()
};
RandomGenerator rg = new RandomGenerator(probabilities);
HashMap<Class, Integer> map = new HashMap<>();

for(int i = 0; i < 100000; i++) {
    Object o = rg.next();
    map.merge(o.getClass(), 1, (oldNum, x) -> oldNum + 1);
}

System.out.println(map);


Here is the rest of the code
http://pastebin.com/xP1EMLBR
8  Game Development / Game Mechanics / Re: [Math] Kinetic energy of impact between two moving objects on: 2014-05-11 12:55:57
My calculations are based on both, momentum and energy.
I calculate the energy lost in the collision.

before collision: 1/2 * 0^2  + 1/2 * 20^2 = 200 J
after collision:   1/2 * 10^2 + 1/2 * 10^2 = 100 J

So 100 J were lost on the collision. And that number is independant of the reference frame.
9  Game Development / Game Mechanics / Re: Moving the camera in 3D space on: 2014-05-11 12:39:11
I don't have the code for camera.rotate but you probably rotate relative to the game coordinate system instead of the camera coordinate system. You just need to reverse the order of operations i.e. apply the matrix/quaternion of the camera to the rotation matrix/quaternion, not the other way around.
10  Game Development / Game Mechanics / Re: [Math] Kinetic energy of impact between two moving objects on: 2014-05-11 08:53:50
I wrote some code that collides two objects 100 times in different random reference frames and calculates the kinetic energy lost in the collision. As expected it gets the same result each time so it really is independant of the reference frame.

http://pastebin.com/dzvxs6RC
11  Game Development / Game Mechanics / Re: [Math] Kinetic energy of impact between two moving objects on: 2014-05-09 06:24:22
Here is a more detailed explanation
http://en.wikipedia.org/wiki/Non-inertial_reference_frame

In short, just don't use an accelerated object as your reference.

The damage caused by a collision depends on the energy dissipated which is the kinetic energy befrore the collision minus the energy after the collision.
12  Game Development / Game Mechanics / Re: [Math] Kinetic energy of impact between two moving objects on: 2014-05-09 05:49:36
If you look at this from the point of view of one of the objects you are dealing with an accelerated frame of reference which behaves in a more complex way than a normal unaccelerated frame of reference. So I suggest you always use absolute velocities (i.e. velocities relativ to your game coordinate system).
Also after the collision the two objects might still be moving. You have to calculate the difference of the total energy before and after the collision.
13  Java Game APIs & Engines / OpenGL Development / Re: Design or pattern for handling background consumer/producer tasks on: 2014-05-08 21:19:09
Java 8 introduced the CompletableFuture class. With that you can do this kind of stuff very easily.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
static Image loadImage(String path) {
}
static Texture allocateTexture() {
}
static void uploadTexture(Image image, Texture texture) {
}
...
CompletableFuture<Image> image = CompletableFuture.supplyAsync(() -> loadImage(path));
CompletableFuture<Texture> texture = CompletableFuture.supplyAsync(() -> allocateTexture());
image.thenAcceptBothAsync(texture, (Image im, Texture tx) -> uploadTexture(im, tx));


Of course you could also have your loadImage method return a CompletableFuture<Image> instead of an Image to shorten the rest of the code.
And by the way - using CompletableFutures in this way will never block a thread. So it will work fine even if you have a million tasks.
14  Discussions / General Discussions / Re: [Web Games] What is the best second language? on: 2014-05-01 20:00:41
You could also use Scala. The Scala.JS project looks very promising. I played around with it and the js files it produces are about the same size as in Dart, the performance is similar too.
You can try it out interactively here
http://www.scala-js-fiddle.com/

The big advantage here is that you can run your Scala code either on the JVM or compile it to JS and run it in the browser.
15  Discussions / General Discussions / Re: [Web Games] What is the best second language? on: 2014-05-01 03:47:34
For someone who is used to Java, Dart is indeed the easiest way to get into web development. It's kind of a mix between Java and JavaScript. Nonetheless it still makes sense to learn JavaScript first since you need to be able to use a JS game engine from within Dart.
16  Discussions / Miscellaneous Topics / Re: Is our Universe Simulatable? on: 2014-04-21 04:08:58
Let's assume that we had a simulated universe running on a computer. A fully deterministic universe without any quantum randomness.
We have an initial state S and a function f that transforms the current state into a future state. f contains therefore the complete laws of physics of that universe.
That means any event that ever happens is fully determined by S and f alone and completely independant of the computer.
One question here is - what happens when intelligent beings evolve in that simulation? They would not be able to determine if they are simulated or not.
Again - anything that happens depends on S and f alone. So they couldn't possibly build an instrument that measures anything that isn't derived from S or f like for example the computer or the outside world.
The paradoxical thing about this is - Even if they somehow came to the believe that they are simulated, they wouldn't be able to tell whether the simulation is running or not, or whether it ever run at all. Because that information is not contained in either S or f.
17  Game Development / Game Mechanics / Re: Mathematical curves. on: 2014-04-20 22:47:54
I would use 1 - (1-x)^3
http://www.wolframalpha.com/input/?i=1+-+%281-x%29%5E3%2C+x+%3D+0+to+1

It's perfectly horizontal at 1,1
18  Discussions / General Discussions / Re: Get position of end of angle? on: 2014-04-19 04:01:09
I remember seeing a post by wessles some where where he got awnsers to how to find the end of his gun on his very first submission, but i cant find it. Any one else?
Do you mean this thread?
http://www.java-gaming.org/topics/calculating-certain-points-after-rotating-an-image/26744/msg/236014/view.html
19  Game Development / Game Mechanics / Re: Mathematical curves. on: 2014-04-13 00:13:58
If you want to play around with lots of different easing functions it makes things easier if you define yourself a function class that you can then scale, shift and combine with other functions.
Here is an example.
Func.java
http://pastebin.com/mFexrzMH
Graph.java
http://pastebin.com/2bPFcahz

btw. if you use Java 8 you can make your function definitions a lot shorter then in that example.

Of course this will make the calculation slower, but that shouldn't matter for easing animations.
20  Game Development / Newbie & Debugging Questions / Re: My most inefficient way of implementing running texts. How to optimize? on: 2014-04-06 01:12:50
Here is another example.
It uses double buffering.

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  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.Font;
import java.util.ArrayList;

public class Text extends JPanel {
    static final int CHAR_WIDTH = 7;
    static final int CHAR_HEIGHT = 14;
   
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        Text text = new Text();
        frame.add(text);
        text.setPreferredSize(new Dimension(800, 600));
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        text.runningText("This is a running text. Words that don't fit completely on a line will automatically be moved to the next line.", 30, 30, 15, 100);
    }
   
    private void runningText(String str, int x, int y, int maxWidth, int delay) {
        new Thread() {
            @Override
            public void run() {
                String[] lines = toLines(str, maxWidth);
                int maxLineLength = maxLength(lines);
               
                Graphics2D g = image.createGraphics();
               
                g.setFont(new Font("monospaced", Font.PLAIN, 12));
                g.setColor(Color.BLACK);
                g.drawRect(x-5, y-5, maxWidth*CHAR_WIDTH + 10, lines.length*CHAR_HEIGHT + 10);
               
                for(int j = 1; j < str.length(); j++) {
                    g.setColor(Color.WHITE);
                    g.fillRect(x, y, maxLineLength*CHAR_WIDTH, lines.length*CHAR_HEIGHT);
                    g.setColor(Color.BLACK);
                    int count = 0;
                    loop:
                    for(int i = 0; i < lines.length; i++) {
                        for(int k = 0; k < lines[i].length(); k++) {
                            g.drawString(lines[i].substring(k, k+1), x+k*CHAR_WIDTH, y+(i+1)*CHAR_HEIGHT);
                            count++;
                            if(count == j) break loop;
                        }
                    }
                    repaint();
                    try {
                        Thread.sleep(delay);
                    } catch(Exception e) {
                    }
                }
               
                g.dispose();
            }
        }.start();
    }
   
    BufferedImage image = null;
   
    public Text() {
        initImage();
    }
   
    public void initImage() {
        int w = getWidth();
        int h = getHeight();
        if(w <= 0 || h <= 0) {
            w = 800;
            h = 600;
        }
        image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, w, h);
        g.dispose();
    }
   
    @Override
    public void setPreferredSize(Dimension d) {
        super.setPreferredSize(d);
        initImage();
    }
   
    static String[] toLines(String str, int maxLength) {
        ArrayList<String> lines = new ArrayList<>();
        String[] words = str.split("\\s");
        String line = "";
        int length = 0;
        for(String word: words) {
            if(length+word.length()+1 > maxLength) {
                lines.add(line);
                line = "";
                length = 0;
            }
            if(length > 0) {
                line += " ";
                length += 1;
            }
            line += word;
            length += word.length();
        }
        if(line.length() > 0) lines.add(line);
        return lines.toArray(new String[lines.size()]);
    }
   
    static int maxLength(String[] lines) {
        int length = 0;
        for(String line: lines) length = Math.max(length, line.length());
        return length;
    }

    @Override
    public void paint(Graphics _g) {
        Graphics2D g = (Graphics2D)_g;
        g.scale(3,3);
        g.drawImage(image, 0, 0, null);
    }
}
21  Discussions / Miscellaneous Topics / Re: new computer purchase advice on: 2014-04-05 15:45:53
For the power supply the most important thing is not the wattage but the overall quality of the psu.
http://www.pcworld.com/article/2025425/how-to-pick-the-best-pc-power-supply.html
The one built into "system 2" is most probably low quality.
Don't be afraid to pay around 100$ for the psu.
22  Game Development / Game Mechanics / Re: Sidescroller jumping/collision (Not Java, but Javascript) on: 2014-04-03 00:45:52
There are some things you can do to make this kind of stuff easier to handle.
For once you should start to think in vectors.
http://www.mathsisfun.com/algebra/vectors.html

You can use vectors to represent a position, velocity, acceleration, etc.
It usually simplifies your code if you make mathematical entities - e.g. vectors - immutable.
That means you don't ever change such an object, instead you always create new ones.

Here is an example for a 2d vector.
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  
function Vec2D(x,y) {
    this.x = x;
    this.y = y;
};

Vec2D.prototype.dotProduct = function(v) {
    return this.x*v.x+this.y*v.y;
};
Vec2D.prototype.crossProduct = function(v) {
    return this.x*v.y-this.y*v.x;
};
Vec2D.prototype.getLength = function() {
    return Math.sqrt(this.x*this.x+this.y*this.y);
};
Vec2D.prototype.add = function(v) {
    return new Vec2D(this.x+v.x, this.y+v.y);
};
Vec2D.prototype.addX = function(dx) {
    return new Vec2D(this.x+dx, this.y);
};
Vec2D.prototype.addY = function(dy) {
    return new Vec2D(this.x, this.y+dy);
};
Vec2D.prototype.withX = function(x) {
    return new Vec2D(x, this.y);
};
Vec2D.prototype.withY = function(y) {
    return new Vec2D(this.x, y);
};
Vec2D.prototype.sub = function(v) {
    return new Vec2D(this.x-v.x, this.y-v.y);
};
Vec2D.prototype.mul = function(m) {
    return new Vec2D(this.x*m, this.y*m);
};
Vec2D.prototype.div = function(d) {
    return new Vec2D(this.x/d, this.y/d);
};
Vec2D.prototype.neg = function() {
    return new Vec2D(-this.x, -this.y);
};
Vec2D.prototype.toString = function() {
    return "("+this.x+", "+this.y+")";
};
Vec2D.prototype.equals = function(v) {
    return this.x === v.x && this.y === v.y;
};


Another useful strategy is to seperate your code into several small pieces and put every piece into it's own function.
Unfortunately due to JavaScripts bad design, that can be a little tricky.

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  
Player.prototype.update = (function() {
  var oldPos, _this;
 
  var move = function(v) {
    oldPos = _this.pos;
    _this.pos = _this.pos.add(v);
  };
 
  var moveBack = function() {
    _this.pos = oldPos;
  };
 
  var checkCollision = function(entities) {
    for(var i = 0; i < entities.length; i++) {
      if(!entities[i].passable && _this.intersects(entities[i])) return true;
    }
    return false;
  };
 
  var tryToMove = function(d, entities) {
    move(d);
    if(checkCollision(entities)) {
      moveBack();
      return true;
    }
    return false;
  };
 
  var update = function(entities) {
    _this = this;
    if(Keyboard.isPressed(Keyboard.D)) {
      this.velocity = this.velocity.withX(10);
    } else if(Keyboard.isPressed(Keyboard.A)) {
      this.velocity = this.velocity.withX(-10);
    } else {
      this.velocity = this.velocity.withX(0);
    }
    if(!this.isJumping && Keyboard.isPressed(Keyboard.SPACE)) {
      this.isJumping = true;
      this.velocity = this.velocity.addY(-this.jumpSpeed);
    }
    if(this.velocity.y < this.grav) {
      this.velocity = this.velocity.addY(this.grav);
    }
    var steps = Math.max(this.velocity.x, this.velocity.y);
    var distance = this.velocity.div(steps);
    var isColliding = false;
    for(var i = 0; i < move && !isColliding; i++) {
      isColliding = tryToMove(distance, entities);
    }
  }
  return update;
})();
23  Game Development / Game Mechanics / Re: Corners of rotated rectangle wrong on: 2014-03-02 00:40:27
There is no error in the code you posted.
If getAngleInRadians returns 0, then translatePoint will always return the same point that was put in.
So there must be somthing wrong elsewhere in your code.
24  Game Development / Newbie & Debugging Questions / Re: How to intersect a oval and a rect ? on: 2014-02-15 19:22:00
Here is a simply example. The ball bounces of the corners of the rectangle at the correct angle.
http://pastebin.com/ez8VWAj2
25  Game Development / Newbie & Debugging Questions / Re: Server/Client on: 2014-02-04 09:15:25
I don't see a call to repaint in your code.
There are a few things you could try.
Use DataInput/OutputStream and don't buffer the streams.
Call Socket.setTcpNoDelay(true); and Socket.setTrafficClass(0x10); before you connect the socket. i.e. between line 16 and 17 in your code.
26  Game Development / Game Mechanics / Re: Raytracing - Tutorials? on: 2014-02-01 19:22:53
There is an example of a very basic raytracer here
http://www.typescriptlang.org/Samples/#Raytracer
You could try and translate the code to Java.
27  Game Development / Game Play & Game Design / Re: Creating a circular "grid" ? on: 2013-11-11 19:34:47
You need to multiply by 2*pi to get the angle in rads.
28  Game Development / Performance Tuning / Re: Impossible race condition between threads? on: 2013-11-03 00:07:06
The problem is like nsigma wrote the complicated dependencies between the tasks.

Can't you reduce those dependencies?

Here is an example of what the code could look like.

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  
final ExecutorService threadPool = new ForkJoinPool();

ArrayList<Model> models = new ArrayList<>();
final ArrayList<Perspective> perspectives = new ArrayList<>();

ArrayList<Future<Model>> futures = new ArrayList<>();

for(Model _model: models) {
    final Model model = _model;
    threadPool.submit(new Callable<Model>() {
        public Model call() {
           
            model.update();
            boolean visible = false;
            for(Perspective p: perspectives) {
                if(model.isVisible(p)) {
                    visible = true;
                    break;
                }
            }
            if(visible) {
                model.computeBones();
                model.generateMatrices();
                return model;
            }
            return null;
           
        }
    });
}

ArrayList<Model> visibleModels = new ArrayList<>();
for(Future<Model> f: futures) {
    Model m = f.get();
    if(m != null) visibleModels.add(m);
}

To make sure values are written to memory and not just kept in registers, all methods of Model should be declared as synchronized.
29  Game Development / Performance Tuning / Re: Impossible race condition between threads? on: 2013-11-01 21:21:45
I may be misunderstanding something here, but why don't you use futures? Create a thread pool with e.g Executors.newFixedThreadPool() and then add some anonymous objects of type Callable to it. That gives you a list of Future objects and then you simply call get() on all of them to block until all threads are finished.
That way you don't need to synchronize anything and you don't need volatile either. As long as the data you pass into your Callables is immutable and your callables are side effect free, you are guaranteed to never run into any kinds of threading problems.
30  Game Development / Newbie & Debugging Questions / Re: Rotating a point around a center point algorithm on: 2013-10-26 12:44:24
Here is an example of a rotation matrix. It can be used to rotate a vector around any arbitary axis.
http://pastebin.com/Gq4mHL82
Pages: [1] 2 3 ... 5
 

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 (22 views)
2014-07-24 01:59:36

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

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

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

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

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

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

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

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

Riven (52 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!