endless11111

Member

25

151 Neutral

• Rank
Member
1. Collision detection problem

I think the reason why the player is going halfway into the rectangle before being pushed out is because of when you handle the collision detection. Let's try stepping through the code one at a time, lets say you start to the left of the rectangle and you press and hold the right key.   At the beginning of the code you gave us you do the collision handling. Because you're not intersecting with the rectangle right now, the collision handling section is skipped because there is no collision.   Next you have your keystates. The game detects that you are holding the right key so it moves your player to the right, then it draws the player in this position. Now this is important to note that the player is drawn right after he moved, meaning the collision detection has not happened yet.   Next you go back to the beginning of the loop and here you run the collision handling because, this time, your player is inside the rectangle. After the collision handling is finished the player is no longer in the rectangle.   But once you reach the input handling you move the player back inside the rectangle and then when you draw the player, he is drawn inside the rectangle because you won't handle collision until the next frame.   If you let go of the right key, then the collision handling will occur like always, but this time the player won't move because you are no longer handling any input. The player is drawn outside the rectangle because you haven't moved him.   So in short the player is drawn right after he moves, before the collision handling can occur so he is drawn inside the rectangle and his position isn't corrected until the next frame. But even if his position is corrected the next frame, if you are still holding down the right key he moves into the rectangle again so in the end you're still in the rectangle.   if you restructure your code so the collision handling occurs after the player actually moves then your problem with the player going inside the rectangle should be fixed.       As for the problem with things moving too fast you'll have to find a way to deal with it yourself. For the most part you can just limit the speed of everything so they can't go through objects, but if thats not possible there are other methods. Since you're using axis-aligned rectangles (non rotated rectangles) you could check out http://www.gamedev.net/page/resources/_/technical/game-programming/swept-aabb-collision-detection-and-response-r3084 to learn about swept AABB collision detection. This is basically where you predict where the player traveled during the frame and check if the course he traveled through intersects with any objects and handle collision accordingly. I think that limiting the speed of the objects are good enough if you're just starting out with game development, but if you want you can always implement more advance techniques if your game needs it.
2. Collision detection problem

When you're checking which side of the player is intersecting with the rectangle the first part will always evaluate to true because if the player is at the right side of the rectangle then  his position will always be greater than the rectangle's position. So when you check for collision it always executes the first half of your if statement. if (PlayerRectangle.Intersects(BoxRectangle)) { if (Position.X + 32 >= BoxRectangle.X ) //this part will always be true because if the player is at the right side of the rectangle //then his position is already greater than the box's position so this is the only part that executes Position.X = BoxRectangle.X - 32; else if (Position.X <= BoxRectangle.X + BoxRectangle.Width) //this part won't execute because the first statement is always true if there is an intersection. Position.X = BoxRectangle.X + BoxRectangle.Width; }   Alternatively if you switch the two statements and run the second test first then the collision will only work from the right side of the box. If there is ever a collision between the box and the player then both of those statements will always be true, whichever one is checked first is the one that's executed. You checked the collision from the left side of the box first so only the left side is executed. when you try to collide from the right side of the rectangle you get pushed to the left.     Instead of doing the above, an easy way to decide where to move the player is to calculate the smallest distance required to move the player in order for the two rectangles to stop intersecting.   I don't know XNA so i don't know if the functions abs and min are available to you. //calculate the smallest distance required to move the player in order to escape collision //good to have named constants so you know what the numbers refer to const int playerWidth = 32; //distance from the right edge of the player to the left edge of the rectangle float rightEdgeDistance = BoxRectangle.x - (Position.x + playerWidth); //distance from the left edge of the player to the right edge of the rectangle float leftEdgeDistance = BoxRectangle.x + BoxRectangle.Width - Position.x; //figure out the smaller distance, use the absolute values of both calculations because //the distance might be negative due to the calculations float smallerDistance = min(abs(rightEdgeDistance), abs(leftEdgeDistance)); //move the player towards the edge of the rectangle that requires the smallest distance to move if(smallerDistance == abs(leftEdgeDistance)) { //distance from right edge of rectangle to the left edge of player is the smallest Position.X = BoxRectangle.X + BoxRectangle.Width; } else if(smallerDistance == abs(rightEdgeDistance)) { //distance from left edge of rectangle to right edge of player is smallest Position.X = BoxRectangle.X - playerWidth; } This usually works if your character is moving in small increments that way when you collide with something only a small portion of the player is actually intersecting with the other object. if your character moves too fast then he risks teleporting to the other side of the rectangle.
3. 2D Platformer collision handling...

I handle collisions pretty easily, first i calculate the distance it traveled in the x axis only, then i check if it collides with anything not passable, like a block. if i collides with anything, i reduce the distance it traveled in this frame and move it back to its previous position, and then i do the same for the y axis.   so for psuedocode it would look like this, actually its closer to c++ code than psudocode, but it's pretty self explanatory:    //so add the distance traveled this frame, distance = velocity * time //save the distance traveled so i can subtract it later and move back to previous position in case of collision float distance_Traveled = x_Velocity * delta_Time;   x_Position += distance_Traveled;   if(check for collision with any entity) {      x_Position -= distance_Traveled; }   then i just repeat that for the y axis, and the object will move to the last position it existed on before it had a collision. note there are some problems, for example if there is a collision before and after it moves, like the bounding box is located inside a block before it moves, and inside a block after it moves, it will move back to its previous position even if its still colliding with something. another problem is if it travels too far within a single frame, and crosses a huge gap at once and collides with something, it will move back to its original position even if the distance is enormous.   lets say the square bracket is the player, and the 0 is a wall []                    0 if its that far, and in one frame the square bracket moves inside the 0                       [(]) then it wil move all the way back to its last position []                      0 and not take into account the entire gap in between.   another problem has more to do with collision detection than handling, if an entity moves from one side of a block to another in a single frame without touching it, it wont count as a collision and he will pass through the block.   ex:   frame 1: []          0   frame 2:              0     []   no collision, so it will keep moving just fine.   anyways this simple way to handle collision works fine until it starts lagging or objects move at high velocities. also if it looks like the player is floating over a block then just make the bounding boxes of the player / block smaller than the image, that way it looks a little bit better. and one last thing, if you move one axis at a time, handling slopes will be easy because you just handle slope on the x axis and you can easily use something like y=mx + b to determien the position.     here is a final guide that you can look over to help you understand how to implement platformers, i really suggest you read it, its great.    http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/ its on gamedev.net too but my computer lags too badly to search for it so i just used the first link i found. Good luck
4. How do you make an AI follow an A* path in a 2D platformer game

Alright thanks for the help guys.
5. How do you make an AI follow an A* path in a 2D platformer game

Thank you for the reply, but im still confused about how i would implement the search algorithm. First of all how would i make the search algorithm take into account gravity and jumping. also how would i make the algorithm go from one solid node to another and take into account the gaps in between and the distances for jumping. lastly would i implement a search algorithm in the AI itself or just a generic search algorithm that anything can use. thanks for the help so far and sorry i couldnt grasp how i would implement it.
6. How do you make an AI follow an A* path in a 2D platformer game

I am currently working on a 2D platformer game and i was hoping to have AI that could follow the player instead of mindlessly moving side to side in the map. I looked into astar pathfinding and i managed to make a simple pathfinding system for an overhead game. The thing i am having trouble with is getting the AI to follow the path in a 2D platformer. I can make simple AI follow paths on overhead games, but i have no idea how to do this in 2D. how do i make the pathfinding itself calculate gravity, jumpheight, pitfalls and all of the other 2D obstacles? also how would i make the AI even follow a path like this? i would like to make an AI like the one in this video  . if it makes a difference i use c++.
7. TTF Problem

no problem, i also had the same problem when i was first learning SDL(although im still learning).
8. TTF Problem

do you have the font file in the save directory as the project file? by font file i mean the actual lazy.ttf file, not the dll's. if the error is due to the program being unable to load the font file it means the compiler cant find the lazy.ttf file. just make sure the lazy.ttf file is in the same directory as your compiler's project file if you are running the program through the compiler. if you run it using the .exe file put the lazy.ttf file in the same directory as the .exe.
9. How do video games render different equipment on players?

how do games such as RPG games show the characters wearing different equipment? do they have a separate image for a character wearing all the different combinations of equipment or is the equipment just an overlay that's stuck on to the character and moves along with him?
10. how do i save levels in a basic platformer game using c++ and SDL?

Thanks for all the help so far, but i have two questions. Why do you use stringstream instead of atoi? also when you use getline() to read the y location, does it start reading from the space between the x and y location or does it start after the space?
11. how do i save levels in a basic platformer game using c++ and SDL?

i tried to do it the way you two described it. for now i use a class called blocks for each platform, and to save a level i save each block's location onto a txt file. i cant seem to upload my code for some reason, it always gets cut off. void save() { ofstream file("level.txt"); //write the location of each block onto a text file for(vector<blocks>::iterator size = blockbox.begin(); size != blockbox.end(); size++) { //while saving add the with of the blocks to make up for the constructor's width and height subtraction file << size->box.x + 8 << " " << size->box.y + 8<< "\n"; } //close the file file.close(); } void load() { ifstream file("level.txt"); string s; while(file >> s) { //convert to int and create the x location int x = atoi(s.c_str()); //move on to y location then move to the next line file.ignore(); file >> s; //convert to int and create the y location int y = atoi(s.c_str()); //move on to next line file.ignore(); //create the block blocks block1(x,y); blockbox.push_back(block1); } //close the file file.close(); }
12. how do i save levels in a basic platformer game using c++ and SDL?

I am fairly new at programming games, and so far i have programmed basic games like pong. right now i was working on a small platformer game. right now i use a basic level creator that i made to create a level on the fly, and it's really annoying to recreate a level every time i compile my game. could anyone please teach me how to save my levels and load them so i dont have to recreate a level over and over again?
13. Simplest place to implement multithreading in my game engine?

you should not implement it into your engine until you have a firm grasp of it in case you encounter numerous bugs which you wont know how to fix because you havent experienced them before. although i am not experienced in multithreading, i have read numerous times to split the threads as logic, rendering, file input or output, events, and audio. basically to start just create a simple program to test out multithreading and create just 2 threads splitting logic and rendering to get a basis of what multithreading can do, then work on making a harder project like a basic game, then try and implement it into your engine. or you could just try to split logic, rendering, input, and opening files into different threads in your engine right away, but thats not a good idea until you have the hang of it.
14. Code::Blocks & SDL

You should go to Lazy foos site, he has tutorials on how to set up and start sdl on code blocks and set up extensions. Here is how to set up sdl -- http://lazyfoo.net/SDL_tutorials/lesson01/windows/index.php and extensions, this is for SDL_image but it works for all the other extensions. -- http://lazyfoo.net/SDL_tutorials/lesson03/windows/index.php
15. Beginner's Problem with SDL in C++

the image might not be loading up because you don't have the image file in the same folder as the project file. if you are compiling a program, all the files accosiated with the program must be in the same folder as the .cpp files, and if you are running the .exe file, all the files accosiated with the program must be in the same folder as the .exe file. and i am not really familiar with text intput and output in sdl using cin and cout, but as far as i know, you can't use cin or cout like you would with a console application, you can't just output text or input text directly using cin and cout.