Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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: Camera movement problem ! on: 2014-08-29 15:08:45
My problem is solved !
I just forgot to change the boolean in the glUniformMatrix4f method when updating the uniforms !

2  Game Development / Game Mechanics / [SOLVED] Camera movement problem ! on: 2014-08-25 21:34:36
Hello,

I'm currently working on a 2D game, for which I don't use any library more than LWJGL.
I'm working with OpenGL 3.3.0, so I've got to make my own matrices to render things.

I use a Orthogonal Projection, but I've got some troubles when moving the camera, from left to right, and up to down (and vice-versa).
In fact, more than 'displacing' objects, it does that :


So here's my Camera.java file :

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  
package com.poulpicious.engine.graphics;
 
import com.poulpicious.engine.maths.Matrix4f;
import com.poulpicious.engine.maths.Vector2f;
import com.poulpicious.engine.maths.Vector3f;
 
public class Camera {
 
    private float viewportWidth, viewportHeight;
 
    private Shader shader;
 
    private Vector3f position;
    private Vector3f rotation;
 
    private Matrix4f projection;
    private Matrix4f rotationMatrix;
    private Matrix4f translationMatrix;
    private Matrix4f combined;
 
    public Camera(float viewportWidth, float viewportHeight) {
        projection = new Matrix4f().initOrthographic(-viewportWidth / 2, viewportWidth / 2, -viewportHeight / 2, viewportHeight / 2, 0, 1000.0f);
        rotationMatrix = new Matrix4f().initIdentity();
        translationMatrix = new Matrix4f().initIdentity();
 
        position = new Vector3f(0, 0, 0);
 
        rotation = new Vector3f(0, 0, 0);
 
        updateViewport(viewportWidth, viewportHeight);
    }
 
    private Vector3f tmpVec = new Vector3f(0, 0, 0);
 
    public void update() {
        projection.initOrthographic(-viewportWidth / 2, viewportWidth / 2, -viewportHeight / 2, viewportHeight / 2, 0, 1000.0f);
        rotationMatrix.initRotation(rotation.x, rotation.y, rotation.z);
        translationMatrix.initTranslation(-position.x, -position.y, -position.z);
        combined = projection.mul(rotationMatrix.mul(translationMatrix));
 
        shader.updateUniform("combinedMatrix", combined);
    }
 
    public void updateViewport(float viewportWidth, float viewportHeight) {
        this.viewportWidth = viewportWidth;
        this.viewportHeight = viewportHeight;
    }
 
    public void startDrawing() {
        shader.bind();
    }
 
    public void endDrawing() {
        shader.unbind();
    }
 
    public void bindShader(Shader shader) {
        this.shader = shader;
    }
 
    public Matrix4f getCombinedMatrix() {
        return combined;
    }
 
    public void translate(float x, float y) {
        this.position.add(x, y, 0);
    }
 
    public void moveTo(float x, float y) {
        this.position.set(x, y, 0);
    }
 
    public void rotate(float z) {
        this.rotation.add(0, 0, z);
    }
 
    public Vector3f getPosition() {
        return position;
    }
 
    public void setPosition(Vector3f position) {
        this.position = position;
    }
 
    public Vector3f getRotation() {
        return rotation;
    }
 
    public void setRotation(Vector3f rotation) {
        this.rotation = rotation;
    }
 
}


And here my Matrix4f.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  
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  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
package com.poulpicious.engine.maths;
 
public class Matrix4f {
 
    private float[][] m;
 
    public Matrix4f() {
        m = new float[4][4];
 
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                m[i][j] = 0;
            }
        }
    }
 
    public Matrix4f initIdentity() {
        m[0][0] = 1;
        m[1][1] = 1;
        m[2][2] = 1;
        m[3][3] = 1;
 
        return this;
    }
 
    public Matrix4f initTranslation(float x, float y, float z) {
        initIdentity();
 
        m[0][3] = x;
        m[1][3] = y;
        m[2][3] = z;
 
        return this;
    }
 
    public Matrix4f initRotation(float x, float y, float z) {
 
        Matrix4f rx = new Matrix4f();
        Matrix4f ry = new Matrix4f();
        Matrix4f rz = new Matrix4f();
 
        x = (float) Math.toRadians(x);
        y = (float) Math.toRadians(y);
        z = (float) Math.toRadians(z);
 
        rz.m[0][0] = (float) Math.cos(z);
        rz.m[0][1] = (float) -Math.sin(z);
        rz.m[1][0] = (float) Math.sin(z);
        rz.m[1][1] = (float) Math.cos(z);
        rz.m[2][2] = 1;
        rz.m[3][3] = 1;
 
        rx.m[0][0] = 1;
        rx.m[1][1] = (float) Math.cos(x);
        rx.m[1][2] = (float) -Math.sin(x);
        rx.m[2][1] = (float) Math.sin(x);
        rx.m[2][2] = (float) Math.cos(x);
        rx.m[3][3] = 1;
 
        ry.m[0][0] = (float) Math.cos(y);
        ry.m[0][2] = (float) -Math.sin(y);
        ry.m[1][1] = 1;
        ry.m[2][0] = (float) Math.sin(y);
        ry.m[2][2] = (float) Math.cos(y);
        ry.m[3][3] = 1;
 
        m = rz.mul(ry.mul(rx)).getM();
 
        return this;
    }
 
    public Matrix4f initScale(float x, float y, float z) {
        m[0][0] = x;
        m[1][1] = y;
        m[2][2] = z;
        m[3][3] = 1;
 
        return this;
    }
 
    public Matrix4f initOrthographic(float left, float right, float bottom, float top, float near, float far) {
 
        this.initIdentity();
 
        float width = right - left;
        float height = top - bottom;
        float depth = far - near;
 
        m[0][0] = 2.0f / width;
        m[0][3] = -(right + left) / width;
 
        m[1][1] = 2.0f / height;
        m[1][3] = -(top + bottom) / height;
 
        m[2][2] = -2.0f / depth;
        m[2][3] = -(far + near) / depth;
 
        m[3][3] = 1;
 
        return this;
    }
    public Matrix4f initRotation(Vector3f forward, Vector3f up) {
        Vector3f f = forward.set(forward.x, 0, forward.z).normalized();
 
        Vector3f r = up.normalized();
 
        r = r.cross(f);
 
        Vector3f u = f.cross(r);
 
        return initRotation(f, u, r);
    }
 
    public Matrix4f initRotation(Vector3f forward, Vector3f up, Vector3f right) {
        Vector3f f = forward;
        Vector3f r = right;
        Vector3f u = up;
 
        m[0][0] = r.x;
        m[0][1] = r.y;
        m[0][2] = r.z;
 
        m[1][0] = u.x;
        m[1][1] = u.y;
        m[1][2] = u.z;
 
        m[2][0] = f.x;
        m[2][1] = f.y;
        m[2][2] = f.z;
 
        m[3][3] = 1;
 
        return this;
    }
 
    public Vector3f transform(Vector3f r) {
        return new Vector3f(m[0][0] * r.x + m[0][1] * r.y + m[0][2] * r.z + m[0][3], m[1][0] * r.x + m[1][1] * r.y + m[1][2] * r.z + m[1][3], m[2][0] * r.x + m[2][1] * r.y + m[2][2] * r.z + m[2][3]);
    }
 
    public Matrix4f mul(Matrix4f r) {
        Matrix4f res = new Matrix4f();
 
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                res.set(i, j, m[i][0] * r.get(0, j) + m[i][1] * r.get(1, j) + m[i][2] * r.get(2, j) + m[i][3] * r.get(3, j));
            }
        }
 
        return res;
    }
 
    public float[][] getM() {
        float[][] res = new float[4][4];
 
        for (int i = 0; i < 4; i++)
            for (int j = 0; j < 4; j++)
                res[i][j] = m[i][j];
        return res;
    }
 
    public float get(int x, int y) {
        return m[x][y];
    }
 
    public void setM(float[][] m) {
        this.m = m;
    }
 
    public void set(int x, int y, float v) {
        m[x][y] = v;
    }
 
    public String toString() {
        String result = "";
 
        for (int i = 0; i < 4; i++)
            for (int j = 0; j < 4; j++)
                result += m[i][j] + ", ";
 
        return result;
 
    }
}

(I assume it's TheBennyBox's one !)

And the Vertex Shader :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
#version 150 core
 
uniform mat4 combinedMatrix;
uniform vec2 cameraPosition;
 
in vec2 in_Position;
in vec2 in_TexCoord;
 
out vec2 texCoord;
 
void main(void) {
    gl_Position = combinedMatrix * vec4(in_Position, 0.0, 1.0);
    texCoord = in_TexCoord;
}


I don't know how to figure this...

Thanks in advance for any response,
yann68
3  Game Development / Newbie & Debugging Questions / Re: Moving problem (Side-scroller) on: 2012-05-08 13:39:19
Sorry, I didn't say it: my game is more like Super Smash Bros Brawl, so you can go to left or right, and the enemies want to hit you...
4  Game Development / Newbie & Debugging Questions / Moving problem (Side-scroller) on: 2012-05-08 13:14:46
Hello !

I currently have a problem with my Side-scroller. (I'm using SLICK2D)
My player moves correctly(the background in fact), and the collisions are working correctly too.
But I don't really know how to move correctly my monster entity, because they don't move with the background...

Here are my codes:

Player.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  
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  
package com.poulpicious.gtw.entities;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.StateBasedGame;

public class Player extends LivingEntity {

   public Player(float x, float y, float width, float height) {
      super(x, y, width, height);
      screenXPos = 400;
   }

   @Override
   public void render(GameContainer gc, StateBasedGame sbg, Graphics g) {
      g.setColor(Color.white);
      g.draw(new Rectangle(screenXPos - width / 2 + 1, yPos, width, height));
   }

   @Override
   public void update(GameContainer gc, StateBasedGame sbg, int delta, Input input) {
      if (input.isKeyDown(Input.KEY_Q)) {
         xDir = -2;
      }
      if (input.isKeyDown(Input.KEY_D)) {
         xDir = 2;
      }
      if (input.isKeyDown(Input.KEY_LSHIFT)) {
         xDir *= 1.4;
      }
      if (input.isKeyDown(Input.KEY_SPACE) && hasJump) {
         yDir = (float) -(4);
         hasJump = false;
         startJumpY = yPos;
      }

      startX = xPos;
      startY = yPos;
      startXDir = xDir;
      startYDir = yDir;

      collidingTop = false;

      moveBounds();
      gravity();
      move();
      handleCollisions();
      xDir = 0;
   }

   public void moveBounds() {
      bounds.setLocation(xPos + xDir, yPos + yDir);
   }

   public void move() {
      xPos += xDir;
      yPos += yDir;
   }

   public void gravity() {
      yDir += 0.06;
   }

   private void handleCollisions() {
      for (Rectangle rec : blocks) {

         Rectangle rect = new Rectangle(rec.getX() - screenXPos + width / 2 - 1, rec.getY(), rec.getWidth(), rec.getHeight());

         float[] points = bounds.getPoints();

         if (rect.contains(points[4] - 2, points[5] + 2) || rect.contains(points[6] + 2, points[7] + 2)) {
            yPos = startY;
            yDir = 0;
            hasJump = true;
            collidingTop = true;
         }

         if (rect.intersects(getBounds()) && collidingTop == false) {
            xPos = startX;
            xDir = startXDir;
         }
      }
   }

   public float getRealXPos() {
      return screenXPos + width / 2 - 1;
   }
}


MobEntity.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  
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  
package com.poulpicious.gtw.entities;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.StateBasedGame;

public class MobEntity extends LivingEntity {

   private Player thePlayer;

   public MobEntity(Player player, float x, float y, float width, float height) {
      super(x, y, width, height);
      thePlayer = player;
      isAlive = true;
      screenXPos = x;
   }

   @Override
   public void render(GameContainer gc, StateBasedGame sbg, Graphics g) {
      g.draw(new Rectangle(xPos - screenXPos, yPos, width, height));
   }

   @Override
   public void update(GameContainer gc, StateBasedGame sbg, int delta, Input input) {
      if (thePlayer.getRealXPos() < xPos - screenXPos) {
         xDir = -2;
      }
      if (thePlayer.getRealXPos() > xPos - screenXPos) {
         xDir = 2;
      }
      startX = xPos;
      startY = yPos;
      startXDir = xDir;
      startYDir = yDir;

      collidingTop = false;

      moveBounds();
      gravity();
      move();
      handleCollisions();
      xDir = 0;
   }

   public void moveBounds() {
      bounds.setLocation(xPos + xDir, yPos + yDir);
      System.out.println(bounds.getX());
   }

   public void move() {
      xPos += xDir;
      yPos += yDir;
   }

   public void gravity() {
      yDir += 0.06;
   }

   private void handleCollisions() {
      for (Rectangle rec : blocks) {

         Rectangle rect = new Rectangle(rec.getX() - screenXPos, rec.getY(), rec.getWidth(), rec.getHeight());
         float[] points = bounds.getPoints();

         if (rect.contains(points[4] - 2, points[5] + 2) || rect.contains(points[6] + 2, points[7] + 2)) {
            yPos = startY;
            yDir = 0;
            hasJump = true;
            collidingTop = true;
         }

         if (rect.intersects(getBounds()) && collidingTop == false) {
            xPos = startX;
            xDir = startXDir;
         }
      }
   }

   public boolean isAlive() {
      return isAlive;
   }

}
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.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (55 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (89 views)
2014-12-09 22:41:13

BurntPizza (112 views)
2014-12-08 04:46:31

JscottyBieshaar (83 views)
2014-12-05 12:39:02

SHC (92 views)
2014-12-03 16:27:13

CopyableCougar4 (100 views)
2014-11-29 21:32:03

toopeicgaming1999 (160 views)
2014-11-26 15:22:04

toopeicgaming1999 (159 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!