I'm trying to create an infinite playable world with Jogl, Jbullet, and OpenSimplexNoise (OSN).
I'm generating the world with OSN, rendering it successfully, but I don't know how to add it to the world/collision system.
I found the btHeightfieldTerrainShape class, but it isn't implemented in Java.
I tried to use BvhTriangleMeshShape too, but i don't understand how it work.
I have 3 values of generation:
int smooth: the number of division in one meter
int viewDistance: the number of chunk to draw in one axis
int chunkSize: the number on
meter in one chunk
I'm using this code for generate heightmap:
/**
* Generate all height in the chunk p[cx][cy].
*/
private float[][] genPerlinMap(int[] p) {
float[][] pts = new float[chunkSize*smooth+1][chunkSize*smooth+1];
for(int i1=0;i1<chunkSize*smooth+1;i1++){
for(int i2=0;i2<chunkSize*smooth+1;i2++){
pts[i1][i2] = (float) (osp.eval(
(p[0]*chunkSize+i1/(float)(smooth))/(float) (mapSize),
(p[1]*chunkSize+i2/(float)(smooth))/(float) (mapSize)
)+1)*0.5f*mapSize;
}
}
return pts;
}
Does someone know how to add this ?
I found a solution: use https://github.com/bubblecloud/jbullet (jbullet from github) with the code here: https://github.com/bubblecloud/jbullet/commit/a5da8cdc679e998ef3a8605ee9b3cd5f94d71fee .
Thanks gouessej for the jbullet github link :)
Related
I am making a Tile based infinite world game (Similar to Notch's Minicraft) and i am trying to implement light. Here is the function i use:
The arguments it takes are the World itself, and the tiles position relative to world co-ordinates
public static Vector.Vector3f calculateLight(World w, int worldx, int worldy){
float tileLightValue = 0;
float totalLights = 0;
for(Light currentLight : BlockLighting.lights){ // for each light
float currentLightXWorldPosition = currentLight.x;
float currentLightYWorldPosition = currentLight.y;
float distanceBetweenPoints = Core.MathFunctions.getDistance(
worldx,worldy,
currentLightXWorldPosition, currentLightYWorldPosition
);
if(distanceBetweenPoints < currentLight.range){
tileLightValue += distanceBetweenPoints / 20.175f;
totalLights += 1;
}
}
float lig = Core.MathFunctions.fclamp(((tileLightValue * -2.4f) + (69.3f / 100)), 0 , 1);
if(totalLights > 0){
return new Vector.Vector3f(lig,lig,lig);
}else{
// If no lights, return 0 as to not change the ambient light.
return new Vector.Vector3f(0,0,0);
}
}
Calculations are then made by taking the light value of this function, and then adding it to the base lighting (ambient)
...
Core.MathFunctions.fclamp(daylight + light, 0, 1)
...
And produces the Intended result:
But When the lights intersect, i get this glitch :
So it has to be a mathematical issue. So im hoping someone could offer a solution to this issue or modify my code to send me in the correct direction, the best way to describe the function is:
Given a list of light points, and a position of a tile. Calculate the appropriate lighting for said tile in an infinite world, so that when it is added to the ambient lighting of the world it produces a Terraria like lighting effect.
Note the code showing the ambient light calculation (2nd code block) is more or less pseudo-code, the actual code uses the Vector3 for color calculation, and that works fine, just thought id strip it down to lessen confusion
Edit: Here is a JSFiddle using the exact same code, to illustrate the issue. So you can see it, and tinker with it!
Interactive JS Example
I am learning java game development with libgdx and have the following issue.
I have a Rectangle array which I iterate through and draw an image according to the position of the rectangle.
My Questions is how do I draw a random image every render but still keep drawing the same random image until it leaves the screen. currently it is drawing the same image but I would like know how to draw a different pipe image every iter.
Thank you
My Iterator
Iterator<Rectangle> upperIter = upperPipes.iterator();
while(upperIter.hasNext()) {
Rectangle upperpipe = upperIter.next();
upperpipe.x -= 8 * Gdx.graphics.getDeltaTime();
if(upperpipe.x < -32) upperIter.remove();
My draw method
public void drawPipes(){
batch.begin();
for(Rectangle upperPipe: Pipes.lowerPipes) {
batch.draw(Assets.pipeImg, upperPipe.x, upperPipe.y, upperPipe.width, upperPipe.height);
batch.end();
}
One great way to get repeatable random data is to use a Random object (java.util.Random is suitable for game usage), and provide it with a non-random seed. Each time you put in the seed and request the same sequence of number types+ranges, you will get the same (psuedo-)random numbers. When you want different numbers, just change the seed.
As an example:
Random rand = new Random(1234);
System.out.println(rand.nextInt()); // an int from Integer.MIN to Integer.MAX
System.out.println(rand.nextInt(100)); // an int from 0 to 100
Will always output the following, every single time.
-1517918040
33
But change the seed (in the constructor for Random), and the output values will change. rand.setSeed(seed) will reset the Random to start its sequence over.
You could use this to produce the same set of random numbers over and over while the rect is on screen.
However, a more direct and simple way would be to to just generate one random number for each rect when it is created, and store that number until it leaves:
public void drawPipes(){
for(int i = 0; i<Pipes.color.size; i++){
num = Pipes.color.get(i);
}
for(Rectangle bottomPipe: Pipes.lowerPipes) {
switch(num){
case 0:
batch.draw(Assets.redPipeImg, bottomPipe.x, bottomPipe.y, bottomPipe.width, bottomPipe.height);
break;
case 1:
batch.draw(Assets.yellowPipeImg, bottomPipe.x, bottomPipe.y, bottomPipe.width, bottomPipe.height);
break;
}
}
}
SOLVED!!
I created a custom pipe class and just created an array of pipes, each new pipe object that went into the array took a random image.
I iterated through the array, and called the draw method on each pipe object.
Simple and it works perfect
In Java using the acm.graphics GPen is there any way to move the entire drawn sequence of lines? I've read the manual thoroughly and I'm beginning to think it's not possible which brings me to my second question. Are there any other graphics objects in Java that work very similar to a pen that can also be moved. The reason I'm asking is because I've been working on a graphing program that allows mouse gestures to be used to pan around and zoom in and out. After building functionality for implicit functions I realized simply clearing the drawing board and redrawing everything is not going to cut it anymore so I really need to work on more efficient ways to handle intermediate changes of the graph without having to recalculate everything. For example with this or similar code:
GPen p = new GPen();
p.setLocation(100,100); //places the pen on the canvas at 100, 100
p.drawLine(-50,0); //draw a line left 50 pixels
p.drawLine(50,-50); //draw a line right and up 50 pixels each
p.drawLine(0,50); //draw a line down 50 pixels
This would result in a simple right triangle who's bottom right most point is at 100, 100 on a particular canvas. What I need to do is be able to move this same drawn sequence of lines relative to one another to another origin. What I hoping for is a class that has separate methods for setLocation() and move() where setLocation() controls pen position and move() would move the entire object around.
Ok so having received almost no attention on here I've came to the conclusion that such a method just needs to be written from scratch and went ahead and did that. I'm not entirely sure how helpful posting my proprietary code would be but in the event that anybody could use it I'll post the basic idea of it. Since Pen utilities are essentially a bunch of lines and lines are a bunch of from and to's I created an object that I called FPen (for FunctionPen) that accepts the instructions for from and to. While defining FPen you pass it where to start and how far to go however many times you need and that's it. Once you've passed these instructions I created another method called returnGPen(Color c) which will on call use the instructions it has on hand and generate the desired GPen object. When you want to move the entire GPen you can then create a method called adjustOrigin(double oX, double oY); which will calculate a change from a previously recorded origin and this new one and go through the list of instructions and adjust them appropriately.
My needs for this Class are strictly for my Graphing program and are not entirely finished either but it does work for most purposes.
import acm.graphics.GPen;
import java.awt.Color;
import java.util.ArrayList;
public class FPen{
private double relativeCenterX;
private double relativeCenterY;
private ArrayList<Double> fromX = new ArrayList<Double>();
private ArrayList<Double> fromY = new ArrayList<Double>();
private ArrayList<Double> distX = new ArrayList<Double>();
private ArrayList<Double> distY = new ArrayList<Double>();
public FPen(double rX, double rY, double z){
relativeCenterX = rX;
relativeCenterY = rY;
}
public void adjustOrigin(double cX, double cY){
double changeX = relativeCenterX-cX;
double changeY = relativeCenterY-cY;
for(int i = 0; i < fromX.size(); i++){
fromX.set(i,fromX.get(i)+changeX*zoom);
fromY.set(i,fromY.get(i)-changeY*zoom);
}
relativeCenterX = cX;
relativeCenterY = cY;
}
public void clear(){
fromX.clear();
fromY.clear();
distX.clear();
distY.clear();
}
public void drawLine(double fX, double fY, double tX, double tY){
fromX.add(fX);
fromY.add(fY);
distX.add(tX);
distY.add(tY);
}
public GPen returnGPen(Color c){
GPen pen = new GPen();
pen.setColor(c);
for(int i = 0; i < fromX.size(); i++){
pen.setLocation(fromX.get(i),fromY.get(i));
pen.drawLine(distX.get(i),distY.get(i));
}
return pen;
}
}
Of course a unexpected nice thing that came out of this was the idea that I can now quickly benchmark different drawing routines by creating different methods for each and calling what I'm interested in.
I'm working with sprite art, and I need to generate a polygon (array of vertices) for a collision detector.
I have a getPixel(x, y) method I can use to get the color of a pixel. I don't need any fancy color detection or anything, just solid pixels and transparent pixels. Here's what I started before my brain started to melt:
boolean[] hasColor = new boolean[size];
for (int i = 0; i < size; i++) {
int row;
row = i % width;
if ((pixmap.getPixel(i, row) != 0) || (pixmap.getPixel(row, i) != -256)) {
hasColor[i] = true;
} else {
hasColor[i] = false;
}
}
That should keep track of what pixels are empty, and what aren't. But I don't know where I should go from here.
Is there an algorithm or something I can use to help? Can someone provide input?
What you have is raster artwork.
What you need is a vector outline.
Converting from vector to raster is easy, raster to vector, not so much.
Here is one possible workflow:
Convert your artwork into a black-and-white image (like your pixel color present/not-present matrix).
Use Adobe Illustrator's "Live Trace" feature to vectorize this image.
Export the outline polygon into a format that you can read back easily.
Use this as your input for the collision detection.
Here's another approach:
A) Assume that the outline looks like a hexagon, like this:
*****
* *
* *
* *
* *
* *
*****
B) Define "fit" as the goodness of an outline, calculated by checking what percentage of the pixels are inside the hexagon (does not have to be the regular-shaped figure shown above).
C) Change the positions of the vertices, until you find an optimum fit (or until you get tired).
Step (C) is, of course, the hardest one. Plus, if your sprite needs more vertices, you may need to start out with an octagon/n-gon instead.
So I have some path generator which now works like this
http://www.openprocessing.org/visuals/?visualID=2615 (There is source; WQRNING - JAVA APPLET)
I want to create some 3D object using paths I generated so it locked in one of perspectives similar to what I get now in 2D.
So how do I dynamically construct 3D object by adding paths?
BTW: actually I ment algorithm like this http://www.derschmale.com/2009/07/20/slice-based-volume-rendering-using-pixel-bender/
So I want to create from such PATH (I do not want to use images and I do not want to use flash I want to use Java + OpenGl)
such 3d image (But note I want openGL Java and Path's))
I'm not sure I understand what you're after.
The example you supplied draws 2d paths, but merely uses z. scaling would have worked
in a similar way.
So How to dinamicly construct 3d
object by adding path's ?
Do you mean extruding/lathing an object, or replicating the scrunch sketch ?
Drawing a path is easy in processing, you just place vertex objects, in a for loop
between beginShape() and endShape() calls.
Here is the bit of code that does that in the example you've sent:
beginShape();
for (int p=0; p<pcount; p++){
vertex(Ring[p].position().x(),Ring[p].position().y());
}
endShape(CLOSE);
you can also call vertex(x,y,z)
I wanted to extrude a path a while back, here is my question in case it helps.
Basic sketch is uploaded here.
EDIT:
If you have an array of 2 polygons, you can just loop through them, and draw
using something similar to beginShape() and endShape(), GL_POLYGON might be handy.
e.g.
import processing.opengl.*;
import javax.media.opengl.*;
int zSpacing = 10;
PVector[][] slices;
void setup() {
size(600, 500, OPENGL);
slices = new PVector[3][3];
//dummy slice 1
slices[0][0] = new PVector(400, 200,-200);
slices[0][1] = new PVector(300, 400,-200);
slices[0][2] = new PVector(500, 400,-200);
//dummy slice 2
slices[1][0] = new PVector(410, 210,-200);
slices[1][1] = new PVector(310, 410,-200);
slices[1][2] = new PVector(510, 410,-200);
//dummy slice 3
slices[2][0] = new PVector(420, 220,-200);
slices[2][1] = new PVector(320, 420,-200);
slices[2][2] = new PVector(520, 420,-200);
}
void draw() {
background(255);
PGraphicsOpenGL pgl = (PGraphicsOpenGL) g; // g may change
GL gl = pgl.beginGL(); // always use the GL object returned by beginGL
for(int i = 0 ; i < slices.length; i ++){
gl.glColor3f(0, .15 * i, 0);
gl.glBegin(GL.GL_POLYGON);
for(int j = 0; j < slices[i].length; j++){
gl.glVertex3f(slices[i][j].x, slices[i][j].y,slices[i][j].z + (zSpacing * i));
}
gl.glEnd();
}
pgl.endGL();
}
The idea is you loop through each slice, and for each slice your loop through all its points. Obviously slices and the number of 3d vectors inside each slice is up to your data. Speaking of which, where does your data come from ?
If slices is not what your after volTron could come in handy:
volTron http://dm.ncl.ac.uk/joescully/voltronlib/images/s2.jpg
HTH,
George