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

Author Topic: Collision Detection and Animation confused  (Read 4579 times)

0 Members and 1 Guest are viewing this topic.

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision Detection and Animation confused
« on: November 16, 2010, 09:54:03 pm »
Hello, I just ended animation code and it has confused me because I can create mask and hide black background, but without mask it looks like that:



And if I would like to check collision beetwen sprite and another sprite(i.e. rock) then it probably will be wrong because Collision will be alarmed when Rock touch Black area of my character, right?

How can I avoid it? I can't test it because I don't have any collision detections, yet but I don't want to get in troubles later.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Re: Collision Detection and Animation confused
« Reply #1 on: November 16, 2010, 10:38:07 pm »
There's different collision detection techniques out there. The simplest would be comparing two boxes against each other like you are suggesting.

Anyway here's a tutorial on one technique: http://lazyfoo.net/SDL_tutorials/lesson18/index.php

He uses SDL but the algorithm is the same for SFML.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Canadadry

  • Hero Member
  • *****
  • Posts: 1081
    • View Profile
Collision Detection and Animation confused
« Reply #2 on: November 17, 2010, 10:04:02 am »
Go see the wiki, there is code which does what's you want, just read it and learn.

Basically, you just the boxes like you said, the you test piel by pixel if a non background pixel of one sprite collide with a non background pixel of the other pixel.

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision Detection and Animation confused
« Reply #3 on: November 17, 2010, 02:38:25 pm »
Mean this collision detection right?
http://www.sfml-dev.org/wiki/en/sources/simple_collision_detection
And basiclly I will have to use:
Collision::PixelPerfectTest(const sf::Sprite& Object1, const sf::Sprite& Object2, sf::Uint8 AlphaLimit)

And set AlphaLimit to right value so it will test only if actual part of masked sprite overlapped with other sprite?

And what about sf::Vector2f? I did'nt see that type of data in C version of SFML.

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision Detection and Animation confused
« Reply #4 on: November 18, 2010, 04:54:46 pm »
Still waiting for answer + I have a problem(I guess, nothing new right?).

When I want to move my character I can do it in normal way I mean if I press D button I want to character to move in right position and to play right move animation.

In my game You will move only if You HOLD Key so I did something like that:
Code: [Select]

Wejscie = sfRenderWindow_GetInput(App);
sfBool Prawo = sfInput_IsKeyDown(Wejscie, sfKeyD);
sfBool Lewo = sfInput_IsKeyDown(Wejscie, sfKeyA);
sfBool Gora = sfInput_IsKeyDown(Wejscie, sfKeyW);
sfBool Dol = sfInput_IsKeyDown(Wejscie, sfKeyS);
float Offset = sfRenderWindow_GetFrameTime(App) * 50.f;
float OffsetX = 0.f;
    float OffsetY = 0.f;
         /* Process events */
         while (sfRenderWindow_GetEvent(App, &Event))
         {
             /* Close window : exit */
             if (Event.Type == sfEvtClosed)
                 sfRenderWindow_Close(App);
//if (Event.Type == sfEvtKeyPressed && Event.Key.Code == sfKeyD)
if (Prawo == sfTrue)
{
OffsetX += Offset;
sfSprite_Move(wsk->duszek, OffsetX, 0);
PlayUstalone(4,8,wsk);
sfClock_Reset(zegar);
}
if (Lewo == sfTrue)
{
OffsetX = -Offset;
sfSprite_Move(wsk->duszek, OffsetX, 0);
PlayUstalone(12,16,wsk);
sfClock_Reset(zegar);
}
if (Gora == sfTrue)
{
OffsetY = -Offset;
sfSprite_Move(wsk->duszek, 0, OffsetY);
PlayUstalone(0,4,wsk);
sfClock_Reset(zegar);
}
if (Dol == sfTrue)
{
OffsetY += Offset;
sfSprite_Move(wsk->duszek, 0, OffsetY);
PlayUstalone(8,12,wsk);
sfClock_Reset(zegar);
}
         }
czas = sfClock_GetTime(zegar);
if (czas > 2.1f)
{
Stop(wsk);
}

But when I hold key it doesn't animat itself but it animate itself when I realease key. I want to animate all the time when i HOLD key, Can someone tell me how to do this?


Could someone tell me how do You manage moving?

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision Detection and Animation confused
« Reply #5 on: November 19, 2010, 05:27:20 pm »
Okay, I discovered some things like when I put PlayUstalone(value,value) right in main before all loops and then I put moving code inside Game loop I can animate my chaacter and also move him but he animate itself all the time. even when I don't press any button.
When I discovered it I tried all different types of loops to keep animate it when key is pressed but noone of them worked. If anyone could give em some kind of tip, I would really appreciate it.

Also still waiting for answer to this question:

And what about sf::Vector2f? I did'nt see that type of data in C version of SFML.

Thanks in advance.

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision Detection and Animation confused
« Reply #6 on: November 20, 2010, 06:19:38 pm »
EDIT: Finally I fixed it.

Still please answer this question:

And what about sf::Vector2f? I did'nt see that type of data in C version of SFML.

finaiized

  • Newbie
  • *
  • Posts: 4
    • View Profile
Collision Detection and Animation confused
« Reply #7 on: November 30, 2010, 06:17:26 am »
Quote from: "darekg11"
EDIT: Finally I fixed it.

Still please answer this question:

And what about sf::Vector2f? I did'nt see that type of data in C version of SFML.


I don't have the C version of SFML, but it definitely isn't found under sf::Vector2f. C doesn't have support classes. You could try simply Vector2f but you are probably going to have to include a header or two. Why specifically do you need to use C/what do you need the vector2f for? It wouldn't be hard to make a vector2f yourself (a struct with x and y would suffice).

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision Detection and Animation confused
« Reply #8 on: November 30, 2010, 03:02:57 pm »
Yeah but well I think i will write my own collision detection system to improve my skills and to learn something.