Skip to main content
GameDev.net gamedev.net
🔒 Locked

Repeating collision over objects

Started by rnw159 Jun 26, 2010 at 10:10 PM 5 replies 1k views
Original Post
rnw159
rnw159
I have a function within an object that is supposed to build a box that the player won't fall through. The function works great except that I can't repeat it and create more boxes due to the way the function is layed out as so:
[script]
if(jumper.x+30 > x1+Map.x && jumper.x < x2+Map.x && jumper.y+60 > y1+Map.y && jumper.y < y2+Map.y){col = 5;}else{col = 0;}
[/script]
as you can see I have used ELSE to change the collision to 0 when he isn't touching the box. This causes any other boxes made to be in collidable and my player just falls through.

please help me fix this problem, I have tried using ELSE IF to find all the times that he isn't touching the block, but that doesn't work. I have also tried only detecting collision of objects within 50 pixels of the player, but that doesn't work either. What is the industry standard way of making multiple solid objects?


collide[2].collisionBox(1000,350,1100,370);
^^^^^the function^^^^^^^
Quote: Original post by rnw159Screw game maker.There's your sig quote for the forums :D[/quote]
rnw159
rnw159
guess no one feels like helping today :(
Quote: Original post by rnw159Screw game maker.There's your sig quote for the forums :D[/quote]
rnw159
rnw159
yup.....no one want's to help..
Quote: Original post by rnw159Screw game maker.There's your sig quote for the forums :D[/quote]
rnw159
rnw159
I don't wan't to create a new post....could anyone offer some theory at the least?
Quote: Original post by rnw159Screw game maker.There's your sig quote for the forums :D[/quote]
DaveMS
DaveMS
What is col used for? If there is no collision, it is set to 0, but if there is a collision, why 5?


public class Rectangle{ public:int x;int y;int width;int height;Rectangle(int x, int y, int width, int height){   this->x = x;   this->y = y;   this->width = width;   this->height = height;}bool intersects(Rectangle rect){   rect1left = x;   rect1top = y;   rect1right = x + width;   rect1bottom = y + width;   rect2left = rect.x;   rect2top = rect.y;   rect2right = rect.x + rect.width;   rect2bottom = rect.y + rect.height;   return (rect1right > rect2left && rect1left < rect2right && rect1bottom > rect2top && rect1top < rect2bottom);}};public class Entity{  .....  .....  Rectangle hitBox;};int main(){   Entity player, monster, floor;   if( Collides(player, floor) )  {     // player is colliding with floor  }  if( Collides(player, monster) )  {     // player is colliding with monster  }   }bool Collides(Entity e1, Entity e2){    return ( e1.hitBox.intersects(e2.hitBox) );}

NineYearCycle
NineYearCycle
@rnw159

Sometimes there are people that can help and sometimes there aren't, leave you posts for at least a day though! Possibly more over the weekend as people will be out living their lives.

Also if you're going to ask a question but don't get a response it might be due to the question not being very good. For example I cannot work out what your example code is for, or how you interact with it. Are you testing all the boxes you have or just one? Try posting more code AND putting it within the correct tags (you can find them in the FAQ).

There isn't really an "industry standard" way of doing this but a naive approach would be to iterate through the objects that you can collide with, whenever you get a "hit" you add that objects ID to a list and keep going. Once you've tested and found all the collisions you go through you're list of object IDs you made and generate your "response", i.e. you bounce off, or you sum the vector of all the rebounds, or you move your object back outside of the collision box, perhap you change to flashing green and orange and drift through(!), it's up to you to decide depending on your situation.

Andy
"Ars longa, vita brevis, occasio praeceps, experimentum periculosum, iudicium difficile" "Life is short, [the] craft long, opportunity fleeting, experiment treacherous, judgement difficult."
rnw159
rnw159
Thanks, but I got it. Sorry I couldn't figure out what the code tags where.
Quote: Original post by rnw159Screw game maker.There's your sig quote for the forums :D[/quote]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.