Jump to content

  • Log In with Google      Sign In   
  • Create Account

Triggering Actions from Physics Engine


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 ajm113   Members   -  Reputation: 307

Like
0Likes
Like

Posted 07 September 2013 - 09:55 PM

Hello, I'm wondering whats the best way to handle "events" from the physics engine? I'm writing a 2D top-view sci-fi 2D shooter where enemies just run into the screen and you shoot basically. Nothing ground breaking...

 

Anyways, say if I move the player into an 'basic' enemy, I want the player to loose 2 points of health a second if colliding. But if it's the boss, loss 20 points a second. Or if the player takes a projectile hit... How many points of health should the player used based on the weapon.

 

Here is what I have so far as of a event system. I was wondering if there was a better way of doing it so I can keep reusable code for future projects.

 

Holds collision data for object...

class CollideableObject
{
   public:
   CollideableObject(double x, double y, double radius)
   {
    m_x = x;
    m_y = y;
    m_lastx = x;
    m_lasty = y;
    m_radius = radius;


   if(m_radius <= 0.0)
   {
      m_radius = 0.01;
   }
}

   CollideableObject()
   {
      m_x = 0;
      m_y = 0;
      m_lastx = 0;
      m_lasty = 0;
      m_radius = 0.01;
    }

double m_radius;
double m_x;
double m_y;


double m_lastx;
double m_lasty;
//Team ID
short rank;


protected:
private:

};

Physics Handler where we handle collision detection and output events.

 

struct eventMessage
{
size_t selRigBodyId;
size_t targetRigBodyId;
};


class PhysicsEngine
{
public:
PhysicsEngine(int sizeX, int sizeY);
~PhysicsEngine(void);


size_t   addObject(Vector2D pos, double radius);
bool     removeObject(size_t index);

Vector2D getObjectPos(size_t index);
bool     setObjectPos(size_t index, Vector2D pos);

bool     testPointCollision(Vector2D pos, double radius, size_t &index);
bool     testPointCollision(short rankOnly, Vector2D pos, double radius, size_t &index);

//Calculate collsion detections and respond with whos colliding into who.
std::vector<eventMessage> step(float fStep);


      void clear(void);

protected:
private:


Projectile* projectileHandler;
std::vector<CollideableObject*> objects;
QuadTree* entireScreen;

double TOUCH_DISTANCE;

};

 

The line "std::vector<eventMessage> step(float fStep);" outputs ID's objects that are colliding.

 

selRigBodyId = The object we are checking...

targetRigBodyId = The object that is being collided into.

 

I don't know if there are better ways of doing this then remaking a for loop in my code to handle the collisions. Kinda seems like poor practice to me the way I'm doing it, but then again, any of your answers are going to be better then mine.


Edited by ajm113, 07 September 2013 - 09:59 PM.

Check out my open source code projects/libraries! My Homepage You may learn something.

Sponsor:

#2 osiris_dev   Members   -  Reputation: 275

Like
0Likes
Like

Posted 08 September 2013 - 09:44 AM

I think you need to separate terms physics event and game. Physics event depends on physical library, it is callback function usual and you are getting just info about 2 bodies in collision contact, any other information (like velocity or positions) you can get in any other place throw physical system for objects. It is not necessary to put all contact information in your event class object.

 

The same thing about game events. You can't contain all information about all game events in one event object. Better way if you'll make event + task system, where multi task objects starts or changes behavior when his event/s happened, and inside event contains only common information about event, all other information is inside the task.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS