Yes but how do i get to know where its colliding? Ahh i'll look online fine i did it already and nothing...well i'll try again.
I already answered this:
You really should use 1 rectangle for collisions, and, the easiest way to check which side collided with what is to move your objects along 1 axis, then check for collisions, then move along the other axis and check for collisions.
This allows you to, not only know where the collisions occurred (move objects along x-axis, and if collided, you know which side caused the collisions
When you 1st just move the object along the x axis (see my example), if there is collision, then you know it collided on the side (and, you can know which side by checking what the velocity is). You can then move the object along the y-axis, and do the same thing.
Well i did it...Now it looks for a collision every frame and if there is no collision it will move, if there is a collision it will not. And i also fixed the problem of getting stuck because it checks if the rectangle has collide not a side of it...am i making sense? i don't know...i had a hard day at school with exams and stuff...so here its my code, but its only for up and down....too lazy to do the rest now i'm gonna be back to it later but with this i'm sure someone can understand how it works and maybe help them too.
//the variables we will need for this (and of course you'll have to create the rectangles but i think you already know that o.o)
float plusVX = 100;//the +X axis
float minusVX = 100;//the -X axis
float plusVY = 100;//the +Y axis
float minusVY = 100;//the -Y axis
float currentV;//the current axis
boolean isO; //the boolean we will use to see if there is a collision or not
//checking to see if there is a collision
if(player.overlaps(enemy)){
isO = true;//if yes then set to true
}
if(!player.overlaps(enemy)){
isO = false;//if not set it to false
}
//if there is no collision then move
if(Gdx.input.isKeyPressed(Keys.UP) && isO == false){
currentV = plusVY;//set the current axis you're moving on
player.y += plusVY * Gdx.graphics.getDeltaTime();//and the value * Gdx.graphics.getDeltaTime() so it will be smoother
}
//if there has been a collision while moving on the +Y axis then stop the object
if(isO == true && currentV == plusVY){
player.y = player.y;
if(Gdx.input.isKeyPressed(Keys.DOWN)){
currentV = minusVY;
player.y -= minusVY * Gdx.graphics.getDeltaTime();
}/* the if statement above will check if down arrow key is pressed and then move it down by minusVY.
this will run for a frame or so until the object is not touching the other object anymore, and the
next frame the other code that take cares of down movement will take its place. we set the
currentV to minusVY when it runs so there is no need to press the button again to make the transition */
}
//and here we do the same, but this time we move on the -Y axis
if(Gdx.input.isKeyPressed(Keys.DOWN) && isO == false){
currentV = minusVY;
player.y -= minusVY * Gdx.graphics.getDeltaTime();
}
if(currentV == minusVY && isO == true){
player.y = player.y;
if(Gdx.input.isKeyPressed(Keys.DOWN)){
currentV = plusVY;
player.y += plusVY * Gdx.graphics.getDeltaTime();
}
}
Hope this will help others:D
I'm glad i finally did it because it was starting to piss me off....and i didn't understand what you guys meant by "move and check" because i didn't know how i would check which side it is on. Anyway glad i did it lol...thanks a lot!