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

Author Topic: Help with Tile Based Animations  (Read 2742 times)

0 Members and 1 Guest are viewing this topic.

Dreken

  • Newbie
  • *
  • Posts: 9
    • View Profile
Help with Tile Based Animations
« on: September 08, 2012, 08:15:15 am »
I'm using SFML 2.0-rc, windows 7, and visual studio 2010.

My character is on a tile map. When I hit 'W', he goes up, but obviously he goes from the bottom tile to the top instantly with no animation.

Does anyone have any suggestions for animating his climb from the bottom tile to the top that doesn't interrupt the game being real time?

My tile map uses a char array, so I can't figure it out because I can't set values inbetween numbers (ie. 1, 1.2, 1.3, ect..) I can only do 1, 2, 3 which is jumpy.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Help with Tile Based Animations
« Reply #1 on: September 08, 2012, 03:14:16 pm »
Tiles should only be used for static (/static animated) objects, that's why one often uses the word tilemap.
Characters , enemies, etc (entities) are mostly defined through their own class (e.g. sprite).
Instead of integrating your entity into the map draw it on top of the map and thus you'll be able to use each and every pixel of the screen. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Dreken

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Help with Tile Based Animations
« Reply #2 on: September 08, 2012, 06:03:05 pm »
Okay, thanks. The reason I was drawing my character on the same array as the map (which limits his movements to the grid) was because that's how i set up the collision detection. (It checked if the new tile was something he couldn't go on, then put him back.

Do you have any advice on how to set up the collision detection if the character freely roams on the screen per pixel?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Help with Tile Based Animations
« Reply #3 on: September 08, 2012, 06:57:08 pm »
The logic and the drawing should be kept seperated, thus you won't need to deal with pixels in the collision procedure. Your original idea isn't that bad, you just have to apply it correctly.
That means you first update the sprite's/entity's position and then you check if the new position violates any collision 'rule'. The checking can be done with axis-aligned collision detection (aka AABB, google will tell quite a lot about this topic), i.e. you check the bounds of your entity against all the surrounding 'walls' (or for simplicity if the count is low, every single wall in the array). If they are defined as sf::Rect<T> or if you write a function to easily obtain their rect, you can simply call: entityRect.intersects(wallRect)
If the two rectangles intersect the function will return true, otherwise false.
If after the checks a collision has occured you can change the position to the maximal allowed position in that direction. ;)

I believe there are some examples/tutorials in the wiki section of SFML on GitHub for AABB. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Help with Tile Based Animations
« Reply #4 on: September 08, 2012, 07:55:10 pm »
Quote
Do you have any advice on how to set up the collision detection if the character freely roams on the screen per pixel?
Box2d will be efficient and have all the features you could dream of but (probably) hard to use at first.
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Help with Tile Based Animations
« Reply #5 on: September 08, 2012, 08:52:08 pm »
Box2d will be efficient and have all the features you could dream of but (probably) hard to use at first.
Without having ever written some simple collision algorithm I wouldn't directly suggest to use Box2D. Sure the library can help you (if you know how to integrated it) and you won't need to worry about all that stuff, but it's bad to have just about null background knowledge. So I'd first implement some nice and easy AABB and later if things should get more complex switch to something like Box2D, because everything has a good and a not so good side. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kaB00M

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Caffeware
    • Email
Re: Help with Tile Based Animations
« Reply #6 on: September 09, 2012, 04:12:46 am »
Try something like this:


const int TILE_WIDTH = 16; //for example
const int TILE_HEIGHT = 16; //for example

bool IsMoving()
{
    if (x != grid_x * TILE_WIDTH) return true; //in transition from one tile to the other
    if (y != grid_y * TILE_HEIGHT) return true; //in transition from one tile to the other
    return false;
}

if (IsMoving()) Animate();
 

Now you need two sets of variables for this to work, real coordinates and grid coordinates.



 

anything