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

Author Topic: Sprites Collision- how to stop them from moving?  (Read 9856 times)

0 Members and 1 Guest are viewing this topic.

Despairy

  • Newbie
  • *
  • Posts: 14
    • View Profile
Sprites Collision- how to stop them from moving?
« on: January 14, 2012, 01:00:52 pm »
Hi, i have a couple of question about using sfml graphics :
first:
lets say i have a world of objects ( walls and characters )
how do i get the character to stop walking if it encounters a wall?

( but the character can move on different things like trees )

second : the world of objects is being handles using a matrix of [20][20] size
            how can i use the sfml for moving while corollated with the matrix?
            if i can do that i can just check the cells and answer  the first Q

would apperciate your help thanks!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Sprites Collision- how to stop them from moving?
« Reply #1 on: January 14, 2012, 01:08:30 pm »
1. You have to use some kind collision detection and response. A simple case just compares the object's and wall's position and prevents the object from walking under certain circumstances.

2. Can you formulate the question more precisely, what do you exactly want to achieve? And did you mean "correlated" instead of "corollated"?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Despairy

  • Newbie
  • *
  • Posts: 14
    • View Profile
Sprites Collision- how to stop them from moving?
« Reply #2 on: January 14, 2012, 01:16:33 pm »
Thanks for the reply.

1. if I have a game board of size 20*20 , wont I have to check that one
    character with ALL of the others with every movement?

2. until now I was holding the moveable characters in a moveables vector
    and drawable objects in drawable vector.
    What if I will hold the entire world in a matrix ( in addition to the vectors)
    will than the collision detector from (1) will be simpler to handle?[/b]

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Sprites Collision- how to stop them from moving?
« Reply #3 on: January 14, 2012, 05:36:29 pm »
In case you store the tiles/walls in a 1D container up to now: Yes, it could become easier if you used a 2D matrix (I would use std::vector or std::array instead of raw arrays). At least if the tiles are aligned on a grid. And the characters can be anywhere?

If so, you only need to check the walls in the direct environment of the characters. But for character-character collisions, you need to check every combination. Actually, you could reduce the amount of checks, but this complicates code -- so only do it if performance requires it.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Despairy

  • Newbie
  • *
  • Posts: 14
    • View Profile
Sprites Collision- how to stop them from moving?
« Reply #4 on: January 14, 2012, 08:09:33 pm »
Finally I went for 2d matrix holding the gameobjects.
now the next step is being checked instead of the whole drawable objects.

but now I have graphic issues.
since i saved all of the objects in a matrix i have a canMove() function
that checks if the next move is legit.
if so the sprite moves to that direction (32b) but it jumps to it instead of
smoothly going there.
i thought of using the elapsedtime option but i have no idea how to use it
so the matrix checks will keep happening while pressing.

well here is the code, hope you can help me figure out what to do ty!!!

   
Code: [Select]
[quote]
while(App.IsOpened())
{
while(App.GetEvent(event))// quit or switch players
{

if (event.Type == sf::Event::Closed)// close program
App.Close();
if (App.GetInput().IsKeyDown(sf::Key::Escape))
App.Close();
if ((event.Type == sf::Event::KeyPressed)&&
(event.Key.Code == sf::Key::P))// switch players
i++;
if (i==_moveables.end())
i=_moveables.begin();
}

if (App.GetInput().IsKeyDown(sf::Key::Left)) // move left
if ((**i).canMove(-1,0,_world))
(**i)._sprite.Move(-32,0);
if (App.GetInput().IsKeyDown(sf::Key::Right))
if ((**i).canMove(+1,0,_world))
(**i)._sprite.Move(+32,0);
if (App.GetInput().IsKeyDown(sf::Key::Up))
if ((**i).canMove(0,-1,_world))
(**i)._sprite.Move(0,-32);
if (App.GetInput().IsKeyDown(sf::Key::Down))
if ((**i).canMove(0,+1,_world))
(**i)._sprite.Move(0,+32);
App.Clear(); // clear window
drawStuff(App);// draw all drawable vector.
App.Display(); // display
[color=red]Sleep(24); // for not moving too fast[/color] // do i need this?
xMove=0;yMove=0;
[/quote]


sorry quote and code arent working O_O

Dper

  • Newbie
  • *
  • Posts: 4
    • View Profile
Sprites Collision- how to stop them from moving?
« Reply #5 on: January 15, 2012, 02:40:48 am »
From what I understand in your code you are changing location of your sprite by 32 at time that means it will jump, you would need some additional functionality to move your object from A->B with chosen speed.
Something like move sprite by 32px over 1 sec (32/sec) in chosen direction till your sprite is in position you want it to be

Despairy

  • Newbie
  • *
  • Posts: 14
    • View Profile
Sprites Collision- how to stop them from moving?
« Reply #6 on: January 16, 2012, 06:36:23 pm »
exactly ... how do i do that?
i want everytime the user press RIGHT BUTTON to move +32 px on the
x axis. move(+32,0) isnt good.
how else should i write this ?
thanks!

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Sprites Collision- how to stop them from moving?
« Reply #7 on: January 16, 2012, 11:01:01 pm »
You must logically stop all collision and input checks and focus on the animation until it is completed.

If you want, you can try and code something so that it doesn't stop colision checks and doesn't go always 32 pixes at each key press, but that would be harder.

Despairy

  • Newbie
  • *
  • Posts: 14
    • View Profile
Sprites Collision- how to stop them from moving?
« Reply #8 on: January 18, 2012, 07:19:41 am »
i gave up the collisions i just try to fix the smooth movement.

the problem is that i use a matrix to control the game.
so each movement ( +1 spot on matrix) should be 32 pxs exactly in the
game window.

from what i understand I HAVE to use elapsedTime in order to
get a smooth movement. but move (xx*elapsedTime,0) wont move
32 pxs... actually when i tried it the character moved around 2 pxs until
the matrix character got to the end of the matrix (instead of 640 pxs)

any ideas?
( in short : i need on getInput/getEvent left to move -32 pxs to the left)
( but SMOOTHHHHHHHHH)

thanks again

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Sprites Collision- how to stop them from moving?
« Reply #9 on: January 18, 2012, 08:30:18 pm »
Create a variable to store the remaining distance to move in X. On the Left key event, add -32 in it.

Outside the event handling, calculate (using the elapsed time) the amount of space you will move (considering the direction the remaining distance variable is - negative or positive), subtract it from the remaining distance and finally move. When the remaining distance gets on 0 or surpasses it, your movement is over.

There are many other ways of doing it, but this one should be enough for what you want.

Despairy

  • Newbie
  • *
  • Posts: 14
    • View Profile
Sprites Collision- how to stop them from moving?
« Reply #10 on: January 18, 2012, 09:40:32 pm »
thanks but thats one of the things i tried.
float remaining=32;
while (remaining>0)
{
     ...move(xx*elapsedtime,yy*elapsedtime)
     remaining-=abs(xx*elapsedtime,yy*elapsedtime)
}

something like that ... and it still moves like teleporting
instead of smooth moving...

i have one more idea to try hope it'll work...
though i will appreciate a few other ideas.

thanks! good day.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Sprites Collision- how to stop them from moving?
« Reply #11 on: January 19, 2012, 03:04:08 am »
No, you've done it all wrong. You are not supposed to create another loop. The animation will get going between different frames of the game.

The variable is supposed to begin with 0, and you add -32 to it on the Left key pressed event.

The actual moving will happen after the input handling, before the App.Clear(); line.

By the way, it would be better if you used the Key events for this (like what you did with the Escape key for closing), instead of using the App.GetInput(), so that it happens only once, and not every frame the key is pressed.

Despairy

  • Newbie
  • *
  • Posts: 14
    • View Profile
Sprites Collision- how to stop them from moving?
« Reply #12 on: January 20, 2012, 04:25:06 pm »
aiiiiiiiiight changed to the events like you said.
now its moving almost smoothly... just need to play with the configurations

thanks alot!
if anyone needs the code you can PM me