Jump to content
  • Advertisement
Sign in to follow this  
ajm113

Triggering Actions from Physics Engine

This topic is 2139 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!