Last active
December 15, 2015 16:29
-
-
Save tylersamples/5289808 to your computer and use it in GitHub Desktop.
Click to move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Camera.h> | |
Camera::Camera(void): | |
m_Model(glm::mat4(0)), | |
m_View(glm::mat4(0)), | |
m_Projection(glm::mat4(0)) | |
{ | |
} | |
Camera::~Camera(void) | |
{ | |
} | |
glm::mat4 Camera::getModel(void) | |
{ | |
return m_Model; | |
} | |
glm::mat4 Camera::getView(void) | |
{ | |
return m_View; | |
} | |
glm::mat4 Camera::getProjection(void) | |
{ | |
return m_Projection; | |
} | |
glm::mat4 Camera::getMVP(void) | |
{ | |
return m_Projection * m_View * m_Model; | |
} | |
void Camera::setModel(glm::mat4 _model) | |
{ | |
m_Model = _model; | |
} | |
void Camera::setView(glm::mat4 _view) | |
{ | |
m_View = _view; | |
} | |
void Camera::setProjection(glm::mat4 _projection) | |
{ | |
m_Projection = _projection; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef VIEW_CAMERA_H_ | |
#define VIEW_CAMERA_H_ | |
#include <glm/glm.hpp> | |
class Camera | |
{ | |
public: | |
Camera(void); | |
~Camera(void); | |
glm::mat4 getModel(void); | |
glm::mat4 getView(void); | |
glm::mat4 getProjection(void); | |
glm::mat4 getMVP(void); | |
void setModel(glm::mat4 _model); | |
void setView(glm::mat4 _view); | |
void setProjection(glm::mat4 _projection); | |
private: | |
glm::mat4 m_Model, m_View, m_Projection; | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SFML/Window.hpp> | |
#include <SFML/Graphics.hpp> | |
#include <glm/gtc/matrix_transform.hpp> | |
#include <glm/gtc/type_ptr.hpp> | |
#include <glm/glm.hpp> | |
#include <Camera.h> | |
#include <iostream> | |
#include <GL/gl.h> | |
Camera m_Camera; | |
glm::vec3 playerLocation; | |
glm::vec3 cameraOffset; | |
glm::vec3 targetLocation; | |
glm::vec3 directionToMove; | |
void initRender(sf::Window &window) | |
{ | |
sf::Vector2u windowSize = window.getSize(); | |
glm::vec2 win = glm::vec2(static_cast<float>(windowSize.x), static_cast<float>(windowSize.y)); | |
glViewport(0,0, win.x, win.y); | |
cameraOffset = glm::vec3(0.f, 0.f, 0.f); | |
m_Camera.setProjection(glm::ortho(0.0f, (float)win.x, (float)win.y, 0.0f, -100.0f, 100.0f)); | |
m_Camera.setView(glm::mat4(1.0f)); | |
playerLocation = glm::vec3(win.x/2, win.y/2,0.f); | |
targetLocation = playerLocation; // same location to start | |
directionToMove = glm::vec3(0.f,0.f,0.f); | |
} | |
void render(sf::Window &window) | |
{ | |
window.setActive(); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glClearColor(0.2f, 0.2f, 0.2f, 1.0f); | |
m_Camera.setModel(glm::translate(glm::mat4(1.0f), cameraOffset)); | |
glColor3f(1.0f, 1.0f, 1.0f); | |
glBegin(GL_QUADS); | |
// apply the player location to the "sprite" | |
glVertex2f(0+playerLocation.x, 10+playerLocation.y); | |
glVertex2f(10+playerLocation.x, 10+playerLocation.y); | |
glVertex2f(10+playerLocation.x, 0+playerLocation.y); | |
glVertex2f(0+playerLocation.x, 0+playerLocation.y); | |
glEnd(); | |
glLoadMatrixf(glm::value_ptr(m_Camera.getMVP())); | |
window.display(); | |
} | |
void moveObject() | |
{ | |
// important stuffs | |
if(targetLocation==playerLocation) | |
{ | |
return; | |
} | |
directionToMove = targetLocation - playerLocation; //this will give you a vector of direction but bare in mind what's below | |
/* | |
[OPTION 1] | |
if targetLocation is 10,15 and playerLocation is 2,2 then direction to move would equal this vector: ([10-2], [15-2]) or (8,13). | |
(8,13) is two different things to us now... It says the direction is postive in both x and y but if we apply this vector directly | |
to our playerLocation it's instant movement; which is not what we want. So now we have compare the direction's to 0 and by doing this | |
we can reasign the direction to move to a much smaller amount, one that doesn't cause instant movement, so that our object moves the | |
way we want it to. | |
[OPTION 2] | |
if((abs(playerLocation.x - targetLocation.x) < 1) // this is because of the * .1f causes decimals | |
&& abs((playerLocation.x - targetLocation.x < 1))) | |
{ | |
playerLocation = targetLocation; | |
} | |
playerLocation = playerLocation + (targetLocation - playerLocation) * .1f; | |
std::cout << "Player: " << playerLocation.x << ", " << playerLocation.y << std::endl; | |
std::cout << "Target: " << targetLocation.x << ", " << targetLocation.y << std::endl; | |
This is the a = a+(b-a)*smooth method. Works but has issues and shouldn't be used unless you understand these issues. | |
See this discussion for that: http://www.reddit.com/r/gamedev/comments/1beigo/android_how_would_i_make_a_sprite_move_to_the/c965set | |
*/ | |
if(!directionToMove.x == 0) | |
{ | |
if(directionToMove.x > 0) | |
{ | |
directionToMove.x = 1; | |
} else | |
{ | |
directionToMove.x = -1; | |
} | |
} | |
if(!directionToMove.y == 0) | |
{ | |
if(directionToMove.y > 0) | |
{ | |
directionToMove.y = 1; | |
} else | |
{ | |
directionToMove.y = -1; | |
} | |
} | |
playerLocation += directionToMove; | |
std::cout << directionToMove.x << ", " << directionToMove.y << std::endl; | |
} | |
int main(int argc, char* argv[]) | |
{ | |
sf::Window window(sf::VideoMode::getDesktopMode(), "Click Move"); | |
initRender(window); | |
while(window.isOpen()) | |
{ | |
sf::Event event; | |
while(window.pollEvent(event)) | |
{ | |
switch(event.type) | |
{ | |
case sf::Event::Closed: | |
window.close(); | |
break; | |
case sf::Event::MouseButtonPressed: | |
if (event.mouseButton.button == sf::Mouse::Right) | |
{ | |
targetLocation = glm::vec3(event.mouseButton.x, event.mouseButton.y, 0); | |
} | |
break; | |
case sf::Event::KeyPressed: | |
switch(event.key.code) | |
{ | |
case sf::Keyboard::Escape: | |
window.close(); | |
break; | |
default: | |
break; | |
} | |
default: | |
break; | |
} | |
} | |
render(window); | |
moveObject(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment