Moving platform not stopping between ranges - c++

I am working on creating a 2d platformer and want to implement moving platforms that move backward and forwards a set distance from their original position.
At the moment, the platform moves in one direction as expected, but does not move back when it reaches the specified range.
What needs to be changed or added to make this work as expected?
KinematicBlock.cpp
/*!
\file kinematicBlock.cpp
*/
#include "kinematicBlock.h"
KinematicBlock::KinematicBlock(b2World * world, sf::Vector2f position, sf::Vector2f size, float orientation, sf::Texture *texture)
{
b2BodyDef l_bodyDef;
b2PolygonShape l_shape;
b2FixtureDef l_fixtureDef;
l_bodyDef.type = b2_kinematicBody;
l_bodyDef.position.Set(position.x, position.y);
l_bodyDef.angle = orientation * DEG2RAD;
m_body = world->CreateBody(&l_bodyDef);
m_body->SetUserData(this); // used by our contact listener
l_fixtureDef.density = mk_fDensity;
l_fixtureDef.friction = mk_fFriction;
l_fixtureDef.restitution = 0.f;
l_fixtureDef.shape = &l_shape;
l_shape.SetAsBox(size.x * 0.5f, size.y * 0.5f);
b2Fixture* bodyFixture = m_body->CreateFixture(&l_fixtureDef);
bodyFixture->SetUserData((void *)PhysicalThing::KINEMATICBLOCK);
setPosition(position);
setSize(size);
setOrigin(size * 0.5f);
setRotation(orientation);
setOutlineThickness(0.f);
setTexture(texture);
setOutlineThickness(0.f);
setTextureRect(sf::IntRect(0, 0, size.x * 24, size.y * 32));
setScale(1, 1);
originPos = position;
m_body->SetLinearVelocity(b2Vec2(0.2f, 0.f));
}
void KinematicBlock::update()
{
if (m_body->GetPosition().x == originPos.x + 0.5)
{
m_body->SetLinearVelocity(b2Vec2(-0.2f, 0.f));
}
if (m_body->GetPosition().x == originPos.x - 0.5)
{
m_body->SetLinearVelocity(b2Vec2(0.2f, 0.f));
}
setPosition(sf::Vector2f(m_body->GetPosition().x, m_body->GetPosition().y));
}
KinematicBlock.h
#pragma once
/*!
\file kinematicBlock.h
*/
#include <Box2D/Box2D.h>
#include <SFML/Graphics.hpp>
#include "physicalThing.h"
/*! \class KinematicBlock
\brief A simple block which can move, rotate and collide with stuff ut is not affected by other dynamic bodies.
*/
class KinematicBlock : public sf::RectangleShape, public PhysicalThing
{
public:
KinematicBlock() {}; //!< Default Contructor
KinematicBlock(b2World * world, sf::Vector2f position, sf::Vector2f size, float orientation, sf::Texture *texture);
void update();//!< Update rendering infomation
sf::Vector2f originPos;
};
Thanks in advance.

Related

OpenGL: Move 2D Orthographic Camera with Mouse

I'm making a level editor for my game with OpenGL in C++. I'm trying to make Editor Camera just like in Unity Engine 2D Scene Camera, but I have an issue when I try to implement mouse movement for the camera (Camera Panning). I'm converting mouse position from screen to world space.
ScreenToWorldSpace Method:
Vector3 Application::ScreenToWorldSpace(int mousex, int mousey)
{
double x = 2.0 * mousex / viewportWidth - 1;
double y = 2.0 * mousey / viewportHeight - 1;
Vector4 screenPos = Vector4(x, -y, -1.0f, 1.0f);
Matrix4 ProjectionViewMatrix = camera1->GetProjectionMatrix() * camera1->GetViewMatrix();
Matrix4 InverseProjectionViewMatrix = glm::inverse(ProjectionViewMatrix);
Vector4 worldPos = InverseProjectionViewMatrix * screenPos;
return Vector3(worldPos);
}
The above method works correctly.
But I'm using ScreenToWorldSpace coordinates to update camera position.
Render Method:
void Application::Render(float deltaTime)
{
Vector3 pos = ScreenToWorldSpace(mousePosition.x, mousePosition.y);
// This is the position of a tile not the camera
position = Vector3(0, 0, 0);
Vector3 rotation = Vector3(0, 0, 0);
Vector3 scale = Vector3(1);
Matrix4 translationMatrix = glm::translate(Matrix4(1.0f), position);
Matrix4 rotationMatrix = glm::eulerAngleYXZ(rotation.y, rotation.x, rotation.z);
Matrix4 scaleMatrix = glm::scale(Matrix4(1.0f), scale);
modelMatrix = translationMatrix * rotationMatrix * scaleMatrix;
if (mouseButtonDown)
{
Console << pos.x << ", " << pos.y << Endl;
camera1->position = Vector3(pos.x, pos.y, -10);
}
{
glScissor(0, 0, 900, 600);
glEnable(GL_SCISSOR_TEST);
glClearColor(236 / 255.0f, 64 / 255.0f, 122 / 255.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, 900, 600);
basicShader->Use();
dirt_grass_tex->Use();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
camera1->SetZoom(zoomFactor);
camera1->Update();
Matrix4 mvp = camera1->GetProjectionMatrix() * camera1->GetViewMatrix() * modelMatrix;
basicShader->SetUniformMat4("MVP", mvp);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glDisable(GL_SCISSOR_TEST);
}
}
Camera Class:
#include "camera.h"
Camera::Camera(int width, int height)
{
swidth = width;
sheight = height;
position = Vector3(0, 0, -10);
rotation = Vector3(0, 0, 0);
m_direction = Vector3(0, 0, -5);
m_up = Vector3(0, 1, 0);
m_right = Vector3(1, 0, 0);
m_offset = Vector3(-swidth / 2 * m_zoom, -sheight / 2 * m_zoom, 0);
m_projection = glm::ortho(0.0f * m_zoom, (float)swidth * m_zoom, 0.0f * m_zoom, (float)sheight * m_zoom, -1000.0f, 0.0f);
}
Camera::~Camera()
{
}
void Camera::Update()
{
Vector3 finalPos = position + m_offset;
m_up = glm::cross(m_right, m_direction);
m_viewMatrix = glm::lookAt(finalPos, finalPos + m_direction, m_up);
m_viewMatrix = glm::scale(m_viewMatrix, Vector3(100));
}
void Camera::SetZoom(float zoom)
{
m_zoom = zoom;
m_offset = Vector3(-swidth / 2 * m_zoom, -sheight / 2 * m_zoom, 0);
m_projection = glm::ortho(0.0f * m_zoom, (float)swidth * m_zoom, 0.0f * m_zoom, (float)sheight * m_zoom, -1000.0f, 0.0f);
}
The following is the output I get when I try to move camera with mouse position converted from Screen to World Space:
if (mouseButtonDown)
{
Console << pos.x << ", " << pos.y << Endl;
position = Vector3(pos.x, pos.y, 0);
}
But if I use mouse position converted from Screen to World space using ScreenToWorldSpace Method the object moves perfectly. Have a look at the following gif:
Following is what I'm trying to achieve:
So I'm Trying to make Game Engine Editor, in that I want to implement Editor Scene Camera like unity / unreal engine scene camera. Following is the editor I'm currently working on:
I tried looking into different resources, but i'm clueless. Help me understand how to move the camera with mouse.
What I think is happening:
Since I'm converting mouse position from screen to world space using camera's projectionView matrix and using those world coordinates to move camera position is causing the problem, because when ever camera moves, projectionView is updated which in turn changes mouse position relative to viewMatrix recursively.
I would Appreciate some help.
Ordinarily, you wouldn't want to write the mouse position directly into the camera location (because that will be of limited use in practice - whenever you click on the screen, the camera would jump).
What you probably want to do something along these lines:
Vector3 g_lastPosition;
void onMousePressed(int x, int y) {
// record starting position!
g_lastPosition = ScreenToWorldSpace(x, y);
}
void onMouseMove(int x, int y) {
// find the difference between new position, and last, in world space
Vector3 new_pos = ScreenToWorldSpace(x, y);
Vector3 offset = new_pos - g_lastPosition;
g_lastPosition = new_pos;
// now move camera by offset
camera->position += offset
}
If you are in an orthographic view, then really you don't need to worry about the projection matrix at all.
int g_lastX;
int g_lastY;
void onMousePressed(int x, int y) {
// store mouse pos
g_lastX = x;
g_lastY = y;
}
void onMouseMove(int x, int y) {
// find the difference between new position, and last, in pixels
int offsetX = x - g_lastX;
int offsetY = y - g_lastY;
// update mouse pos
g_lastX = x;
g_lastY = y;
// get as ratio +/- 1
float dx = ((float) offsetX) / swidth;
float dy = ((float) offsetY) / sheight;
// now move camera by offset (might need to multiply by 2 here?)
camera->position.x += camera->m_offset.x * dx;
camera->position.y += camera->m_offset.y * dy;
}
But in general, for any mouse based movement, you always want to be thinking in terms of adding an offset, rather than setting an exact position.

Making objects rotate in ellipses in JOGL

I've been working on an animation of the Solar System. So far I've gotten the planets to rotate in circles around the Sun. Now I want to make them rotate in ellipses with perihelion and aphelion so that the planets move towards and away from the Sun.
I have some problems understanding the math behind it. I've seen examples like this one where he simply draw the ellipse using GL_LINE_LOOP. I use makeRotationAxis() and makeTranslation() from the FloatUtil class, and I use time as a way to tell how fast the rotation should go. I think my code says more than I can express, so here we go (I've edited the code using the ellipse-equation commented below).
Edit: I've fixed some bugs and simplified the code (also, the planets are moving in the X and Z axis, that's why makeTranslation(.., .., tx, 0f, ty);), but I still cannot get the elliptical orbit, now the planets' rotation is circularly.
The main class:
.....
//Inside init()
// Store the starting time of the application
start = System.currentTimeMillis();
// The planets rotational speed around the planet's own axis (planetary day)
float[] ownRotation = {15.0f, 10.0f, 06.0f, 03.0f, 02.0f, 01.5f, 00.5f, 00.2f, 00.1f};
// The planets rotational speed around the Sun's axis (planetary year)
float[] sunRotation = {10.0f, 08.0f, 06.0f, 04.0f, 02.0f, 01.3f, 01.0f, 00.5f, 00.2f};
// The planets distance from the Sun on the X axis
float[] xDistanceFromSun = {1.6f, 4.0f, 9.0f, 13.0f, 20.0f, 24.0f, 27.0f, 30.0f, 34.0f};
// The planets distance from the Sun on the Y axis
float[] zDistanceFromSun = {0.8f, 2.0f, 4.5f, 6.5f, 10.0f, 12.0f, 13.5f, 15.0f, 17.0f};
// The scale size of the planets
float[] scale = {0.2f, 0.3f, 0.5f, 0.2f, 1.2f, 1.2f, 1.0f, 0.4f, 0.1f};
// The sun object, specifies it's rotation
sun = new Sun(0.5f, 1f);
// Make the planet objects and put them in the array
for(int i = 0; i < scale.length; i++)
planets.add(new Planet( ownRotation[i],
sunRotation[i],
xDistanceFromSun[i],
zDistanceFromSun[i],
scale[i]
));
// Make the moon object
moon = new Moon(
planets.get(2).getPlanetMatrix(),
1.0f,
02.0f,
0.3f);
}
.....
// Inside display()
long now = System.currentTimeMillis();
// Find a time delta for the time passed since the start of execution
float diff = (float) (now - start) / 10000;
// Copy the Sun's matrix to the server
pointer[0].asFloatBuffer().put(sun.getSunMatrix());
// Update the Sun's Matrix
sun.updateSun(diff);
// Copy the plantes's matrixes to the server and then update them
for(int i = 0; i < 9; i++) {
pointer[i+1].asFloatBuffer().put(planets.get(i).getPlanetMatrix());
planets.get(i).updatePlanet(diff);
}
// Copy the moon's matrix to the server
pointer[10].asFloatBuffer().put(moon.getMoonMatrix());
// Update the Moon's Matrix
moon.updateMoon(diff, planets.get(2).getPlanetMatrix());
.....
The Planet class:
public class Planet {
private float sunRotation;
private float ownRotation;
private float diff;
private float[] ownRotationMatrix;
private float[] sunRotationMatrix;
private float[] scaleMatrix;
private float[] ellipticRotationMatrix;
private float[] planetMatrix;
/**
* #param ownRotation The planet's rotation around it's own axis
* #param sunRotation The planet's rotation around the Sun
* #param xDistance The distance from the Planet to the Sun at X = max/min
* #param zDistance The distance from the Planet to the Sun at Y = max/min
* #param scale The scale/size of the Planet
*/
public Planet (float ownRotation, float sunRotation, float xDistance, float zDistance, float scale) {
// Set the values
this.ownRotation = ownRotation;
this.sunRotation = sunRotation;
// Create the matrixes needed
setOwnRotationMatrix(ownRotation);
setSunRotationMatrix(sunRotation);
setEllipticRotationMatrix(xDistance, zDistance);
setScaleMatrix(scale);
// Create the planet matrix
createPlanet();
}
public void setOwnRotationMatrix(float ownRotation) {
this.ownRotationMatrix = FloatUtil.makeRotationAxis(new float[16], 0, ownRotation, 0f, 1f, 0f, new float[3]);
}
public void setSunRotationMatrix(float sunRotation) {
this.sunRotationMatrix = FloatUtil.makeRotationAxis(new float[16], 0, sunRotation, 0f, 1f, 0f, new float[3]);
}
public void setScaleMatrix(float scale) {
this.scaleMatrix = FloatUtil.makeScale(new float[16], false, scale, scale, scale);
}
// tx = a*sin(ang_rad) + sqrt(a*a-b*b);
// tz = b*cos(ang_rad);
public void setEllipticRotationMatrix(float xDistance, float zDistance) {
float tx = (float)(xDistance * Math.sin(diff) + Math.sqrt(xDistance * xDistance - zDistance * zDistance));
float tz = (float)(zDistance * Math.cos(diff));
System.out.println("tx: " + tx);
System.out.println("tz: " + tz);
this.ellipticRotationMatrix = FloatUtil.makeTranslation(new float[16], false, tx, 0f, tz);
}
public void createPlanet() {
this.planetMatrix =
FloatUtil.multMatrix(
FloatUtil.multMatrix(
FloatUtil.multMatrix(
sunRotationMatrix,
ellipticRotationMatrix,
new float[16]
),
ownRotationMatrix,
new float[16]
),
scaleMatrix,
new float[16]
);
}
public void updatePlanet(float diff) {
setOwnRotationMatrix(ownRotation * diff);
setSunRotationMatrix(sunRotation * diff);
createPlanet();
}
public float[] getOwnRotationMatrix() {
return this.ownRotationMatrix;
}
public float[] getPlanetMatrix() {
return this.planetMatrix;
}
}

Setting the position of a sprite not working in SFML/Box2D

I'm trying to get physics working in SFML using Box2D, but for some reason I can't set the position of my sprite in my class.
I created a TestBox for my game, basically just a box that has physics, but it doesn't seem to be working. I am able to set the position of the box in my class constructor, but not in the update function I created.
So onto the code:
TestBox.h:
#pragma once
#include<Box2D/Box2D.h>
#include<SFML/Graphics.hpp>
#include<string>
#include<iostream>
class TestBox : public sf:: Drawable
{
public:
TestBox(b2World *world, std::string texturePath, float x, float y, float w, float h);
void update();
b2Body *getBody() { return body; }
b2Fixture *getFixture() { return fixture; }
private:
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const;
b2Body *body = nullptr;
b2Fixture *fixture = nullptr;
sf::Texture texture;
sf::Sprite boxSprite;
};
TestBox.cpp:
#include "TestBox.h"
TestBox::TestBox(b2World *world, std::string texturePath, float x, float y, float w, float h) {
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(x, y);
body = world->CreateBody(&bodyDef);
b2PolygonShape boxShape;
boxShape.SetAsBox(w / 2.0f, h / 2.0f);
b2FixtureDef fixtureDef;
fixtureDef.shape = &boxShape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
fixture = body->CreateFixture(&fixtureDef);
texture.loadFromFile(texturePath.c_str());
boxSprite.setTexture(texture);
boxSprite.setOrigin(16.f, 16.f);
boxSprite.setPosition(30.0f * body->GetPosition().x, 30.0f * body->GetPosition().y);
boxSprite.setRotation(body->GetAngle() * 180 / b2_pi);
}
void TestBox::update() {
std::cout << boxSprite.getPosition().x << ", " << boxSprite.getPosition().y << std::endl;
boxSprite.setPosition(30.0f * body->GetPosition().x, 30.0f * body->GetPosition().y);
boxSprite.setRotation(body->GetAngle() * 180 / b2_pi);
}
void TestBox::draw(sf::RenderTarget &target, sf::RenderStates states) const {
target.draw(boxSprite, states);
}
So, I've tried to narrow down my problem as much as possible, and rule as much as I could out. Here is what I found:
I am able to set the position in the constructor, this works fine.
I have been checking the position of the Box2D body, the position of that changes fine.
I have checked the math:
30.0f * body->GetPosition().x
30.0f * body->GetPosition().y
this is accurate and works fine.
The update function does run, it prints normally.
The result of printing the position is always the start position
Let me know if I left out any information you could need.

Camera Translation in OpenGL

I am trying to do a class to walk through the world in OpenGL, but I am having problems with the mathematics. My idea here is to use the function lookAt from glm to set the observer in the position I wanted, and then just operate with the points that I pass to the function.
I think the functions to do rotations that I made are correct, but the translation part in the walk method seems to be wrong, and when I try to walk in the world if I just translate, or just rotated, things go right, but when I do both things just get messed.
here is the class so far:
#ifndef OBSERVER_H
#define OBSERVER_H
#include <GL/gl.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
class Observer {
private:
glm::vec3 eye, center, upp;
public:
glm::mat4 view;
Observer() {}
~Observer() {}
void initialize(glm::vec3 eye, glm::vec3 center, glm::vec3 upp);
void walk(GLfloat distance);
void pitch(GLfloat pitch);
void yaw(GLfloat yaw);
void roll(GLfloat roll);
void setView();
};
void Observer::initialize(glm::vec3 eye, glm::vec3 center, glm::vec3 upp)
{
this->eye = eye;
this->center = center;
this->upp = upp;
}
void Observer::walk(GLfloat distance)
{
glm::vec3 vector = glm::normalize(center - eye);
glm::vec3 translate = vector*distance - vector;
eye += translate;
center += translate;
upp += translate;
}
void Observer::roll(GLfloat roll) {
glm::mat4 rotate(1.0f);
rotate = glm::rotate(rotate, roll, glm::vec3(center - eye));
center = glm::vec3(rotate * glm::vec4(center, 1.0f));
upp = glm::vec3(rotate * glm::vec4(upp, 1.0f));
}
void Observer::yaw(GLfloat yaw) {
glm::mat4 rotate(1.0f);
rotate = glm::rotate(rotate, yaw, glm::vec3(upp - eye));
center = glm::vec3(rotate * glm::vec4(center, 1.0f));
upp = glm::vec3(rotate * glm::vec4(upp, 1.0f));
}
void Observer::pitch(GLfloat pitch) {
glm::mat4 rotate(1.0f);
glm::vec3 cross = glm::cross(center - eye, upp - eye);
rotate = glm::rotate(rotate, pitch, cross);
center = glm::vec3(rotate * glm::vec4(center, 1.0f));
upp = glm::vec3(rotate * glm::vec4(upp, 1.0f));
}
void Observer::setView()
{
view = glm::lookAt(eye, center, upp);
}
#endif
So right before I starting draw things I set the view matrix with this class in other part in the program. Can someone tell me if my maths are right?
When you walk, you only want to transform the eye and center position, not the upp vector. Just remove the upp += translate; line.

Ray picking mess

I am using ray picking to find the boundaries for a character. It's not optimal, but it's the best I can do and will have to do; I need to have (close to) pixelperfect collisions and also I have lots and lots of objects.
I do not however get the raypicking to work correctly. It collides, but not at the right places. I tried adjust the size of the ray, and more to no avail.
Excuse me for the messy code, I just threw it together.
private void renderCollision(Vector3f dir){
//
// Render the models
//
glPushMatrix();
glPushAttrib(GL_ALL_ATTRIB_BITS);
float size = 10.0f;
Picker.startPicking2D(10, 10, 20, 20, -1.0f, size);
//Picker.startPicking2D(10, 10, 20, 20, 0.1f, 20.0f);
glClear (GL_COLOR_BUFFER_BIT);
glDisable(GL_BLEND);
glDisable(GL_ALPHA);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glDepthMask(true);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(100.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_TEXTURE_2D);
glLoadIdentity ();
//box.drawMoving(camera);
player.move(dir);
glTranslatef(-player.position.x, -player.position.y + size / 2.0f, -player.position.z);
glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
box.drawAll();
glDisable(GL_DEPTH_TEST);
boolean hit = Picker.getHit();
if (hit) {
player.move(new Vector3f(-dir.x, -dir.y, -dir.z));
}
Picker.stopPicking();
glPopAttrib();
glPopMatrix();
}
public class Picker {
private static IntBuffer selBuffer;
private static int hits;
private static int xSelected;
private static int ySelected;
/**
* Makes the game available for picking (when in 3D mode)
*
* #param xMouse The x coordinate of the mouse on the screen
* #param yMouse The y coordinate of the mouse on the screen
*/
public static void startPicking3D(int xMouse, int yMouse, int screenWidth, int screenHeight, float near, float far) {
startPickingGeneric(xMouse, yMouse);
GLU.gluPerspective(SCREEN_FOV, SCREEN_RAT,
near, far);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
}
/**
* Makes the game available for picking (when in 2D mode)
*
* #param xMouse The x coordinate of the mouse on the screen
* #param yMouse The y coordinate of the mouse on the screen
*/
public static void startPicking2D(int xMouse, int yMouse, int screenWidth, int screenHeight, float near, float far) {
startPickingGeneric(xMouse, yMouse);
GL11.glOrtho(0, screenWidth, 0, screenHeight, near, far);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
}
/**
* Makes the game available for picking (generic)
*
* #param xMouse The x coordinate of the mouse on the screen
* #param yMouse The y coordinate of the mouse on the screen
*/
private static void startPickingGeneric(int xMouse, int yMouse){
// The selection buffer
selBuffer = ByteBuffer.allocateDirect(1024).order(ByteOrder.nativeOrder()).
asIntBuffer();
IntBuffer vpBuffer = ByteBuffer.allocateDirect(64).
order(ByteOrder.nativeOrder()).asIntBuffer();
// Size of the viewport. [0] Is <x>, [1] Is <y>, [2] Is <width>, [3] Is <height>
int[] viewport = new int[4];
// Get the viewport info
GL11.glGetInteger(GL11.GL_VIEWPORT, vpBuffer);
vpBuffer.get(viewport);
// Set the buffer that OpenGL uses for selection to our buffer
GL11.glSelectBuffer(selBuffer);
// Change to selection mode
GL11.glRenderMode(GL11.GL_SELECT);
// Initialize the name stack (used for identifying which object was selected)
GL11.glInitNames();
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPushMatrix();
GL11.glLoadIdentity();
// Create 5x5 pixel picking region near cursor location
GLU.gluPickMatrix((float) xMouse, (float) yMouse,
5.0f, 5.0f, IntBuffer.wrap(viewport));
}
/**
* Stops the picking mode
*/
public static void stopPicking(){
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPopMatrix();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
hits = 0;
hits = GL11.glRenderMode(GL11.GL_RENDER);
}
/**
* Gets the tile the mouse points to
*
* #return TileCoords object with the coordinates of the selected tile
*/
public static boolean getHit(){
int[] buffer = new int[256];
xSelected = -1000;
ySelected = -1000;
selBuffer.get(buffer);
if (hits > 0) {
// If there were more than 0 hits
xSelected = buffer[3]; // Make our selection the first object
ySelected = buffer[4];
int depth = buffer[1]; // Store how far away it is
for (int i = 1; i < hits; i++) {
// Loop through all the detected hits
// If this object is closer to us than the one we have selected
if (buffer[i * 4 + 1] < (int) depth) {
xSelected = buffer[i * 4 + 3]; // Select the closest object
ySelected = buffer[i * 4 + 4];
depth = buffer[i * 4 + 1]; // Store how far away it is
}
}
return true;
}
return false;
}
}
I should rotate around x-axis instead of y-axis and then translate.