Whats the best way to say just put a square over that image and stop my player from moving into them

I started putting pink squares/rectangles over the item I want collision with, to get the x1, x2, y1, y2 co-ordinates but there are maybe around 25 squares and rectangles.

Do i just do it 1 by one or does it in an array (not done this before)

Also How do I overcome this

#SelectExpand

1if(Human.y <90&& Human.x <860)//Check to see if he has hit a bound 2{ 3 Human.y =90;//Stop him going past the up bound 4} 5if(Human.x <860&& Human.y <90) 6{ 7 Human.x =860;//Stop him going past the right bound 8//What happens is that if I approach from this side regardless of where I am at my player gets set to the above Human.y value. 9}

I know I need to somehow stop him moving in the x/y direction but I still need him to move in the other x/y direction

If you are deciding whether two OBJECTS are colliding

for each objectA in array of objects
for each objectB in array of objects
if(objectA != objectB)//we don't want to collide with ourself!// do bounding box collision

I don't recall the exact formula off the top of my head for bounding boxes, but the point here is you are doing an O(n^2) ("n squared") algorithm that means the number of operations is equal to the square of the number of objects in the array.

You only need more elegant solutions when number of objects become very high. (1000 or more.)

If you are deciding whether an OBJECT is colliding with a BITMAP

You can use pixel-perfect collision detection (each pixel is tested), or you can use what I'll call "finite point" method where you a certain number of points that are relative to the center x,y position. So if you had a guy that was twice as tall as he was wide, you would test say [x,y-10],[x,y+10],[x-5,y],[x+5,y]. The number of points and their positions can very. So you can use more for a bigger object, or even change their positions as the sprites change. It's almost as good as pixel perfect, much faster, and in some cases, can behave even better. (A single stray pixel won't cause problems, only the pixel locations you want.)

[edit] I think I'm going to learn HTML5/JS/whatever and then throw together some scripted examples so people can see/feel how they interact.

-----sig:“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

You're not using the BoundingBox function correctly. It should be used to check whether the player overlaps with something when a key is pressed by checking for the future (one step ahead) position of the player versus the future position of the game objects and walls. Also don't case the bounds in variables, use the player and game object's positions directly.

That global variable hasCollision is a bad idea, it's bound to get out of date. In stead, make the Collide function return an int for C89 or bool for C++ or C99, and then check that in your if.

Secondly, the reason your player is getting stuck is because Collision checks the current position of the objects. This means that once a collision happens the object will not be able to move anymore because it will always keep colliding since the overlap already happened, like bullet stuck in the wall. It's necessary to check the next position of the game objects by taking their speed into consideration.

Alternatively, you can leave the collision as it is now, but then, you have to move the player back out of collision whenever collision happens.

Yes, this looks better. What you are doing is actually not so bad at all. There are several ways to do collision handling. The main distinction between these ways is how objects are kept from getting stuck.

This can be done in general in two ways:

Check where the objects will be after moving and if they will collide, don't move them.

Check if the objects are colliding and were moving before. If thy collide and were moving, put the objects back at their previous position.

The way you are trying is the second correcting approach, which can work if done correctly. In such an approach you could give the player struct an extra old_x and old_y to keep track of the previous position and restore that on collision.

Thanks Beoran, long delay as I had stayed up all night trying to get my head around all this bounding box/collision stuff, and so many people write things which are slightly different or totally different to how I have it done and then I get confused .

I have tried to add in the old_x and old_y value which you mentioned although I still get stuck on the bounding box.

There is this i found which I think explains what you said about old_x and old_y

You can only do this after checking that Human1 is not colliding with anything at it's x and y position. Otherwise you're just storing the "stuck" coordinates in the oldx and oldy , which makes them useless.

I do like to know that code is correct and what the error is and it helps me figure it out, sometimes it just takes forever, but I have a somewhat working collision

Although (Sorry another issue)

My player now kinda bounces back and forth between pixels on collision and if I release the movement key while he is furthest away from the collision he can carry on moving as normal, but if i release the movement key when he is closest to collision he gets stuck.

What area of the code would/could causes this issue ?

I've tried to add/subtract numbers from various x/y values to compensate for this but haven't fixed it yet

EDIT, I kinda got the bouncing to stop, but it then stops me moving in the other axis, so if I collided --> i cant move up or down. Still working on it though :-)

EDIT AGAIN, I think I have solved it now, seems to be working fine.

EDIT AGAIN: Any idea how I go about adding around 20 different x, y, w, h values for Human2 (There not humans, just keeping it that way for this purpose) Mine seems to be a long long way

1class human{ 2 3public: //ignore this for now, it just means anyone can access the 4//following code from anywhere in the program. 5 6float x; 7float y; 8 9//default constructor, note no return name OR void. Takes no arguments. 10//This one is automatically made by the compiler (and empty) unless you 11//override it. It's ALWAYS called when you make an object. 12human() 13{ 14x =200;//all humans spawn here unless specified (see next) 15y =300; 16} 17 18//custom constructor, same as before but allows you to specify arguments. 19human(int x, int y) 20{ 21human::x = x;//human:: selects the HUMAN class variable "x", needed because we 22// also have a FUNCTION "x". The other solution is to just use different 23// names like _x and _y for all class member variables so you can always 24// tell the difference. 25 26human::y = y; 27} 28 29} 30 31int main() 32{ 33human new_human;//will be at 200,300 34human new_human2(350,350);//will be at 350,350 35 36human human_array[100];//CANNOT use a custom constructor here. How would you specify them? 37//Solutions: 38 39human *human_array[10];//array of ten pointers to UNASSIGNED humans. Need to create AND assign them. 40 41for(int i =0; i<10; i++) 42{ 43human next_human =new human(352, 250);//can call custom constructor! 44human_array[i]= next_human;//array position i now points to our next human. 45}//this creates ten humans all at 352, 250, but we could specify 46// anything else such as using rand() to randomly populate them. 47 48//CAVEAT. Anything that has a "new" MUST have a "delete." 49//Because they don't disappear when they go out of scope 50// like a local (non new) variable will. 51for(int i =0; i<10; i++) 52{ 53delete human_array[i]; 54} 55 56}

[edit] DOH, I confused your post with someone else, so I thought you were using C++.

You certainly seem to know what your talking about Edgar in the way you respond with your coding, although at times I don't understand your code especially for this newbie (Bearing in mind my C++/Allegro5 knowledge was none existent a month - 6 weeks ago and I only watched Mike Geigs tutorials and some of CodingMadeEasy).

If I do get what you mean, should it be like this for me ? I'm trying not to change too much as I need to be able to take this code and put it straight into my game's code.

Take a look at the highlighted lines (which have problems) and the comments I made

#SelectExpand

1typedefstruct Player { 2 Bounds* b2; 3}; 4 5// This is shadowed (has the same name as another variable) 6// and uninitialized (you never give it a value)

7Bounds* b2;

8 9Player p1;// our bounds is inside the player struct 10 11//... 12 13//Example piece of code to show how I call the function in main. 14elseif(ev.type == ALLEGRO_EVENT_TIMER){ 15 render =true; 16 17// This is unitialized. Accessing it will crash

18 TestBounds(*b2);

19 20// This is what you want 21 TestBounds(p1->b2);

Please note that this code still has problems. p1->b2 is still unitialized. You never allocate memory for the Bounds struct. What you want is probably a member variable for Bounds that isn't a pointer.

I think the problem I am having it that I don't fully understand the setup up between having a struct then calling information from it inside another struct and linking everything together and I only have 2 days left before I finish the game:-(

I read into this and 1 person fixed it they saidThey added:#using <system.drawing.dll>at the topHowever if I add this I get : Error:"#using" requires C++/CLI mode

That is not how you fix it. :/

The error has to do with Microsoft so helpfully defining a function named Rectangle which conflicts with naming the struct Rectangle as well. If you #define NOGDI before including windows headers then the Rectangle function won't be visible.

You should get comfortable working with both member access using the dot operator . and dereferencing pointers with the dereference operator ->. & means "the address of" an object, and is what a pointer stores. That's why I set pp to &p in the code above. It now holds the value of the address of the Player object p. And you can also dereference a pointer with *. *pointer means "the data pointed to by" pointer. And so to access the Player p through pp you would use *pp = Player object.