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.


Topics - Jove

Pages: 1 [2]
16
Graphics / A little problem with SetSubRect
« on: March 31, 2011, 03:45:31 pm »
Apologies if I've missed the obvious.

I'm using SetSubRect to point at 64x64 images in a sheet and have noticed that when moving the sprites, I get part of the adjacent image 'creeping' in.

In this example, which is basically the tutorial, you can notice it as a white line at the edge of the sprite. Be patient when moving it around, sometimes it takes a little while to happen.

I suppose a workaround would be to leave a blank pixel on every edge, but I'd prefer to understand it better.

Thanks!

The test image is here:




Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

    sf::Image Image;
    if (!Image.LoadFromFile("spritesheet.png"))
        return EXIT_FAILURE;

Image.SetSmooth(false);

    sf::Sprite Sprite(Image);

    Sprite.SetColor(sf::Color(255, 255, 255, 255));
    Sprite.SetPosition(200.f, 100.f);
    Sprite.SetScale(.5, .5);
Sprite.SetSubRect(sf::IntRect(64, 64, 128, 128));

float xpos = 200;
float ypos = 100;

    // Start game loop
    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Get elapsed time
        float ElapsedTime = App.GetFrameTime();

        // Move the sprite
        if (App.GetInput().IsKeyDown(sf::Key::Left))  xpos -= 10 * ElapsedTime;
        if (App.GetInput().IsKeyDown(sf::Key::Right)) xpos += 10 * ElapsedTime;
        if (App.GetInput().IsKeyDown(sf::Key::Up))    ypos -= 10 * ElapsedTime;
        if (App.GetInput().IsKeyDown(sf::Key::Down))  ypos += 10 * ElapsedTime;

Sprite.SetPosition(xpos, ypos);
       
        App.Clear();
        App.Draw(Sprite);
        App.Display();
    }

    return EXIT_SUCCESS;
}

17
General / Help with sprite movement/collision logic
« on: March 20, 2011, 11:04:41 am »
I'm having a tough time with my control sprite (ship) occasionally getting stuck on objects it should bounce off of. Now I've completely lost focus and can't see the wood for the trees!

The ship's Y position is fixed and objects in the playfield scroll vertically up and down creating Y movement. X movement is normal stuff.

I know that with the correct chain of events this problem shouldn't occur; I did this 20 years ago on a C64 but my code skills are very rusty.

Collision detection function is good, no problems there. Feel free to ask for that code snippet if it helps. Feel free to correct me anywhere else too!

Thanks,
Mark

Code: [Select]
///////////////////////////////////////////////////////////////////////////////////////////////////
// Control the ship sprite
///////////////////////////////////////////////////////////////////////////////////////////////////

bool collision = ship_collision(ship_X, ship_Y);

// Left & Right
// Neither left or right keys pressed
if (!App.GetInput().IsKeyDown(sf::Key::Left) && (!App.GetInput().IsKeyDown(sf::Key::Right)))
// Wait until speed has decreased to near-zero
if ((xspeed > -20) && (xspeed < 20))
{
xacceleration = 0; // Then cancel acceleration & speed
xspeed = 0;
}
else
// Not zero yet, so force it to slow down
if (xspeed >  0)
xacceleration = -xaccel_factor;
else
xacceleration =  xaccel_factor;


// LEFT
// Check if left key pressed (but not right)
if (App.GetInput().IsKeyDown(sf::Key::Left) && (!App.GetInput().IsKeyDown(sf::Key::Right)))
// Need to set acceleration to LEFT but,
// before changing check we aren't over the side wall
{
if (ship_X >= 20)
// All clear, go left
xacceleration = -xaccel_factor;
else
//bounce off wall
xacceleration = xaccel_factor;
}

// RIGHT
// Check if right key pressed (but not left)
if (App.GetInput().IsKeyDown(sf::Key::Right) && (!App.GetInput().IsKeyDown(sf::Key::Left)))
// Need to set acceleration to RIGHT but,
// before changing check we aren't over the side wall
if (ship_X <= 770)
xacceleration = xaccel_factor;
else
xacceleration = -xaccel_factor;



// Check if ship has reached the side wall
if ((ship_X < 20) || (ship_X > 770))
{
// Invert xspeed
xspeed=-xspeed;
wobble_screen=true;
}


// UPDATE
// Add acceleration to current speed
xspeed += xacceleration;

// Limit speed
if (xspeed > xmaxspeed) xspeed = xmaxspeed;
else
if (xspeed < -xmaxspeed) xspeed = -xmaxspeed;


if (collision == true)
// Invert xspeed
xspeed=-xspeed;




// Up & Down
// Neither up or down keys pressed
if (!App.GetInput().IsKeyDown(sf::Key::Up) && (!App.GetInput().IsKeyDown(sf::Key::Down)))
// Wait until speed has decreased to near-zero
if ((yspeed > -20) && (yspeed < 20))
{
yacceleration = 0; // Then cancel acceleration & speed
yspeed =0;
}
else
// Not zero yet, so force it to slow down only by a smaller factor so it drifts longer
if (yspeed >  0)
yacceleration = -yaccel_factor +25;
else
yacceleration = yaccel_factor -25;


// UP
// Check if up key pressed (but not down)
if (App.GetInput().IsKeyDown(sf::Key::Up) && (!App.GetInput().IsKeyDown(sf::Key::Down)))
// Set acceleration UP
yacceleration = -yaccel_factor;

// DOWN
// Check if down key pressed (but not up)
if (App.GetInput().IsKeyDown(sf::Key::Down) && (!App.GetInput().IsKeyDown(sf::Key::Up)))
// Set acceleration DOWN
yacceleration =  yaccel_factor;


// UPDATE
// Add acceleration to current speed
yspeed += yacceleration;

// Limit speed
if (yspeed > ymaxspeed) yspeed = ymaxspeed;
else
if (yspeed < -ymaxspeed) yspeed = -ymaxspeed;


if (collision == true)
// Invert yspeed
yspeed=-yspeed;



// Update ship position
ship_X += xspeed  * ElapsedTime;
screen_pos += yspeed  * ElapsedTime;

ship_Y = screen_pos + 300;

Ship.SetPosition(ship_X, 300);

18
Graphics / Newb Sprite question
« on: March 15, 2011, 03:48:58 pm »
First post here. Great forum, I have learned so much from the many posts.

I'm a complete newbie to PC coding, C++ and SFML, but loving it so far!

If I am executing a loop like this, am I generating copies like the Sprite tutorial warns of?

Ignore the Scale/Center stuff, they're in there for testing.



Code: [Select]
   sf::Image Ball;
    if (!Ball.LoadFromFile("ball.png"))
        return EXIT_FAILURE;

---------------------------------------------


for (i=0; i<20; i++)
{
sf::Sprite Test(Ball);
Test.SetColor(sf::Color(55, 55, 55, 255));
Test.SetPosition(TestX[i], TestY[i]);
Test.SetScale(.3f, .3f);
Test.SetCenter(28, 21);
App.Draw(Test);

TestY[i] += Speed / 6;

if (Speed > 0 && (TestY[i] > 625)) TestY[i]-=640;
else
if (Speed < 0 && (TestY[i] < -30)) TestY[i]+=640;
}

Pages: 1 [2]