As many people before me, I'd like to thank you for this series. Your explanations are detailed, but easy enough for even a beginner to understand. I also appreciate that you tried to show us very clean, reusable code. (As opposed to less professional "as long as it compiles, it's good enough" tutorials.) Your series may (so far) only teach the basics, but they are good, solid basics on which we can build further learning. Your personality is a big + too. The viewer can feel that you're a good person who enjoys sharing his knowledge. That's important too when you teach beginners, since a beginner may need to feel some comforting human warmth in this unknown SFML territory. You got a well deserved donation from me. Thank you, sir. :)
I have a problem with my collision where it doesnt accept player before .GetCollider(). This happens at the very end between player.Update and view.setCenter. If you know what my problem could be please tell me.
Instead of push you could say inertia. Inertia is defined as how resistant an object is to any force. An object having a higher inertia would “push back” harder
As of 2.5.1 the coordinates systems is a bit different so if the colliders go crazy after touching, try inverting the ">" on the if statements in the checkCollision method I think its because the Y axis becomes positive when tou go down and negative if you go up in the lasts updates. Idk why i also had to invert the X axis tho, i think that one is still normal
Hey its me from earlier videos! Don't worry i didnt give up and am still plugging away! I actually feel proud i managed to make a little "game" where a character moves and shoots and learned quite a bit! now im trying to understand collision this is really tough and my next challenge. I am probably going to watch this video alot!
Hey, I was just having this same problem and I fixed it by removing the & in the CheckCollision parameters. So instead of CheckCollision(Collider &other, float push), it is CheckCollision(Collider other, float push). (make sure to modifyt the header as well)
It needs you to store the players collider before passing it by reference, so Collider col = player.GetCollider(); , then pass col in. Since the getcollider returns a Collider, not a Collider* or & so as soon as that finishes it goes out of scope, meaning the pass by reference will be referencing bad memory. It's usually more efficient to pass by const reference for somewhat complicated data structures (aka anything thats not a standard int or float etc) but if within the function that is taking it, wants to modify that type - it cannot be const.
Hello good sir! So far your tutorial videos have been fantastic. However, I encountered an error on the main.cpp. The player in platform1.GetCollider().CheckCollision(player.GetCollider(), 0.0f); platform2.GetCollider().CheckCollision(player.GetCollider(), 1.0f); gives an error: initial value of reference to non-const must be Ivalue. Do you know how I can fix it?
When I write player.getCollider() in the CheckCollision method it tells me this: initial value of reference to non-const must be an lvalue 49 cannot convert argument 1 from 'Collider' to 'Collider &' Can anyone tell me why?
I got this error too, I don't know how he didn't get this. The problem is that in the getCollider function you are returning a temporary Collider object and you have a reference to Collider in CheckCollision function, you are trying to bind a non-const reference to a temporary object which is not allowed. What I did was create an empty default constructor for Collider class, create private default Collider objects in the platform class and player classes, then in the getCollider function I set these objects to Collider(body) and return that value. Lastly, I have the type of getCollider be Collider& (reference to collider object) instead of Collider. Now when you use pass in player.getCollider() in the CheckCollision method you are actually passing a reference to a non-temporary Collider object. There might be a shorter way to bypass this issue but this is what worked for me.
And here I am at the end of the journey, eagerly waiting for your next video. As many others here, I have a question for you: I code on a laptop, and I've noticed that CPU usage goes up by a lot (from ~10% to ~70%) when I launch the application, so my machine starts heating up real quick. Apparently, this is due to the opening of the app window, and it's just in part VS's fault: if I open the .exe in the release folder it's still at least 40% extra work on the CPU. I wonder if that's normal and if there's a way to improve the performance on the system. Thanks
i`m trying to work your code out on my project , but somehow i cant collide in both ways , only on one way. if i do platform1.checkcollision with player it works well , but if i do player.checkcollision with platform1 it doesnt work . any clue on that? tried revising everything and it looks ok.
ATTENTION EVERYONE WHO USED A LIST AND A FUNCTION INSTEAD OF CLASSES if you had a list that you go throw with a for loop in a function to check for each individual tile then when you collide do break; or its gonna continue to loop after collision has been detected and set collision to false thus fucking it up but I recommend if you are gonna use a functions make it a bool copy the code then in the update set isColliding = collisionFunc(tiles) (or however you called them) and everything is gonna be fine
error: no matching function for call to 'Collider::CheckCollision(Collider, float)'| on platform1.GetCollider().CheckCollision(player.GetCollider(), 0.0f); same for platform2 What might be the reason? Anyone?
Hey.. For all who have the same problem, Here is the simply solution: ..... case sf::Event::Resize: ResizeView(window, view); break; } } Collider playerCollRect = player.GetCollider(); // (or) auto playerCollRect = player.GetCollider(); player.Update(deltaTime); platform1.GetCollider().checkCollision(playerCollRect, 0.5f); platform2.GetCollider().checkCollision(playerCollRect, 0.5f) ----------------------------------------------------- I hope i could help.. Nice Tutorials Hilze Vonck
@Mr.Lowpoly Hey thanks a lot man! Really needed this for my project. I also have a correction though: you spelt "CollRect" wrong on the platform1 and platform2 lines
can this 1 collision system be appliied to multiple sprites or game objects like enemies, pickups and walls as i thought this is some sort of collision parents for all objects. If so, how can i include it inside each one of them, means like do i have to declare different names of the collider for every different objects. Please let me know cuz im kinda new to sfml and also c++. T-T
in 14:33 i made that getCollision in my player.cpp file but it says that body is an undeclared identifier? I have even include the collision file in the h file of my player.
Hi! I am trying to apply this code into my project, changing it up a bit so it runs with sprites instead of a rectangleshapes, however, when it goes into the "GetPosition" function, it is tossing an Exception thrown at error, Access violation reading location.......not entirely sure what I am doing wrong and why returning a position is giving me this
Hey, I have a question. I need to know how can I make my character to pass through the, lets say right side of the window and appear on the left? Its really important for me.Ty
I have done exactly what is shown in the video but the platform is not moving. I tried writing platform1.GetCollider().Move(0.1f,0.0f); directly in my main file but still my platform isn't moving. Can anyone think of any mistake that I might be doing? I have already checked the video once again and haven't found any mistake
Hello, i realy like you tutorials. You realy explain things well. but it would be nice if you could put the files online (or code). I am running into some problems when i am colliding with the platforms. When i walk to the platforms, platform 1 is moving away(without colliding), and platform 2 isnt doing anything. except when walking upstairs, then it starts blinking Could someone help?
can You please send me the code of player.h where you returns the collider of body. because i got an error that cannot bind non-constant value reference of type 'Collider&' to a reverse Type Collider.
This has something to do with the vs2017 and vs2019 compiler. With the vs2015 compiler you were able to directly use a returned copy as a constant reference, but you are not allowed to do this anymore with the vs2017 and vs2019 compilers. To fix this you simply have to store the collider in a local variable first and then pass that collider into the function, or change the function so it takes a collider instead of a constant reference to a collider.
Here you go, this function will check if a collider touches the border of a view. bool TouchesViewBorder(Collider collider, sf::View view) { sf::Vector2f colliderHalfSize = collider.GetHalfSize(); sf::Vector2f colliderPosition = collider.GetPosition(); sf::Vector2f viewHalfSize = view.getSize() / 2.0f; sf::Vector2f viewPosition = view.getCenter(); float rdx = colliderPosition.x - viewPosition.x; float rdy = colliderPosition.y - viewPosition.y; float dx = abs(rdx) - (viewHalfSize.x - colliderHalfSize.x); float dy = abs(rdy) - (viewHalfSize.y - colliderHalfSize.y); if ((dx < colliderHalfSize.x * 2.0f && dx > 0.0f) || (dy < colliderHalfSize.y * 2.0f && dy > 0.0f)) { return true; } return false; } As you can see it looks almost exactly the same as the collision code for checking if two colliders are colliding. That is because a view also has a center position and a size. The only thing that really changed is the if statement at the end. This if statement consists out of two parts: (dx < colliderHalfSize.x * 2.0f && dx > 0.0f) and (dy < colliderHalfSize.y * 2.0f && dy > 0.0f). both of these parts basically do the same thing but one of them checks the collision on the x axis and the other one on the y axis. let's take one of these parts and see what it does. The first part (dx < colliderHalfSize.x * 2.0f) basically checks if the collider is within a certain range from the border (inwards), we need to multiply the halfSize by two because we need the full size for this calculation. The second part (dx > 0.0f) basically checks if the rectangle is within a certain range from the border (outwards). So the first part checks the collision for the left and top side of the collider and the second part checks the collision for the right and bottom part of the collider. The reason that they are separated by an or ( || ) is because we want this statement to be true whenever the collider is colliding with one of the sides of the view, and not just the corners.
Can you do a tutorial on gravity, so that when you jump you will fall down and so that standing on blocks matters otherwise you will fall through the screen.
I get an error "error: no matching function for call to 'Platform::Platform(std::nullptr_t, sf::Vector2f, sf::Vector2f)'" when creating the platforms. Help anyone please?
Hey i have an error C2664 "bool Collider :: CheckCollision (Collider &, float)": it is not possible to convert argument 1 from "Collider" to "Collider &"
Hey, the problem is that it is trying to get a reference to an object that isn't defined anywhere. The solution is to simply store the result of the getcollider function and use that variable as an input for the checkcollision function. This is an lvalue vs rvalue issue. vs2015 didn't see this as an issue, but since vs2017 the compiler started to treat this as an error. I hope this helps
@@HilzeVonck anyway i am new and i work at sfml like 1 hours can u tell me how can i use code to do it and where should i put it please. Still thanks a lot dude
would you happen to know of a way to write *(simple)* non OOP no structs nothing, just bare bones code. of aabb response of 2 regular rectangles. or point me in the right direction? i am desperate. ive spent hours and hours looking for a simple implementation that i can read, i just want to a solid square!!!!!! why is this so hard to find!!!!!!!!!!!!!!!!!. im sure its not ideal but its the only way i can understand it. so many tutorials. or rather always have the starting ccordinates as something crazy like "entity.x.whatever.idontcare = delta.whatever.why?"
You can use the same code as is in the final check collision method. Just also pass in the position for the first aabb as a reference the size of the first aabb, the position of the second aabb as a reference and the size of the second aabb. It shouldn't be hard to convert this to a simple, c like function.
@@HilzeVonck converting it is the problem I just wanna *(see)* an example so that I know how it works. Everything being in objects makes it way to hard to pick apart. Also I'm asking in every UA-cam video about aabb resolution.
@ It could look something like this. (keep in mind that I just wrote the code and did not test it, so there might be some small syntax errors) The functionality of the function stays the same, the only thing that I changed is that we now are passing the positions and half sizes in directly, and we are also modifying these positions directly. Just make sure that the position that you pass in is the center of the AABB, and the size is half of the total size of the AABB. bool CheckCollision(sf::Vector2f& thisPosition, sf::Vector2f thisHalfSize, sf::Vector2f& otherPosition, sf::Vector2f otherHalfSize, float push) { float deltaX = otherPosition.x - thisPosition.x; float deltaY = otherPosition.y - thisPosition.y; float intersectX = abs(deltaX) - (otherHalfSize.x + thisHalfSize.x); float intersectY = abs(deltaY) - (otherHalfSize.y + thisHalfSize.y);
@@Herovy070 wow to be honest this is way more than I could have asked for thank you I'll check it out when I get home. I appreciate it. I'm gonna go ahead and give you a like and a sub
I keep getting the error 'bool Collider::checkCollider(Collider &,float)': cannot convert argument 1 from 'Collider' to 'Collider &' At the very end, I've tried re watching the video multiple times but the code seems to be fine, anybody know what is going wrong here?
Okay the solution I found was to put this line before we check collisions between (platform1 and player) and (platform2 and player in main.cpp: 'Collision (name of your Collider member) = player.getCollider(); After that, just replace player.getCollider with (name of your Collider member) in both checkCollision calls that follow
As far as I can tell, the compiler just didn't want us to use a member function for this variable. If anyone has a better explanation, I would love to hear it.
Not a solution, but I did this tutorial a while back on VS community 2015, and the code worked exactly as is, I am redoing it now to get back into SFML with VS community 2017, and I get this exact same error. So, perhaps it is something weird in the newest iteration of the compiler. And your solution did work for me.
It's not compiling, and it's telling me: "initial value of reference to non-const must be an lvalue" and "'bool Collider::CheckCollision(Collider &, float)': cannot convert argument 1 from 'Collider' to 'Collider &'" :(
Hey, first, its a good tutorial, thank you. But could you pleaseupload your code to github or something else? I'm geting two errors in the code for this two lines: platform1.GetColider().CheckCollision(player.GetCollider(), 0.0f); platform2.GetColider().CheckCollision(player.GetCollider(), 1.0f); Invalid arguments ' Candidates are: bool CheckCollision(Collider &, float) ' Invalid arguments ' Candidates are: bool CheckCollision(Collider &, float) ' invalid initialization of non-const reference of type 'Collider&' from an rvalue of type 'Collider' invalid initialization of non-const reference of type 'Collider&' from an rvalue of type 'Collider' Can anybody help?
I have actually been working on rewriting all the code to upload to the site that I am building all day. All of the source code for this video should be up there. Here is the link: lucrifi.com/index.php/24-sfml-2-4-for-beginners-12-collision-detection-aabb?start=1. As a note though. The site is not yet finished at all, so if something is wrong or if something does not work, It'll be fixed as soon as possible. I think that the problem is either in the player's GetCollider() function or in the variables that you are passing into the CheckCollision() function. Make sure that the player's GetCollider() is NOT returning a reference to a collider. And make sure that the CheckCollision() DOES have references to the collider (both in the .h and the .cpp).
Thanks for the quick respons, I checked your link and checked your code with this page www.qualidator.com/WQM/de/Tools/TextDiff.aspx, I could not see any significant difference to my code, This is the play's GetCollider() Collider GetCollider() { return Collider(body);} and this is CheckCollision() in .h bool CheckCollision(Collider& other, float push); in .cpp bool Collider::CheckCollision(Collider & other, float push){
Hi,Thx for this great tutorial! I'm using code::block and got the same error even after copying all files from lucrifi.com/index.php/24-sfml-2-4-for-beginners-12-collision-detection-aabb?start=1. I've solved it by using bool CheckCollision(Collider , float).
platfom1.GetCollider().CheckCollision(player.GetCollider(), 0.0f); platfom2.GetCollider().CheckCollision(player.GetCollider(), 1.0f); player gives an error in my code?
As many people before me, I'd like to thank you for this series. Your explanations are detailed, but easy enough for even a beginner to understand. I also appreciate that you tried to show us very clean, reusable code. (As opposed to less professional "as long as it compiles, it's good enough" tutorials.) Your series may (so far) only teach the basics, but they are good, solid basics on which we can build further learning.
Your personality is a big + too. The viewer can feel that you're a good person who enjoys sharing his knowledge. That's important too when you teach beginners, since a beginner may need to feel some comforting human warmth in this unknown SFML territory.
You got a well deserved donation from me. Thank you, sir. :)
This is the ONLY resource on the entire internet that worked for me, I've been tearing my hair out over this for days. THANK YOU SO MUCH!
your tutorials are the best out there. really good job. you make it so easy to follow. thanks for existing
the same: IM SUBBING
Thank you for all the work that you put into these videos, I learned a lot and really became fond of sfml it feels more natural than SDL2.
Thank you so much for this tutorial! :3 Not only did I finally make my collision work, but I also understood what I was doing C:
I have a problem with my collision where it doesnt accept player before .GetCollider(). This happens at the very end between player.Update and view.setCenter. If you know what my problem could be please tell me.
Instead of push you could say inertia. Inertia is defined as how resistant an object is to any force. An object having a higher inertia would “push back” harder
i might repeat but i want more of ur stuff :D
It seems so damn easy with your tutorial!
As of 2.5.1 the coordinates systems is a bit different so if the colliders go crazy after touching, try inverting the ">" on the if statements in the checkCollision method
I think its because the Y axis becomes positive when tou go down and negative if you go up in the lasts updates. Idk why i also had to invert the X axis tho, i think that one is still normal
Hey its me from earlier videos! Don't worry i didnt give up and am still plugging away! I actually feel proud i managed to make a little "game" where a character moves and shoots and learned quite a bit! now im trying to understand collision this is really tough and my next challenge. I am probably going to watch this video alot!
I get an error "Expression must be LValue" on this part:
platform1.getCollider().chechCollision(player.getCollider(), 0.0f);
^
I got the same error, did you fix yours?
Same here, not sure what I'm doing wrong.
So I quess we will never get an answer what's wrong :(
Hey, I was just having this same problem and I fixed it by removing the & in the CheckCollision parameters.
So instead of CheckCollision(Collider &other, float push), it is CheckCollision(Collider other, float push).
(make sure to modifyt the header as well)
It needs you to store the players collider before passing it by reference, so Collider col = player.GetCollider(); , then pass col in. Since the getcollider returns a Collider, not a Collider* or & so as soon as that finishes it goes out of scope, meaning the pass by reference will be referencing bad memory.
It's usually more efficient to pass by const reference for somewhat complicated data structures (aka anything thats not a standard int or float etc) but if within the function that is taking it, wants to modify that type - it cannot be const.
Hello good sir! So far your tutorial videos have been fantastic.
However, I encountered an error on the main.cpp.
The player in platform1.GetCollider().CheckCollision(player.GetCollider(), 0.0f);
platform2.GetCollider().CheckCollision(player.GetCollider(), 1.0f);
gives an error: initial value of reference to non-const must be Ivalue.
Do you know how I can fix it?
me too, i don't now how fix ;(
see the comentary of Brian, fixed here
@@teamfortress2ful Who is Brian?
YOU HAVE TO TAKE & OUT OF collision & other, float push IN COLLISION.CPP AND COLLISON.H
@@camerontelford7667 Thank you so much!!!!
This is the best tutorial series on YT great job.
your thing called push in physics is named mass :D
Great video! can't wait to see a gravity tutorial
YAY THANK YOU FOR MAKING THIS!!
Great tutorial as always Vonck!
I may be asking too late now, but can you start an openGL series?
Realy nice examples that are easy too folow and bring much programming value with them.
Thanks ;)
When I write player.getCollider() in the CheckCollision method it tells me this:
initial value of reference to non-const must be an lvalue 49
cannot convert argument 1 from 'Collider' to 'Collider &'
Can anyone tell me why?
did you fix it? I got the same thing
@@aayushkarki108 nope, I've done the collider method in the class to resolve, but it isn't very correct
@@_simobr is it possible for you to share the resolved code?
@@aayushkarki108 meme its not properly solved
I got this error too, I don't know how he didn't get this. The problem is that in the getCollider function you are returning a temporary Collider object and you have a reference to Collider in CheckCollision function, you are trying to bind a non-const reference to a temporary object which is not allowed.
What I did was create an empty default constructor for Collider class, create private default Collider objects in the platform class and player classes, then in the getCollider function I set these objects to Collider(body) and return that value. Lastly, I have the type of getCollider be Collider& (reference to collider object) instead of Collider.
Now when you use pass in player.getCollider() in the CheckCollision method you are actually passing a reference to a non-temporary Collider object.
There might be a shorter way to bypass this issue but this is what worked for me.
And here I am at the end of the journey, eagerly waiting for your next video. As many others here, I have a question for you: I code on a laptop, and I've noticed that CPU usage goes up by a lot (from ~10% to ~70%) when I launch the application, so my machine starts heating up real quick. Apparently, this is due to the opening of the app window, and it's just in part VS's fault: if I open the .exe in the release folder it's still at least 40% extra work on the CPU. I wonder if that's normal and if there's a way to improve the performance on the system. Thanks
A better name for the variable "push" would be "movability", for example since a wall is not movable then you have a movability of 0.
Why isn't it a bool if it's always going to be either 1 or 0?
@@mort0303 it is not always going to be 1 or 0.. The case can happen that it is for example a heavy box and you want the push to be around 0.8 or so..
Edgar ah, ok. thank you
i think "push" variable can be rename to "resistance'
i`m trying to work your code out on my project , but somehow i cant collide in both ways , only on one way. if i do platform1.checkcollision with player it works well , but if i do player.checkcollision with platform1 it doesnt work . any clue on that? tried revising everything and it looks ok.
How to fix redefinition in collider??
Nice job, thanks!
Does it have to be a perfect square or would a rectangle work
+EricZeBaws The collider does not have to be a perfect square.
okay
Please keep the tutorial videos coming
ATTENTION EVERYONE WHO USED A LIST AND A FUNCTION INSTEAD OF CLASSES
if you had a list that you go throw with a for loop in a function to check for each individual tile
then when you collide do break; or its gonna continue to loop after collision has been detected and set collision to false thus fucking it up but I recommend if you are gonna use a functions make it a bool copy the code then in the update set isColliding = collisionFunc(tiles) (or however you called them) and everything is gonna be fine
error: no matching function for call to 'Collider::CheckCollision(Collider, float)'|
on
platform1.GetCollider().CheckCollision(player.GetCollider(), 0.0f);
same for platform2
What might be the reason? Anyone?
same
Hey did you fix this problem?
Hey.. For all who have the same problem, Here is the simply solution:
.....
case sf::Event::Resize:
ResizeView(window, view);
break;
}
}
Collider playerCollRect = player.GetCollider(); // (or) auto playerCollRect = player.GetCollider();
player.Update(deltaTime);
platform1.GetCollider().checkCollision(playerCollRect, 0.5f);
platform2.GetCollider().checkCollision(playerCollRect, 0.5f)
-----------------------------------------------------
I hope i could help..
Nice Tutorials Hilze Vonck
@Mr.Lowpoly Hey thanks a lot man! Really needed this for my project. I also have a correction though: you spelt "CollRect" wrong on the platform1 and platform2 lines
@Chez Boyz Oh yes sorry .. I changed it, thanks
can this 1 collision system be appliied to multiple sprites or game objects like enemies, pickups and walls as i thought this is some sort of collision parents for all objects. If so, how can i include it inside each one of them, means like do i have to declare different names of the collider for every different objects. Please let me know cuz im kinda new to sfml and also c++. T-T
8:29 Hilze is now an evil eyed sorcerer
in 14:33 i made that getCollision in my player.cpp file but it says that body is an undeclared identifier? I have even include the collision file in the h file of my player.
Hi! I am trying to apply this code into my project, changing it up a bit so it runs with sprites instead of a rectangleshapes, however, when it goes into the "GetPosition" function, it is tossing an Exception thrown at error, Access violation reading location.......not entirely sure what I am doing wrong and why returning a position is giving me this
Hey, I have a question. I need to know how can I make my character to pass through the, lets say right side of the window and appear on the left? Its really important for me.Ty
whats the point of (1.0f - push) ????
I have done exactly what is shown in the video but the platform is not moving. I tried writing platform1.GetCollider().Move(0.1f,0.0f); directly in my main file but still my platform isn't moving. Can anyone think of any mistake that I might be doing? I have already checked the video once again and haven't found any mistake
samee! ;(
Hello, i realy like you tutorials. You realy explain things well. but it would be nice if you could put the files online (or code).
I am running into some problems when i am colliding with the platforms. When i walk to the platforms, platform 1 is moving away(without colliding), and platform 2 isnt doing anything. except when walking upstairs, then it starts blinking
Could someone help?
it doesnt works if object are moving fast?
thanks
can You please send me the code of player.h where you returns the collider of body.
because i got an error that cannot bind non-constant value reference of type 'Collider&' to a reverse Type Collider.
This has something to do with the vs2017 and vs2019 compiler. With the vs2015 compiler you were able to directly use a returned copy as a constant reference, but you are not allowed to do this anymore with the vs2017 and vs2019 compilers. To fix this you simply have to store the collider in a local variable first and then pass that collider into the function, or change the function so it takes a collider instead of a constant reference to a collider.
@@HilzeVonck actually i am compiling this project using latest version of gcc compiler.
@@HilzeVonck I haven't quite figured out how to store the Collider inside of a variable, could you explain what you mean please?
is it possible to use this format/lib file to make 3D AABB instead of 2D AABB? * still for opengl
I like this, but it's a lot more difficult to get to work with smart pointers.
I did it!!!
Sorry I have one more question, How do you check a border in sfml. Ex If an object is touching the border it will do something
+EricZeBaws do you mean the border of the screen?
Yes
Here you go,
this function will check if a collider touches the border of a view.
bool TouchesViewBorder(Collider collider, sf::View view)
{
sf::Vector2f colliderHalfSize = collider.GetHalfSize();
sf::Vector2f colliderPosition = collider.GetPosition();
sf::Vector2f viewHalfSize = view.getSize() / 2.0f;
sf::Vector2f viewPosition = view.getCenter();
float rdx = colliderPosition.x - viewPosition.x;
float rdy = colliderPosition.y - viewPosition.y;
float dx = abs(rdx) - (viewHalfSize.x - colliderHalfSize.x);
float dy = abs(rdy) - (viewHalfSize.y - colliderHalfSize.y);
if ((dx < colliderHalfSize.x * 2.0f && dx > 0.0f) ||
(dy < colliderHalfSize.y * 2.0f && dy > 0.0f))
{
return true;
}
return false;
}
As you can see it looks almost exactly the same as the collision code for checking if two colliders are colliding.
That is because a view also has a center position and a size.
The only thing that really changed is the if statement at the end. This if statement consists out of two parts:
(dx < colliderHalfSize.x * 2.0f && dx > 0.0f) and (dy < colliderHalfSize.y * 2.0f && dy > 0.0f).
both of these parts basically do the same thing but one of them checks the collision on the x axis and the other one on the y axis.
let's take one of these parts and see what it does.
The first part (dx < colliderHalfSize.x * 2.0f) basically checks if the collider is within a certain range from the border (inwards), we need to multiply the halfSize by two because we need the full size for this calculation.
The second part (dx > 0.0f) basically checks if the rectangle is within a certain range from the border (outwards).
So the first part checks the collision for the left and top side of the collider and the second part checks the collision for the right and bottom part of the collider.
The reason that they are separated by an or ( || ) is because we want this statement to be true whenever the collider is colliding with one of the sides of the view, and not just the corners.
Thanks so much!!
Can you do a tutorial on gravity, so that when you jump you will fall down and so that standing on blocks matters otherwise you will fall through the screen.
+Elijah Andrushenko Yeah, that is going to be the topic of the next video. :)
When we should expect it? :D
+SmykuPyka As soon as possible. I don't have any time to make an episode right now, but I'll probably have some time next week.
Delta is called Δ in Greek Its the letter D
error: no matching function for call to 'Collider::CheckCollision(Collider, float)'
anybody else had this problem? :(
You do know that, SFML has functionality to detect collisions right?
Frixoe i
bruh he is detecting aabb to do the tile collsion
I get an error "error: no matching function for call to 'Platform::Platform(std::nullptr_t, sf::Vector2f, sf::Vector2f)'" when creating the platforms. Help anyone please?
holy shit this is soo hard :)
Bro how did you color code visual studio?
there's a bunch of tuts on YT
at higher speeds you can easily glitch through the collision, does anyone know how to fix this?
Hey i have an error C2664 "bool Collider :: CheckCollision (Collider &, float)": it is not possible to convert argument 1 from "Collider" to "Collider &"
Please really help me i wanna make the game but i cant
Hey, the problem is that it is trying to get a reference to an object that isn't defined anywhere. The solution is to simply store the result of the getcollider function and use that variable as an input for the checkcollision function.
This is an lvalue vs rvalue issue. vs2015 didn't see this as an issue, but since vs2017 the compiler started to treat this as an error.
I hope this helps
@@HilzeVonck i love u Man. Thanks for fast response. You made mine Day better 😀
@@HilzeVonck anyway i am new and i work at sfml like 1 hours can u tell me how can i use code to do it and where should i put it please. Still thanks a lot dude
@@HilzeVonck i am 14years So i am bad xD
Hello,
my Player keeps moving left or right through the screen at an absurdly high speed.
Any solution?
Please tell me you sorted it out, and can share how, because I'm having the same problem, but it's either moving downwards or upwards. :D
I believe that issue is because you aren't multiplying by deltaTime.
Collsion GetCollider() { return Collsion(shape); there is a error coming collsion.h is not inaccesbile
My getCollider function is not working! It has no match for Collider(body). Does anyone had the same issue?
I didn't understand the init-list (body(body)) but I will try to understand. :D
What this means is that the private variable body, will be initialized by the parameter body which is given by the constructor.
I walk by the square, and its not moving. 0 errors or warnigs. whats wrong?
Maybe you should check if you are passing the body in the Collider class by a reference.
Im actually more confused then before. You're doing with the centers, others with some shit like min and max and some with x, y, w and h...
10:35 reminder that std::clamp() exists too.
would you happen to know of a way to write *(simple)* non OOP no structs nothing, just bare bones code. of aabb response of 2 regular rectangles. or point me in the right direction?
i am desperate. ive spent hours and hours looking for a simple implementation that i can read, i just want to a solid square!!!!!! why is this so hard to find!!!!!!!!!!!!!!!!!.
im sure its not ideal but its the only way i can understand it.
so many tutorials. or rather always have the starting ccordinates as something crazy like
"entity.x.whatever.idontcare = delta.whatever.why?"
You can use the same code as is in the final check collision method. Just also pass in the position for the first aabb as a reference the size of the first aabb, the position of the second aabb as a reference and the size of the second aabb.
It shouldn't be hard to convert this to a simple, c like function.
@@HilzeVonck converting it is the problem I just wanna *(see)* an example so that I know how it works. Everything being in objects makes it way to hard to pick apart. Also I'm asking in every UA-cam video about aabb resolution.
@
It could look something like this. (keep in mind that I just wrote the code and did not test it, so there might be some small syntax errors)
The functionality of the function stays the same, the only thing that I changed is that we now are passing the positions and half sizes in directly, and we are also modifying these positions directly.
Just make sure that the position that you pass in is the center of the AABB, and the size is half of the total size of the AABB.
bool CheckCollision(sf::Vector2f& thisPosition, sf::Vector2f thisHalfSize, sf::Vector2f& otherPosition, sf::Vector2f otherHalfSize, float push)
{
float deltaX = otherPosition.x - thisPosition.x;
float deltaY = otherPosition.y - thisPosition.y;
float intersectX = abs(deltaX) - (otherHalfSize.x + thisHalfSize.x);
float intersectY = abs(deltaY) - (otherHalfSize.y + thisHalfSize.y);
if (intersectX < 0.0f && intersectY < 0.0f)
{
push = std::min(std::max(push, 0.0f), 1.0f);
if (intersectX > intersectY)
{
if (deltaX > 0.0f)
{
thisPosition.x += intersectX * (1.0f - push);
otherPosition.x -= intersectX * push;
}
else
{
thisPosition.x -= intersectX * (1.0f - push);
otherPosition.x += intersectX * push;
}
}
else
{
if (deltaY > 0.0f)
{
thisPosition.x += intersectY * (1.0f - push);
otherPosition.x -= intersectY * push;
}
else
{
thisPosition.x -= intersectY * (1.0f - push);
otherPosition.x += intersectY * push;
}
}
}
return false;
}
@@Herovy070 wow to be honest this is way more than I could have asked for thank you I'll check it out when I get home. I appreciate it. I'm gonna go ahead and give you a like and a sub
I keep getting the error 'bool Collider::checkCollider(Collider &,float)': cannot convert argument 1 from 'Collider' to 'Collider &'
At the very end, I've tried re watching the video multiple times but the code seems to be fine, anybody know what is going wrong here?
Hey. I got the same error. Did you manage to resolve it?
I've got this one too... If I find a solution I'll post it
Okay the solution I found was to put this line before we check collisions between (platform1 and player) and (platform2 and player in main.cpp: 'Collision (name of your Collider member) = player.getCollider();
After that, just replace player.getCollider with (name of your Collider member) in both checkCollision calls that follow
As far as I can tell, the compiler just didn't want us to use a member function for this variable. If anyone has a better explanation, I would love to hear it.
Not a solution, but I did this tutorial a while back on VS community 2015, and the code worked exactly as is, I am redoing it now to get back into SFML with VS community 2017, and I get this exact same error. So, perhaps it is something weird in the newest iteration of the compiler. And your solution did work for me.
It's not compiling, and it's telling me:
"initial value of reference to non-const must be an lvalue"
and
"'bool Collider::CheckCollision(Collider &, float)': cannot convert argument 1 from 'Collider' to 'Collider &'"
:(
I figured it out.
how
@@christiancarter255
@@christiancarter255 what was wrong plz tell us
@@woff5 I wish I remembered 😔😭
@@christiancarter255 no worries someone had the same problem and had the awnser
will this only ever work for squares? will it ever work for rectangles?
Nevermind, I can see that it works.
Redraw
Relocate
this guy reminds me of pewdiepie lmao
Hey,
first, its a good tutorial, thank you. But could you pleaseupload your code to github or something else?
I'm geting two errors in the code for this two lines:
platform1.GetColider().CheckCollision(player.GetCollider(), 0.0f);
platform2.GetColider().CheckCollision(player.GetCollider(), 1.0f);
Invalid arguments '
Candidates are:
bool CheckCollision(Collider &, float)
'
Invalid arguments '
Candidates are:
bool CheckCollision(Collider &, float)
'
invalid initialization of non-const reference of type 'Collider&' from an rvalue of type 'Collider'
invalid initialization of non-const reference of type 'Collider&' from an rvalue of type 'Collider'
Can anybody help?
I have actually been working on rewriting all the code to upload to the site that I am building all day. All of the source code for this video should be up there.
Here is the link: lucrifi.com/index.php/24-sfml-2-4-for-beginners-12-collision-detection-aabb?start=1.
As a note though. The site is not yet finished at all, so if something is wrong or if something does not work, It'll be fixed as soon as possible.
I think that the problem is either in the player's GetCollider() function or in the variables that you are passing into the CheckCollision() function. Make sure that the player's GetCollider() is NOT returning a reference to a collider. And make sure that the CheckCollision() DOES have references to the collider (both in the .h and the .cpp).
+Herovy070 whoops, I replied with my secondary YT account. Herovy070s reply is mine.
Thanks for the quick respons, I checked your link and checked your code with this page www.qualidator.com/WQM/de/Tools/TextDiff.aspx, I could not see any significant difference to my code,
This is the play's GetCollider()
Collider GetCollider() { return Collider(body);}
and this is CheckCollision()
in .h
bool CheckCollision(Collider& other, float push);
in .cpp
bool Collider::CheckCollision(Collider & other, float push){
Hi,Thx for this great tutorial! I'm using code::block and got the same error even after copying all files from lucrifi.com/index.php/24-sfml-2-4-for-beginners-12-collision-detection-aabb?start=1. I've solved it by using bool CheckCollision(Collider , float).
Please help! I have the same issue, or if there is anyone else who has a fix for that matter.
Please I need missing Clip!!!!!!!!!!!!!!!!!!!!!!
Great video but I got a question.
Why didn't you just use self.getGlobalBounds().intersects(other.getGlobalBounds()); ? Much simpler for AABB IMHO
Hey Ricardo, I got an error. Can you give me a hand?
yeah that's what I thought too
why was I making this whole lot of stupid stuff
platfom1.GetCollider().CheckCollision(player.GetCollider(), 0.0f);
platfom2.GetCollider().CheckCollision(player.GetCollider(), 1.0f);
player gives an error in my code?
yeah same did you figure it out