Jump to content

  • Log In with Google      Sign In   
  • Create Account


Kain5056

Member Since 22 Dec 2012
Offline Last Active Yesterday, 02:54 AM
-----

Topics I've Started

Homing missile problem

05 June 2014 - 07:14 AM

I'm trying to follow this Actionscript tutorial for homing missiles, translating it to C++ with SFML.

 

But I seem to have a problem with this piece of code:

 

In Actionscript:

var rotation:int = Math.atan2(targetY, targetX) * 180 / Math.PI;
        if (Math.abs(rotation - missile.rotation) > 180)
        {
            if (rotation > 0 && missile.rotation < 0)
                missile.rotation -= (360 - rotation + missile.rotation) / ease;
            else if (missile.rotation > 0 && rotation < 0)
                missile.rotation += (360 - rotation + missile.rotation) / ease;
        }
        else if (rotation < missile.rotation)
            missile.rotation -= Math.abs(missile.rotation - rotation) / ease;
        else
            missile.rotation += Math.abs(rotation - missile.rotation) / ease;

Translated in C++ with SFML:

int current = rocket_sprite.getRotation();
    if( abs( rotation - current ) > 180 )
    {
        if( rotation > 0 && current < 0 ) rotation -= ( 360 - rotation + current ) / ease;
        else if( current > 0 && rotation < 0 ) rotation += ( 360 - rotation + current ) / ease;
    }
    else if( rotation < current ) rotation -= abs( current - rotation ) / ease;
    else rotation += abs( rotation - current ) / ease;

With this code, the missile acts crazy, getting stuck in places, continually rotating in place for no reason and other stuff like that.

 

I suspect that the problem lies in the fact that .getRotation() returns absolute values, but I don't know how to even test it.

 

Here is my complete code:

#include <math.h>
#define PI 3.14159265

class rocket
{
private:
    sf::Texture rocket_texture;
    sf::Sprite rocket_sprite;
    sf::Vector2f position;
    int rotation;

public:
    rocket();
    bool rocket_load();
    void rocket_show( sf::RenderWindow * window );
    void set_position( sf::Vector2f new_position );
    void rocket_rotate( sf::RenderWindow * window );
    void rocket_move();
};

rocket::rocket()
{
    rotation = 0.f;
}

bool rocket::rocket_load()
{
    rocket_texture.loadFromFile( "rocket.png" );
    rocket_sprite.setTexture( rocket_texture );

    return true;
}

void rocket::rocket_show( sf::RenderWindow * window )
{
    rocket_sprite.setOrigin( rocket_texture.getSize().x / 2 , rocket_texture.getSize().y / 2 );
    rocket_sprite.setPosition( position );
    window->draw( rocket_sprite );
}

void rocket::set_position( sf::Vector2f new_position )
{
    position = new_position;
}

void rocket::rocket_rotate( sf::RenderWindow * window )
{
    sf::Vector2i mouse_position = sf::Mouse::getPosition( *window );
    int targetx = mouse_position.x - position.x;
    int targety = mouse_position.y - position.y;

    rotation = atan2( targety , targetx ) * 180 / PI;

    int ease = 5;
    int current = rocket_sprite.getRotation();
    if( abs( rotation - current ) > 180 )
    {
        if( rotation > 0 && current < 0 ) rotation -= ( 360 - rotation + current ) / ease;
        else if( current > 0 && rotation < 0 ) rotation += ( 360 - rotation + current ) / ease;
    }
    else if( rotation < current ) rotation -= abs( current - rotation ) / ease;
    else rotation += abs( rotation - current ) / ease;

    rocket_sprite.setRotation( rotation );
}

void rocket::rocket_move()
{
    int speed = 5;
    float speedx = 0;
    float speedy = 0;
    speedx = speed * ( 90 - abs( rotation ) ) / 90;
    if( rotation < 0 ) speedy = -speed + abs( speedx );
    else speedy = speed - abs( speedx );
    position.x += speedx;
    position.y += speedy;
}

If anyone can point me to the right direction, I would really appreciate it. :-)

Thank you in advance. :-)


How to stick on slopes?

25 November 2013 - 07:14 PM

So, I was making my first game, and I have encountered a problem I did not expect. blink.png

 

19l43s.jpg

 

As you can see, instead of staying on the ground, my entity just flies off when it meets a downward slope. wacko.png

I want the entity to stick to the ground, but I do not want to change its moving and falling speed.

How can I do that? unsure.png

 

Thanks in advance. happy.png


y = mx + b for sloped tiles

27 September 2013 - 09:56 AM

I'm trying to implement slopes in my first platform game in C++, and I seem to have some trouble.

I need to calculate the player's y position in a 45 degrees slope using the slope's size and coordinates and the player's x position.

I have found the equation y = m * x + b, but I'm not sure how to use it.

 

I need to find position.y using position.x, tile.x, tile.y, tile_size and player_size (both the player and the tiles are square so their width and their height are equal).

 

I know that m = ( y2 - y1 ) / ( x2 - x1 ) and b = y1 - m * x1

 

But I don't know how to find y1, y2, x1 and x2 based on the slope's degree.

 

I searched Google and found nothing, so if someone more experienced could help I would really appreciate it.

Thank you in advance. smile.png


Small jitter on slope tiles.

24 September 2013 - 07:42 AM

I'm trying to implement 45 degrees slopes in my platform game, and I came up with the following code:

//position.x, position.y, width and height are the sprite's coordinates and size.

void entity::slope_collision_detection( tile * Tile )
{
     if( position.y >= Tile->y + ( tile_size - ( position.x + width - Tile->x ) ) - height )
     {
          position.y = Tile->y + ( tile_size - ( position.x + width - Tile->x ) ) - height;
     }

      if( position.y + height <= Tile->y ) position.y = Tile->y - height;
}

This code is for the left-pointing slope, btw.

It works great, but the player sprite jitters just a little bit when it's standing still on the slope.
It's not very noticeable, but it's really annoying.

I have tried many things to get rid of it, like changing the order of the collision, moving and displaying functions, or tinkering with the equations, but nothing seems to work.

Can anyone more experienced point me to what I am doing wrong here?

Thank you in advance.  smiley.gif


External functions?

07 August 2013 - 02:50 AM

I have a newbie question about C++.

My program can read variables, strings and stuff from an external file with no problem.

But is there a way to make my program read whole functions, or at least if statements from an external file?

 

Like, having a .txt file with "if( something > something else) something = 0" written in it and my program being able to read it from the file and execute it in some way?

 

Thank you in advance. :-)


PARTNERS