SFML community forums
General => SFML projects => Topic started by: ravenheart on November 19, 2008, 12:59:05 pm
-
so there are many fine projects out there
im still trying to complete mine but i still fail at implementing collision-control
has anybody a simple game with about 5 -20 objects that could collide?
and if so would somebody publish the code here?
for me it´s just to learn how a good project should be built...
thanks =)
-
what kind of game are you making?
you could look up something called double dispatch (http://www.google.com/search?hl=en&q=double+dispatch+c%2B%2B&btnG=Search) if you want to have different behavior depending on what 2 items collide... [/url]
-
Here's a simple function for collision on Sprites (untested).
This function should be a member of some sort of class specifically for managing sprites:
bool IsCollided(const sf::Sprite& First, const sf::Sprite& Second)
{
sf::Vector2f FirstPosition1 = First.GetPosition();
sf::Vector2f FirstSize = First.GetSize();
sf::Vector2f FirstPosition2 = sf::Vector2f(FirstPosition1.x + FirstSize.x, FirstPosition1.y + FirstSize.y);
sf::Vector2f SecondPosition1 = Second.GetPosition();
sf::Vector2f SecondSize = Second.GetSize();
sf::Vector2f SecondPosition2 = sf::Vector2f(SecondPosition1.x + SecondSize.x, SecondPosition1.y + SecondSize.y);
if (FirstPosition1.x > SecondPosition2.x) return false;
if (FirstPosition2.x < SecondPosition1.x) return false;
if (FirstPosition1.y > SecondPosition2.y) return false;
if (FirstPosition2.y < SecondPosition1.y) return false;
return true;
}
-
what im trying to do is a 2d shooter in aerial perspective
when mouse is moves the player rotates and if w is pressed he moves in direction of mouse, so far so good
now i want aliens to move around in my map
(the only ki is
- when they would collide in next step they check what the object is
+ is it a wall they change direction
+if its a missile they walk on and will be hit
+ if the player is less than x away they start to move to him =attack him)
now i just need a good function to know if there would be a collision next step, is currently any collision?
one more thing is i have all my objects in std vector of type object*
is this a goos solution or not?
well i have still some problems with the vector
id like to know how to solve my problems in an efficient way
(no pixel-exact collision management ...,
but axes collision should be in )
-
Well, I think that you should try something like this (psuedo-code):
if placeFree(Object1.X position + movementFactorX, Object1.Y position + movementFactorY)...
placeFree would certainly ask for a Rect and not for a Vector (point), but I think that you may get the idea, tho
let's say
bool PlaceFree(const sf::Rect& o_Rect)
{
if there is any object (INTERFACE NEEDED?) in the area of this Rect return false
else return true
}
Hope it was helpful