Separate two colliding rectangles

Started by
12 comments, last by marcClintDion 10 years, 9 months ago

What exactly is "MovableObject target" variable? Is it a tile, or another moving character? Also, I think you should replace everything to this:


public class MovableObject extends GameObject
{
	public void collisionRespone(MovableObject target)
	{
		if(isColliding(target))
		{
			float centerX = currX + width / 2;
			float centerY = currY + height / 2;
			
                        double overX = ((target.width  + width ) /  2.0) - Math.abs(target.currX - centerX);
                        double overY = ((target.height + height) /  2.0) - Math.abs(target.currY - centerY);
	       
                        if(overY > overX)
	                {
	                    if(this.currX > centerX){
                                this.velocity.x = 0;                      
	            	        this.currX += overX;
                            }
	                    else
                                this.velocity.x = 0;
	            	        this.currX -= overX;
	                }
	                else
	                {
	                    float vy = this.currY - this.getPrevY(); //vy is a positive value if we are falling.
	        	
	                    if(vy > 0){
                                this.velocity.y = 0;
	            	        this.currY -= overY;
                            }   
	                    else{
                                this.velocity.y = 0;
	            	        this.currY += overY;
                            }
	                }
		}
	}
}

I should tell you, that the object which you are passing to this function should be static, like ground tile, not movable object, at least just for now, to check if everything is working all right.

Secondly, I didn't know that it was a platformer, so now I fixed the code above, that when you colliding the with ground, we set velocity.y to zero, because whats the point of moving the object back, it if the velocity will keep getting bigger, and bigger until it's velocity is so huge, that it passes your obstacle in one frame, and can't check if it is collided or not.

You should put a vector in your GameObject class representing velocity, because as of now I see you don't have one. And each frame you will add that velocity to your players position like this:


currX += velocity.x;
currY += velocity.y;
“There are thousands and thousands of people out there leading lives of quiet, screaming desperation, where they work long, hard hours at jobs they hate to enable them to buy things they don't need to impress people they don't like.”? Nigel Marsh
Advertisement

Are you running a collision test before running the collision response function that's being shown above? The function above won't do anything to help you detect a collision and it wont even run unless isColliding(target) is set true by a collision detection algorithm.

I apologize if I sound insulting for asking such a question, but since this issue hasn't been resolved yet I thought I'd confirm the basic's.

Do you have something like the following that is setting isColliding(target) = true.

This is the basic formula for detecting two overlapping bounding boxes. It will help to cut out two squares of paper and write the variable names on the edges of the two pieces of paper so you can verify it and understand why it works.

//==================================================================================================================

if((player_rightEdge >= target_leftEdge) && (target_rightEdge >= player_leftEdge) && (player_TopEdge >= target_bottomEdge) && (target_topEdge >= player_BottomEdge)) { isColliding(target) = true; }

//=================================================================================================================================

Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, 3 because you know that the testing of your faith produces perseverance. 4 Let perseverance finish its work so that you may be mature and complete, not lacking anything.

Edvinas Kilbauskas: target is a movable game entity, not a tile. The entire purpose of this function, collisionRespone, is to separate two colliding entities. Is that what your function do? Just wanna make sure we are coding the same thing.

The code he posted definitely separates two objects that have collided. I was able to implement it and I'm quite happy with the results, objects are no longer getting stuck together.

currX and currY is the topleft corner and its current position in the game.

It's hard to tell what info you are feeding into that function but the following looks like a problem according to what you said.

The variable that is highlighted blue has been adjusted from the topLeft to be at the center. I don't see a similar adjustment for target.currX.

double overX = ((target.width + width ) / 2.0) - Math.abs(target.currX - centerX);

It looks like some of the variables are aligned for the top left corners and some are aligned to the centers.

Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, 3 because you know that the testing of your faith produces perseverance. 4 Let perseverance finish its work so that you may be mature and complete, not lacking anything.

This topic is closed to new replies.

Advertisement