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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Snizzlenose

Pages: [1]
1
std::atan2. Yes, the std is required in correct C++.

But you could use thor::polarAngle() which would greatly simplify your code. See my signature (Thor -> API doc -> Vectors module) :)

Could you explain how thor::polarAngle( ) is defined? 
I don't understand the result I got when I searched for it in the github repo...
template <typename T>
T polarAngle(const sf::Vector2<T>& vector)
{
        assert(vector != sf::Vector2<T>());
        return TrigonometricTraits<T>::arcTan2(vector.y, vector.x);
}

2
General / Trouble manipulating an angle based on the mouse position.
« on: June 02, 2015, 06:28:04 pm »
I'm trying to rotate a rectangle, representing an gun, around the center of the player position. It's rotating normally, aiming towards the mouse, as long as the mouse position x is less than the arm position x. If it goes above that it rotates 180 degrees and instead aims in the opposite direction. I made a hacky fix by add 180 to the angle reactively, but it's pretty buggy and I'm wondering if there's any better and more proper solution?

void Player::StartShooting( float mouse_x, float mouse_y, float timestep )
{
        for( unsigned int i = 0; i < bullet.size( ); i++ )
        {
                if( bullet[i].GetActive( ) == false )
                {
                        bullet[i].SetOrigin( GetPosition( ).x + GetHitbox( ).x / 2, GetPosition( ).y + GetHitbox( ).y / 2 );
                        sf::Vector2f mousePos = { mouse_x, mouse_y };
                        sf::Vector2f projEndNorm = vectorNormal( mousePos - bullet[i].GetOrigin( ) );
                        bullet[i].SetPosition( bullet[i].GetOrigin( ) + projEndNorm );
                        float angle = atan( projEndNorm.y / projEndNorm.x ) * 180.f / 3.141592f;
                        if( mouse_x >= GetPosition( ).x )
                        {
                                angle += 180;
                        }
                        shapeArm.setRotation( angle );
                        if( projEndNorm.x != 0 &&
                                projEndNorm.y != 0 )
                        {
                                bullet[i].SetActive( true );
                                bullet[i].SetStep( 0 );
                                bullet[i].SetVelocity( vectorNormal( bullet[i].GetPosition( ) - bullet[i].GetOrigin( ) ).x * bullet[i].GetInitialVelocity( ) + GetVelocity( ).x / 5,
                                                                           vectorNormal( bullet[i].GetPosition( ) - bullet[i].GetOrigin( ) ).y * bullet[i].GetInitialVelocity( ) + GetVelocity( ).y / 5 );

                                break;
                        }
                }
        }
}

3
Graphics / setOrigin( ), good or bad?
« on: May 30, 2015, 06:38:58 pm »
Should you keep the origin as it is in the top left corner or change it to say center mass of your sprite / shape? 
I assume this depends on preference, but does it come with implications further on?

4
Personally, you're doing it the hard way.

Try vector math! Makes stuff like this much easier.

For example:
// constants
const sf::Vector2f CENTER = static_cast<sf::Vector2f>(window.getSize() / 2u);
constexpr float LENGTH = 35.f;
constexpr float VELOCITY = 122.f;

// the projectile
sf::VertexArray line(sf::Lines, 2);

// calculate projectile direction based off: mouse press location and center of window
sf::Vector2f mousePos = {static_cast<float>(event.mouseButton.x), static_cast<float>(event.mouseButton.y)};
sf::Vector2f projEndNorm = vecNormal(mousePos - CENTER);
line[0].position = CENTER + projEndNorm;        // offset from the center slightly so: line[0].position - CENTER != {0, 0}
line[1].position = projEndNorm * LENGTH + CENTER;

// update
line[0].position += vecNormal(line[0].position - CENTER) * VELOCITY * dt.asSeconds();
line[1].position += vecNormal(line[1].position - CENTER) * VELOCITY * dt.asSeconds();

// the vecNormal function:
sf::Vector2f vecNormal(const sf::Vector2f& vec)
{
        if(vec.x != 0 && vec.y != 0)
                return vec / std::sqrt(vec.x * vec.x + vec.y * vec.y);    // vector / length = unit vector of the vector
        else
                return {0, 0};
}
 

Unit Vector Definition

Minimal and Naive Example if you need it

Awesome thanks! I'll try that tomorrow and use your naive guide as a reference to recreate it. 
However I have some questions beforehand, for the things I haven't encountered yet: 
const sf::Vector2f CENTER = static_cast<sf::Vector2f>(window.getSize() / 2u);
What is static_cast used for, it's a type of conversion from what I understand but what difference does it do to using ( sf::Vector2f ), or does that not work? 
is 2u supposed to be something specific or just half the window size? 
constexpr float LENGTH = 35.f; 
From what I understand constexpr is like const used for objects and functions, does it have purpose for a simple data type? 
sf::Vector2f mousePos = {static_cast<float>(event.mouseButton.x), static_cast<float>(event.mouseButton.y)};
This captures the position of the mouse as the mousebutton is pressed and doesn't update right?
As for using CENTER as a whole, what does it do? I assume that's why you don't need to calculate angles from the player. 
I probably should just read up a lot more til I understand, but vectors, are they just positions for dots right, with x, y , z, etc and the normal is the product of two or more vectors, or does vectors always have a magnitude / normal include together with the position?

5
I'm trying to implement projectiles, which for now is just a line (VertexArray). 
I managed to create a line from the player to the mouse, and now I'm trying with a constant hypotenuse, projectileLength, but I'm having problems getting the correct position for the new x and y. 
it sets the vertex strange position close to the top left corner of the window and moves around a circle like area as I move the mouse. 
Github: https://github.com/Snizzlenose/2D-SFML-shooter-v2
The function and the functioncall: 

main.cpp:
world.player.Shooting( sf::Mouse::getPosition( window ).x, sf::Mouse::getPosition( window ).y );

Player.cpp:
void Player::Shooting( double mouse_x, double mouse_y )
{
        /* Calculate angle with restricted projectile length (hypotenuse) */
        double delta_x = GetPosition( ).x - mouse_x;
        double delta_y = GetPosition( ).y - mouse_y;
        double radians = atan( delta_y / delta_x );
        double new_x = cos( radians ) * _projectileLength;
        double new_y = sin( radians ) * _projectileLength;

        if( _shooting == true )
        {
                projectile[0].position = sf::Vector2f( GetPosition( ).x, GetPosition( ).y );
                projectile[1].position = sf::Vector2f( new_x, new_y );
                std::cout << "Shooting!\n";
        }
        else
        {
                projectile[0].position = sf::Vector2f( 9999, 9999 );
                projectile[1].position = sf::Vector2f( 9999, 9999 );
        }
}

6
I'm trying to separate my code into different threads, using C++11 <thread>, namely player movement to make it consistent and not dependent on the speed of  the game loop.
Don't.

You may think it's a good idea and may think it will make things "independent", but truth is, it will make everything a lot more complicated and it won't even be independent.
Multi-threading is an advanced topic and needs a lot of knowledge on synchronization and parallel programming in general. Additionally you need to have all the object's logic updated before rendering a frame, thus making the whole parallel part totally useless.

There are a lot of threads on the forum talking about if you're interested.

Oh okay, I figured threads was a set and forget kind of thing, but if they need a lot more work, they're unneeded for a simple game like mine. 
Should I worry about player movement being inconsistent as more computation is required in the main gameloop, as I'm essentially teleporting the sprite every loop individual loop?

7
I'm trying to separate my code into different threads, using C++11 <thread>, namely player movement to make it consistent and not dependent on the speed of  the game loop. 
The game builds and runs in debug mode (VS2013) but the problem I'm facing is that my player sprite's position, a red rectangle, is not being updated on my window despite it's position is getting registered back-end, printing my test outputs in the console, and interacting with my collision function. 
The movement works when I put world.player.movement in the game loop (note: not world.movement). 
Any ideas why it doesn't work and suggestions to fix it?

Github link: https://github.com/Snizzlenose/2D-SFML-shooter 

main.cpp
(click to show/hide)

World.h 
(click to show/hide)

World.cpp 
(click to show/hide)

Player.h 
(click to show/hide)

Player.cpp 
(click to show/hide)

Ground.h 
(click to show/hide)

Ground.cpp 
(click to show/hide)

Pages: [1]