Jump to content
  • Advertisement
Sign in to follow this  
thelegendofzaku

Collision detection hassles

This topic is 4561 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

It seems that I'm trying to juggle some defined collision rectangles, and my game is crashing as a result. What I wanna do is this: when my enemy hits me, I want to set the collision rectangle to cover my sprites torso and another to cover his fist, and when I hit him, I want to do the same (one rectange covering my guy's fist and another covering the enemy's torso). I'm not sure as to where to place the appropriate calls to set the collision rectangle. Should I do them in the my game canvas class, and if yes, where. Here's some pseudocode
public void enemyCollisions(){
   //should I set the collision rectangles before checking for collision.
   if (enemySprite.collidesWith(playerSprite, true){
      //energy drains against my player go here
   }
}

public void playerCollisions(){
   //should I change the collision rectangles here?
   if (playerSprite.collidesWith(enemySprite, true){
   //energy drains against my enemy
   }
}


Share this post


Link to post
Share on other sites
Advertisement
I like this idea, this "fisting" stuff :-)

Maybe it would be simpler to use pixelwise collision with some constraints.

eg. if the player collides while jumping and punching, but is above the bad guy, then it is not a hit

Share this post


Link to post
Share on other sites
After working on my code today, I managed to use pixel collisions to see if it did anything. It seems that I can't hit my enemy anymore since it's not detecting the collision pixels and when I tried to set a pixel for my standing frame, it was colliding even though my enemy is not near me at all. I'm confused in trying to use the collidesWith method for pixel collision. My enemy image is 468 x 56 with nine 52 x 56 frames and my player image is 572 x 56 pixels with 11 62 x 56 frames. What I want to do is set collision pixels for frames zero through nine for my player and frames zero through seven for my enemy. With these pixel calls, I want to set them as conditions within a method in my game canvas class so that they drain energy for my player and enemy. When setting constraints such as not hitting when I am above or below my enemy, do I use while loops or would i have to use boolean conditionals?

Share this post


Link to post
Share on other sites
I would use the following logic

switch(playerstate)
{
...
case NORMAL:
{
...
if(punch_key)
{
playerstate=PUNCH;
punch_phase=0;
punch_lastupdate=0;
}
...
break;
}
case PUNCH:
{
//need to update frame?
if(GetTime()-punch_lastupdate < 1.0/PUNCH_FPS)
{
break;
}

punch_updated=GetTime();
punch_phase++;
if(punch_phase>MAX_PUNCH_PHASE)
{
playerstate=NORMAL;
break;
}

//did it hit someone?
if(punch_phase==PUNCH_HIT_FRAME_NUMBER) //the phase in which we can hit him
{
if(player overlaps enemy)
if(player facing enemy)
if(player not much below/above enemy)

then enemy hit, drain energy
}
break;
}
...
}

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!