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

Author Topic: Would be fine if...  (Read 7417 times)

0 Members and 1 Guest are viewing this topic.

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Would be fine if...
« 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 =)

zarka

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
Would be fine if...
« Reply #1 on: November 19, 2008, 09:06:12 pm »
what kind of game are you making?

you could look up something called double dispatch if you want to have different behavior depending on what 2 items collide... [/url]
//Zzzarka

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Would be fine if...
« Reply #2 on: November 19, 2008, 09:50:16 pm »
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:
Code: [Select]
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;
}

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Would be fine if...
« Reply #3 on: November 19, 2008, 10:08:45 pm »
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 )

Leandro

  • Newbie
  • *
  • Posts: 8
    • MSN Messenger - leostera@hotmail.com
    • View Profile
    • http://www.elrincondelea.com.ar
Would be fine if...
« Reply #4 on: November 20, 2008, 01:23:40 am »
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

Code: [Select]
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
Leandro Ostera
My Blags
Who Am I