Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: A Rectangle Shake Around  (Read 1279 times)

0 Members and 1 Guest are viewing this topic.

rmxhaha

  • Newbie
  • *
  • Posts: 8
    • View Profile
A Rectangle Shake Around
« on: August 11, 2013, 05:54:24 pm »
A month ago I experiment with viewport and everything I tried works
Now, I tried putting them into my project as a Camera system

and something bothers me
A Rectangle seems to shake around

Here is the compiled program
https://dl.dropboxusercontent.com/u/33612776/Kuroneko.rar

Left - Move Left
Right - Move Right
Space - Jump ( can jump twice )


This White Rectangle which shakes around represent "The Hero" and it's also the anchor point of the camera

Here is some code regarding the Camera system

Code: [Select]
#ifndef CAMERA_H
#define CAMERA_H

#include "Global.h"
#include "Physic.h"
#include "Hero.h"

#include <SFML/Graphics.hpp>

namespace Camera {
void initialize();

void update( float dt );

void calibrate( sf::RenderWindow& App );
}

#endif
Code: [Select]

#include "Camera.h"

namespace Camera {
namespace {
sf::View view;
const sf::FloatRect viewPortBig ( 0.f, 0.f, 1.f, 1.f );

// the first 2 parameters are the camera coordinate
sf::FloatRect viewRect ( 100.f, 0.f, WINDOW_WIDTH, WINDOW_HEIGHT );

const float focus_dx = -WINDOW_WIDTH / 2;
const float focus_dy = WINDOW_HEIGHT / 3.f;

Physic::Body HeroBody;
}

void initialize(){
HeroBody = Hero::get_hero_body();
sf::Vector2f position = HeroBody.getPosition();

viewRect.left = focus_dx + position.x;
viewRect.top = position.y - focus_dy;
}

void update( float dt ){
}

void calibrate( sf::RenderWindow& App ){
static sf::Vector2f TargetCoordinate;
static sf::Vector2f position;

position = HeroBody.getPosition();

TargetCoordinate.x = focus_dx + position.x;
TargetCoordinate.y = -position.y + focus_dy;


viewRect.left += ( TargetCoordinate.x - viewRect.left ) / 10.f;
viewRect.top += ( TargetCoordinate.y - viewRect.top ) / 10.f;

view.setViewport( viewPortBig );

view.reset( viewRect );
App.setView( view );
}
}
Code: [Select]
#include "TextureManager.h"
#include "Utility.h"
#include <iostream>

using namespace std;


int main()
{
    Physic::initialize();
Hero::initialize();
    Camera::initialize();

Physic::set_gravity( sf::Vector2f( 0.f, -500.f ) );

    sf::RenderWindow window( sf::VideoMode( WINDOW_WIDTH, WINDOW_HEIGHT ), "Project Kuroneko By Rmxhaha" );
    window.setFramerateLimit( 50 );

// -- map things
Physic::add_body( 100, 100, 40000, 70, "static" );
Physic::add_body( 500, 400, 400, 70, "static" );


    sf::Clock Timer;
    float delta_time = 0.f;

    while ( window.isOpen() )
    {
delta_time = Timer.getElapsedTime().asSeconds();
        Timer.restart();

        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }


Physic::update( delta_time );
Hero::update( delta_time );

        Camera::update( delta_time );
        Camera::calibrate( window );

// -- clearing the screen
        window.clear( ClearColor );

// -- draw everything
Physic::debug_draw( window );

// -- end of drawing
        window.display();
    }

    return 0;
}

Did I do something wrong ?
I don't think I do but well I don't know much about viewport anyway

Can someone point out the source of this problem ?
Is the problem is outside the program ? Like my GPU is too old or something ?

I used SFML 2.0 with "GCC 4.7 TDM (SJLJ) - 32 bits" written beside the download button
and CodeBlocks 12.11 with it's default compiler
and Window 7 Ultimate
« Last Edit: August 11, 2013, 07:16:25 pm by rmxhaha »

 

anything