How to draw a line between two points in LibGDX, in 3D - java

I'm trying to learn the 3D side of LibGDX, and I've came to a problem. I want to draw a line from 0, 0, -5 to 0, 0, 5. I've tried a few things to make this work.
First I looked to see if I could create a line as a Model. As far as I can see, I can't do this.
What I then realised is that theoretically I can draw a line using a ShapeRenderer. Here's my code to try to do this.
public class Main implements ApplicationListener {
...
public ShapeRenderer srend;
...
#Override
public void create() {
...
srend = new ShapeRenderer();
srend.setColor(Color.RED);
...
}
#Override
public void render() {
...
srend.begin(ShapeType.Line);
srend.line(0, 0, -5, 0, 0, 5);
srend.end();
...
}
...
}
But for some reason this doesn't appear to work. I use ShapeRenderers a lot, but it's possible that I'm making a mistake initializing or using it, I don't think that's the problem though.
I've only just started using the 3D part of LibGDX, so I assume the problem is around where I'm drawing the actual line.

Model Builder works well for me.
ModelBuilder modelBuilder = new ModelBuilder();
modelBuilder.begin();
MeshPartBuilder builder = modelBuilder.part("line", 1, 3, new Material());
builder.setColor(Color.RED);
builder.line(0.0f, 0.0f, -5.0f, 0.0f, 0.0f, 5.0f);
lineModel = modelBuilder.end();
lineInstance = new ModelInstance(lineModel);

You need to set camera matrix before rendering:
#Override
public void create() {
...
srend = new ShapeRenderer();
srend.setProjectionMatrix(new Matrix4())
srend.setColor(Color.RED);
...
}
I don't know why, but this helps. Matrix4() is just identity matrix. With identity camera matrix right side of the screen has coordinate 1, left -1. So if coordinates inside cube -1..1, you should see them.
In real code (for example, for drawing gizmos in 3d world) you can set matrix from PerspectiveCamera:
shapeRenderer.setProjectionMatrix(camera.combined)

Related

Text not displaying in LibGdx Java

I am trying to add text for debugging purposes in my program. I call the players debugDraw method like so in the main class:
public void create () {
//Initialize essentials
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.setToOrtho(false);
rend = new ShapeRenderer();
rend.setAutoShapeType(true);
batch = new SpriteBatch();
// Initialize Entities
player = new Player(new Vector2(100, 100), new Vector2(100,100));
enemy = new Enemy(new Vector2(100, 100), new Vector2(100,10));
}
#Override
public void render () {
//Update player
player.update();
//Update camera then set matrix of batch
cam.update();
batch.setTransformMatrix(cam.combined);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// ShapeRenderer begin
rend.begin();
player.render(rend);
enemy.render(rend);
rend.end();
// ShapeRenderer end
// SpriteBatch begin
batch.begin();
player.renderDebugText(batch);
batch.end();
// SpriteBatch end
}
Here is the entity class which is the parent class to player:
public Entity(Vector2 coords, Vector2 dims){
//Assign constructors
position = coords;
dim = dims;
//For debugging purposes of all entities
debugText = new BitmapFont();
debugText.setColor(Color.WHITE);
}
public void renderDebugText(SpriteBatch batch){
debugText.draw(batch, "Vel.x: " + vel.x, 100, 100);
}
However when I run the program, I just get my normal screen with no text at all. I can't seem to figure out why this isn't working. Any help is extremely appreciated.
Nothing immediately looks wrong with the code you posted, so here's a few ideas;
The default BitmapFont is 15pt, if this is drawn at 15px height then it could be very small if you force your game to a high resolution, like Full-HD. My current project is Full-HD and the font I use looks just about right at 45px, so you could try scaling yours by a factor of 3 or 4. E.g. use this after initialising your font;
bitmapFont.getData().setScale(3);
Your Camera should also have the virtual/viewport dimensions, so if you are forcing a particular resolution then you should pass in your virtual dimensions instead.
As #Tenfour04 has suggested, you should try to avoid multiple instances of the same font, so instead of initialising your font in the Entity class, initialise it in your main game and pass it through the Entity constructor. I can't see how this would fix your issue though as this would be purely for performance.
I made a very simple mistake, but from how much code I posted, it is easily missed. On the line where I put
batch.setTransformMatrix(cam.combined);
it should be replaced with
batch.setProjectionMatrix(cam.combined);
Then all errors go away, sorry, I don't know why it took me so long to figure out. Thanks for all the help!

How do I animate tiled sprite after contact? AndEngine

I am trying to animate one of my TiledSprites after the player makes contact with the sprite. Here is my code so far:
levelObject = new AnimatedSprite(x, y, resourceManager.wooden_crate_region, vbom)
{
#Override
protected void onManagedUpdate(float pSecondsElapsed)
{
super.onManagedUpdate(pSecondsElapsed);
if (player.collidesWith(this))
{
addToScore(1);
final long[] CRATE_ANIMATE = new long[] { 1000, 1000, 1000, 10000};
this.animate(CRATE_ANIMATE, 0, 3, true);
this.setIgnoreUpdate(true);
}
}
};
But when I try to play the game, the player makes contact and goes through the sprite. Not sure whats going on. It does add the score, but no animation.
Any help would be appreciated.
remove the line
this.setIgnoreUpdate(true);
this line ignores any update on your object, means it won't animate it either.

How to make the camera follow 2 objects?

I'm making my first 3D game and it's a sort of streetfighter/tekken game. I've seen how the some of the camera modes works ie. chasecam and camnode. What I dont know is how to make the camera follow both players at the same time. I want the camera to zoom in when the players are near each others, and zoom out when they are not.
I would appreciate any help with ideas or possible solutions.
Thanks
You can achieve this fairly easily be manually setting the camera position provided you know two pieces of information, where you want the camera to be, and where you want it to look
Where the camera should look
This is the easiest thing to determine, the camera should be looking at the midway position of the two objects. Once you know that you can set where the camera is looking using
Vector3f boxsCentre=box1.getWorldTranslation().add(box2.getWorldTranslation()).mult(0.5f);
cam.lookAt(boxsCentre, Vector3f.UNIT_Y);
Where the camera should be
Where the camera should be is more tricky. You know it should be somewhere on the line that extends from the centre of the two objects in a direction perpendicular to the line between those two objects. Thankfully the cross product gives us this. We want the camera to always be on the same level as the objects so by crossing the seperation vector by a vector going straight up we get that perpendicular line
Vector3f seperationVector=box2.getWorldTranslation().subtract(box1.getWorldTranslation());
Vector3f perpendicularFromTheAction= seperationVector.cross(Vector3f.UNIT_Y);
perpendicularFromTheAction.normalizeLocal();
So, that gives us the line, but where on the line should we put the camera. I just played around with this and found that twice the distance between the objects gives a nice look, so
float distance=2*seperationVector.length();
Vector3f newCameraLocation=boxsCentre.add(perpendicularFromTheAction.mult(distance));
Then you can set the cameras position
cam.setLocation(newCameraLocation);
Putting it all together
I've used this code together with two boxes that are moving on a loop to demostrate this, as you can see you get the effect you want
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.*;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
public class FightTest extends SimpleApplication {
Geometry box1;
Geometry box2;
public static void main(String[] args) {
FightTest app = new FightTest();
app.start();
}
#Override
public void simpleInitApp() {
//put in some reference boxes
for(int i=-20;i<=20;i+=20){
for(int j=-20;j<=20;j+=20){
if (j!=0||i!=0){
Geometry referenceBox = createBox(ColorRGBA.Red);
referenceBox.setLocalTranslation(i, 0, j);
rootNode.attachChild(referenceBox);
}
}
}
//put in our two players
box1 = createBox(ColorRGBA.Blue);
box1.setLocalTranslation(5, 0, 0);
box2 = createBox(ColorRGBA.Green);
rootNode.attachChild(box1);
rootNode.attachChild(box2);
}
private Geometry createBox(ColorRGBA color){
Box b = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry box = new Geometry("Box", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", color);
box.setMaterial(mat);
return box;
}
#Override
public void simpleUpdate(float tpf) {
adjustCam();
movePlayers(tpf);
}
#Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
private void adjustCam(){
//we want our camera to look at the centre of the boxes
Vector3f boxsCentre=box1.getWorldTranslation().add(box2.getWorldTranslation()).mult(0.5f);
cam.lookAt(boxsCentre, Vector3f.UNIT_Y);
//we also want our camera to move closer/further away as the boxes seperate.
//and move around so its always parallel to the action
//parallel to the action means on the line given by the cross product of the
//box seperation and the upwards vector
Vector3f seperationVector=box2.getWorldTranslation().subtract(box1.getWorldTranslation());
Vector3f perpendicularFromTheAction= seperationVector.cross(Vector3f.UNIT_Y);
perpendicularFromTheAction.normalizeLocal();
//we could (and you should) get complicated on exactly how far the camera should
//move backwards, but I'm just going to make the camera twice as far away as the
//objects are seperated
float distance=2*seperationVector.length();
Vector3f newCameraLocation=boxsCentre.add(perpendicularFromTheAction.mult(distance));
cam.setLocation(newCameraLocation);
}
float timeAccumulator=0;
private void movePlayers(float tpf){
//basic movement, just for demo
timeAccumulator+=tpf;
if (timeAccumulator<2){
box1.move(new Vector3f(5f*tpf,0,0));
box2.move(new Vector3f(0,0,5f*tpf));
}else if (timeAccumulator<4){
box1.move(new Vector3f(-5f*tpf,0,0));
box2.move(new Vector3f(0,0,-5f*tpf));
}else{
timeAccumulator=0;
}
}
}

Model texturing error with libgdx

I am having an issue with libgdx where when I load a model in any format the texture is not appearing correctly. I'm not sure what is causing this issue.
Example: In 3ds max or blender it looks like this
but when I put it into a libgdx project it looks like this
My code is based on http://blog.xoppa.com/loading-models-using-libgdx/
And I have tried it with and without the lights with no luck
Code:
//Loading the model
assetManager = new AssetManager();
assetManager.load("data/models/M4A1 SOPMOD/M4A1 SOPMOD.g3db", Model.class);
//..................................................
//Calling to an object and drawing it
public void create()
modelBatch = new ModelBatch();
box = Assets.assetManager.get("data/models/M4A1 SOPMOD/M4A1 SOPMOD.g3db", Model.class);
//box = Assets.getModelByName("M4");
//box = modelBuilder.createBox(1f, 1f, 1f, new Material(ColorAttribute.createDiffuse(Color.WHITE), ColorAttribute.createSpecular(Color.WHITE), FloatAttribute.createShininess(64f)), Usage.Position | Usage.Normal);
boxInstance = new ModelInstance(box);
lights = new Lights();
lights.ambientLight.set(1f, 1f, 1f, 1f);
lights.add(new DirectionalLight().set(1f, 1f, 1f, -1f, -0.8f, -0.2f));
}
public void Update(float deltaTime)
{
dotd.camera.rotateAround(Vector3.Zero, new Vector3(0, 1, 0), deltaTime* 50.0f);
dotd.camera.update();
}
public void Render()
{
modelBatch.begin(dotd.camera);
modelBatch.render(boxInstance, lights);
modelBatch.end();
}
My first guess would be that the texture coordinates need to be flipped, try adding (or removing) the -f command line option of fbx-conv (.e.g fbx-conv -f model.fbx).
You also might want to check your normals. Add the following line to your create method: DefaultShader.defaultCullFace = 0; and don't apply lights. If this renders your model better, you probably need to recalculate the normals in 3ds max or blender. Don't forget to remove the defaultCullFace line afterwards.
Finally, obviously, make sure the correct texture is loaded and don't use high poly models.

JOGL objects "Disappearing" when moving around a scene

I'm trying to implement a basic physics engine in Java and I'm using the JOGL bindings so I can visualize the results. I can create and rotate shapes easily enough, but have run into problems whilst manipulating the viewport and whilst moving the shapes.
I don't think a clipping issue - I've tried using the gluPerspective method with a massive range (0.0001f - 10000f) with no success. When I move the camera further away from my objects or move the objects themselves, they disappear.
Tutorials about JOGL are few and far between and many also use different versions of OpenGL, so I turn to the only friend I have left: the wonderful users of stack overflow. :)
Flattery aside, the code follows:
public class JoglEventListener implements GLEventListener, KeyListener, MouseListener, MouseMotionListener {
// keep pointer to associated canvas so we can refresh the screen (equivalent to glutPostRedisplay())
public GLCanvas canvas;
public Particle triforce;
public float x;
// constructor
public JoglEventListener(GLCanvas canvas) {
this.canvas = canvas;
}
#Override
public void display(GLAutoDrawable drawable) {
update();
render(drawable);
}
#Override
public void init(GLAutoDrawable drawable) {
triforce = new Particle();
x = 0;
}
private void update() {
triforce.integrate(0.0001);
x = x + 0.25f;
}
private void render(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
GLU glu = new GLU();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
//gl.glFrustum (.5f, -.5f, -.5f * 1080, .5f * 960, 1.f, 500.f);
glu.gluPerspective(0, 1, 0.1f, 100f);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glHint(GL2.GL_CLIP_VOLUME_CLIPPING_HINT_EXT,GL2.GL_FASTEST);
glu.gluLookAt(0, 0, 1.5, 0, 0, -10, 0, 1, 0);
//gl.glRotatef(90, 0f , 1f , 0f );
//Draw some scale lines
gl.glBegin(GL.GL_LINES);
gl.glColor3f(0.75f, 0.75f, 0.75f);
for (int i = 0; i < 20; i += 1)
{
gl.glVertex3f(-5.0f, 0.0f, i + 0.5f);
gl.glVertex3f(5.0f, 0.0f, i + 0.5f);
}
gl.glEnd();
//gl.glRotatef(x, 1f , 1f , 1f );
gl.glPushMatrix();
gl.glTranslated(triforce.position.x, triforce.position.y, triforce.position.z);
gl.glBegin(GL.GL_TRIANGLE_STRIP);
gl.glColor3f(1f, 0f, 0f);
gl.glVertex3d(0, 0, -2);
gl.glColor3f(0f, 1f, 0f);
gl.glVertex3d(0, 0.25d, -2);
gl.glColor3f(0f, 0f, 1f);
gl.glVertex3d(0.25d, 0, -2);
gl.glColor3f(1f, 1f, 0f);
gl.glVertex3d(0.25d, 0.25d, -2.25d);
gl.glEnd();
gl.glPopMatrix();
gl.glFlush();
}
// (empty overridden methods omitted)
public Particle () {
setMass(200d);
velocity = new Vector3(0d, 30d, 40d);
acceleration = new Vector3(0d, -20d, 0d);
position = new Vector3(0d, 0d, 0d);
damping = 0.99d;
}
public void integrate (double duration) {
if (inverseMass <= 0.0d) {
return;
}
assert (duration > 0.0);
position.addScaledVector(velocity, duration);
Vector3 resultingAcc = new Vector3(acceleration.x, acceleration.y, acceleration.z);
velocity.addScaledVector(resultingAcc, duration);
velocity.multEquals(Math.pow(damping, duration));
//clearAccumulator();
}
public void setMass(double mass)
{
assert(mass != 0);
inverseMass = (1.0d)/mass;
}
Before movement / starting position:
The shape drifts upward and is obscured from the right and top, becoming invisible:
Any help would be greatly appreciated! Thanks!
The massive view range can be a problem. The coordinates of the objects are only so precise, and with a huge view range, things that should be near each other are determined to be at the same point. This can cause an object that should be in front of another to disappear behind it. Try using a smaller view range.
I had the same problem. Objects disappearing, while some stay in the scene. After removing:
gl.glEnable(GL2.GL_CULL_FACE);
everything was working just fine ! Of course this is JOGL code, in C, the command would be without all those objects. Just to make this answer clear for everyone.
In the render function, change the value of last parameter of gluPerspective from 100f to 1000f. It will solve your problem.
gl.gluPerspective(0, 1, 0.1f, 100f);
to
gl.gluPerspective(0, 1, 0.1f, 500f);
And I think in your code you have done a mistake in the above line writing glu.gluperspective
I think it is gl.gluPerspective.
In the end, I was never able to track down the issue, and started again from scratch. I didn't run into any further clipping issues on my new build.
My best guess as to my initial failure is an improperly used glHint or glClear call, or perhaps some problem with the version of JOGL I was referencing.

Categories

Resources