• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
pharaoh357

Problem animating sprite in SFML 2

1 post in this topic

Hello guys I am new to game programming, I never programmed a game in my life, I am starting small with C++ and sfml 2 creating a pong game, but, I ran into an issue, I was trying to deform the ball when it hits the borders, but I am failing to get the result I want, the animation is no seamless and don't know how to correct It. Can someone help.

Here is the code (you can get it too at : [url="https://bitbucket.org/ed3lgon/pong-pong/src"]https://bitbucket.or...n/pong-pong/src[/url])

header file

[source lang="cpp"]/*
* FILE : PongBall.hxx
* AUTHOR : ed3lgon
* CREATED : Oct 21, 2012
* MODIFIED : Undefined
* PURPOSE : Undefined
* DESCRIPTION : Undefined
*/

#ifndef PONGBALL_HXX_
#define PONGBALL_HXX_

#include <SFML/Graphics.hpp>

using namespace sf;

class PongBall
{
public:
PongBall(RenderWindow& pGameDisplay);
~PongBall();
void move(const Time& pElapsedTime);
void draw();
const Sprite& getSprite();

private:
static const float BALL_SPEED = 120.0f;
static const float BALL_DIAMETER = 50.0f;
int mImageIndex;
// reference to game window
RenderWindow& mGameDisplay;
Texture mBallSpriteSheet;
Sprite mBallSprite;
// will hold coordinates to images in the texture
IntRect mTextureRect[9];
enum Direction
{
NEGATIVE_X,
NEGATIVE_Y,
NEGATIVE_DIAGONAL1,
NEGATIVE_DIAGONAL_2,
POSITIVE_X,
POSITIVE_Y,
POSITIVE_DIAGONAL1,
POSITIVE_DIAGONAL2,
NONE,
};

enum AnimationStatus
{
START_ANIMATE,
END_ANIMATE,
DEFAULT_ANIMATE,
};
// indicates the direction the ball is facing, i.e. It is moving to.
Direction mDirection;
AnimationStatus mAnimtionStatus;
};

#endif /* PONGBALL_HXX_ */
[/source]

here is the implementation:
[source lang="cpp"]/*
* FILE : PongBall.cxx
* AUTHOR : ed3lgon
* CREATED : Oct 21, 2012
* MODIFIED : Undefined
* PURPOSE : Undefined
* DESCRIPTION : Undefined
*/

#include "PongBall.hxx"

#include <iostream>

PongBall::PongBall(RenderWindow& pGameDisplay) :
mImageIndex(0), mGameDisplay(pGameDisplay), mDirection(POSITIVE_X),
mAnimtionStatus(DEFAULT_ANIMATE)
{
// initialize horizontal images coordinates.
// this one is the default image, i.e. with no deformation.
mTextureRect[0].left = 0;
mTextureRect[0].top = 0;
mTextureRect[0].width = 51;
mTextureRect[0].height = 51;
// ball with minimum horizontal deformation.
mTextureRect[1].left = 51;
mTextureRect[1].top = 0;
mTextureRect[1].width = 49;
mTextureRect[1].height = 51;
// ball with medium horizontal deformation.
mTextureRect[2].left = 99;
mTextureRect[2].top = 0;
mTextureRect[2].width = 48;
mTextureRect[2].height = 51;
// the second most deformed ball.
mTextureRect[3].left = 147;
mTextureRect[3].top = 0;
mTextureRect[3].width = 46;
mTextureRect[3].height = 51;
// ball with max horizontal deformation.
mTextureRect[3].left = 197;
mTextureRect[3].top = 0;
mTextureRect[3].width = 42;
mTextureRect[3].height = 51;


// initialize vertical images coordinates.
// minimum vertical deformation.
mTextureRect[4].left = 0;
mTextureRect[4].top = 51;
mTextureRect[4].width = 51;
mTextureRect[4].height = 48;
// medium vertical deformation.
mTextureRect[5].left = 51;
mTextureRect[5].top = 51;
mTextureRect[5].width = 51;
mTextureRect[5].height = 47;
// maximum vertical deformation.
mTextureRect[6].left = 102;
mTextureRect[6].top = 51;
mTextureRect[6].width = 52;
mTextureRect[6].height = 45;
// maximum vertical deformation.
mTextureRect[6].left = 154;
mTextureRect[6].top = 51;
mTextureRect[6].width = 51;
mTextureRect[6].height = 43;


// load ball texture and set sprite texture.
mBallSpriteSheet.loadFromFile("res/gfx/ball_sheet.png");
mBallSprite.setTexture(mBallSpriteSheet);
mBallSprite.setTextureRect(mTextureRect[mImageIndex]);
// position ball at (100, 100)
mBallSprite.setPosition(500.0f, 300.0f);
}

PongBall::~PongBall()
{

}

void PongBall::move(const Time& pElapsedTime)
{
// get ball position on screen.
Vector2f ballPosition = mBallSprite.getPosition();
const float amount = BALL_SPEED * pElapsedTime.asSeconds();


// move the ball acording to the position It is facing
switch (mDirection)
{
// here I decrement ball's X cordinate and check if it has it the border.
case NEGATIVE_X:
ballPosition.x -= amount;
// if ball has touched the border invert direction
if (ballPosition.x < 1)
{
ballPosition.x = 0;

switch (mAnimtionStatus)
{
case START_ANIMATE:
++mImageIndex;
if (mImageIndex == 5)
{
mAnimtionStatus = END_ANIMATE;
}

break;

case END_ANIMATE:
--mImageIndex;
if (mImageIndex == 0)
{
mDirection = POSITIVE_X;
mAnimtionStatus = DEFAULT_ANIMATE;
}
break;

case DEFAULT_ANIMATE:
mAnimtionStatus = START_ANIMATE;
break;

default:
break;
}
}
break;

case NEGATIVE_Y:
ballPosition.y -= amount;
if (ballPosition.y < 0)
{
ballPosition.y += amount;
mDirection = POSITIVE_Y;
}
break;

case NEGATIVE_DIAGONAL1:

break;

case NEGATIVE_DIAGONAL_2:

break;

case POSITIVE_X:
ballPosition.x += amount;
// if ball has touched the right border
if (ballPosition.x > (1024 - BALL_DIAMETER))
{
ballPosition.x -= amount;
switch (mAnimtionStatus)
{
case START_ANIMATE:
mImageIndex++;
if (mImageIndex == 5)
{
mAnimtionStatus = END_ANIMATE;
}

break;

case END_ANIMATE:
mImageIndex--;
if (mImageIndex == 0)
{
mDirection = NEGATIVE_X;
mAnimtionStatus = DEFAULT_ANIMATE;
}
break;

case DEFAULT_ANIMATE:
mAnimtionStatus = START_ANIMATE;
break;

default:
break;
}
}
break;

case POSITIVE_Y:
ballPosition.y += amount;
if (ballPosition.y > (600 - BALL_DIAMETER))
{
ballPosition.y -= amount;
mDirection = NEGATIVE_Y;
}
break;

case POSITIVE_DIAGONAL1:

break;

case POSITIVE_DIAGONAL2:

break;

default:
break;
}

// update sprite rect.
mBallSprite.setTextureRect(mTextureRect[mImageIndex]);
// update sprite position
mBallSprite.setPosition(ballPosition);
}

void PongBall::draw()
{
mGameDisplay.draw(mBallSprite);
}
[/source]
0

Share this post


Link to post
Share on other sites
You're controlling the speed of the ball, but you aren't controlling the speed of the animation. In the same way that you calculate "amount" in PongBall::move, you need to calculate what frame of the animation you should be on based upon what fps you would like your animation to play at. Only increment mImageIndex when you've accumulated enough time for a frame.

Something like this, following the pattern of your code:

[CODE]

mAnimationAccum += ANIMATION_SPEED * pElapsedTime.AsSeconds();

if(mAnimationAccum >= 1){
mAnimationAccum = 0;
++mImageIndex;
}
[/CODE]

Say ANIMATION_SPEED is 30, for 30 fps. Then everytime mAnimationAccum == 1, 1/30 of a second has passed, so we increment the frame we're on and reset the accumulator. You'll probably want ANIMATION_SPEED to be 5-10 fps, but I suppose it depends on how fast the ball is moving on screen.
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0