How to get which edge of two rectangles are colliding - geometry

I have an ALMOST working physics system. When two objects collide, each will have it's OnCollide function called so that it may properly react to the collision.
My issue comes into handling collisions properly for both x AND y. I know that an edge of a rectangle can only collide with one edge of the other rectangle. However, I don't know the math to determine which edge it is.
So how would I tell rectangle 1 which edge rectangle 2 collided with knowing only the x,y,height,and width of each rectangle?
(I'm working in c# by the way)

After a bit of trial and error, I was able to discover a solution.
First, you check which axis the collision is happening "the most" on. Once you've determined that, you simply compare the values on that axis to see which direction it's happening. The following psudocode should do nicely when translated into the language of your choice.
if absoluteValue(box1.y - box2.y) > absoluteValue(box1.x - box2.x)
if box1.y > box2.y
box 1 is collding with the top of box 2
else
box 1 is colliding with the bottom of box 2
else
if box1.x < box2.x
box 1 is collding with the left of box 2
else
box 1 is collding with the right of box 2
This doesn't take the boxes height and width into account, but I don't think that should be a problem in most cases. Though you may run into issues dealing with rectangles with largely different sizes in their axis.

Related

Libgdx sprite ring object passing through it

How is it possible object can pass through ring spirte like in the image below?
Please can you help me I have no idea how can i do that.
I think you posted a incorrect image. To get the image you posted you just have to draw the red bar on top of the black ring.
I guess you want the ring on the left side to be on top and the right side to be over so it visually goes through. Well this is simply not so easy in 2D since draw order.
I have a couple of suggestion you can explore.
Always draw the ring on top of the bar but when a collision is happening you calculate where the bar overlaps and don't draw the pixels in that place. You can use a Pixmap for calculations like this. Depending on the size of your images this could be very expensive to calculate each frame.
A faster but slightly more hacky way could be to split red bar in multiple images and if a certain part of it should be overlapped by the ring draw it first otherwise draw it after the ring. Depending on what the red bar is going to look in your end product and how much possible angles the bar could have I can imagine this can be very tricky to get right.
Use 3D for this. You could have a billboard with a slight angle for the ring and have the bar locked on the distance axis at the rings center. However, on certain angles of entrance and exit you will get Z fighting since the pixels will be at the same distance from the camera. This might or might not be noticable and I have no idea how LibGDX would handle Z fighting.
I wanna add this solution :
if the object gonna pass through the ring horizontally i propose to devise sprite ring in to to sprite (sprite 1 & sprite 2)
you just have to draw sprites in that order :
Sprite1
Sprite Object
Sprite2
You can do the same if the object is gonna pass through ring vertically
PS : this solution don't work if the object is going to passs through ring both Vertically and Horizontally
Hope this was helpfull
Good luck

How to draw shapes in the proper order when rendering?

I am trying my hand at writing a 3d graphics engine, but I am having some trouble with drawing the shapes in the correct order.
When I translate the points of triangles into window space, i.e. the 2-dimensional space that directly correlates to position on the screen, in addition to an x and y position of each point, I also assign them a depth variable that stores how far away from the viewer that point was in 3d space.
At the moment, the only shapes I am rendering are triangles. My current render order algorithm sorts the triangles by the average depth of their 3 points. I knew when I started it that it would not be perfect, but I wanted a placeholder for testing.
For testing purposes, I constructed a square box with an open top, each side being a different color and made from 2 triangles, as shown below:
As you can see from the image above, the algorithm I am using works most of the time. However, at certain angles and positions, the triangles will be rendered in the wrong order, as show below:
As you can see, one of the cyan triangles on the bottom of the box is being drawn before one of the yellow triangles on the side. Clearly, sorting the triangles by the average depth of their points is not satisfactory.
Is there a better method of ordering shapes so that they are rendered in the correct order?
The standard method to draw 3D in correct depth order is to use a Z-buffer.
Basically, the idea is that for each pixel you set in the color buffer, you also set it's interpolated depth in the z (depth..) buffer. Whenever you're about to paint the next pixel, you first check that z-buffer to validate the new pixel if in front of the already painted pixel.
On top of that you can add various sorts of optimizations, such as sorting triangles in order to minimize the number of times you actually paint the color buffer.
On the other hand, it's sometimes required to do the exact opposite in order to properly handle transparency or other "advanced" effects.

Increasing X coordinate positions sprite leftward in Phaser.. Why?

I'm doing a Phaser project for work and I've adopted the current code base.
I have a bit of an odd situation.
The game width is 1024 by 748. The world bounds are set by
game.world.setBounds(-1024,0,3072,748)
When adding sprites, the code base I've adopted has
game.add.sprite(2424,0,'name of atlas','name of png')
Based on this it appears the previous developer was creating these sprites off screen.
When I adopted this it was using Phaser 2.1. I upgraded to Phaser 2.2.1 because it had an Internet Explorer Fix I needed.
Now however, half the time the sprites will be created on the screen and the other half of the time the sprites are created off screen.
What was changed between the versions to create this type of issue?
Also note that, if I add a sprite with x coordinate is between 0 and 1024, I indeed see this sprite moving from the left to the right of the screen.
From 1024 to roughly 2200 I don't see the sprite at all.
However adding the sprite with an x coordinate from 2200 to roughly 2800 has the odd effect of starting at the far right of the screen and moving left. That means, when I use 2200 the sprite is at the edge of my 1024 game screen. When I use 2800 it is at the edge of my left game screen.
This is counter intuitive to me because as X goes up the sprite should move farther away from the left.
Anyone know why or what is happening here?

Problem in M3G rendering in J2ME

I have made 3 planes and positioned them in a way that they make a corner of cube. (For some reasons I don't want to make a cube object). The 3 planes have 3 different Texture2Ds with different images. The strange problem is when I render the 3 objects and start rotating the camera, in some perspectives some parts of these 3 planes don't get rendered. For example when I look straight at the corner a hole is created which is shaped as a triangle. This is the image of the problem in a netbeans emulator:
alt text http://www.pegahan.com/m3g.jpg
I put the red lines there so you can see the cube better.
The other strange thing is that the problem resolves when I set the scale of the objects to 0.5 or less.
By the way the camera is in its default position and the cube's center is at (0,0,0) and each plane has a width and height of 2.
Does anyone have any ideas why these objects have a conflict with each other and how could I resolve this problem.
Thanks in advance
looks like classic case of "box bigger then camera far clipping plane" error :)
since I don't know anything about m3g I can just point you to google that.

Vector math and rectangles

I know this is more high school math(wow been a long time since I was there) but I am trying to solve this programatically so I am reaching out to the collective knowledge of stackoverflow
Given this layout:
Midpoint is my reference point and in an array I have the vector points of all other points (P)
I can get to this state with code of having the light blue area by breaking it into four quadrants and doing a lame bubble sort to find largest(y) or lowest(x) value in each quadrant.
I need to find only the quadrants that outer border fully hits red no white space. For example the lower left and the up right dont have any white space hitting the light blue rectangle.
I am sure my terminology is all off here and im not looking for any specific code but if someone could point me to a more optimized solution for this problem or the next step in what I already have.
Thank you
I might do some BFI solution first, then perhaps look to generalize it or at least reduce it to a table-drive loop.
So, if it's exactly these shapes, and not a general solution, I think you should proceed sort of like this:
Derive the coordinates of the blue rectangle. I suspect one thing that's confusing you is that you have each individual x and y for the blue rect but you can't easily loop through them.
Derive the coordinates of the midpoint of each rectangle edge. You are going to need this because you care about quadrants. It will be trivial to do this once you have done 1.
Write different code for each 1/2 rectangle edge. There is no doubt a more clever way but this will get working code.
Make it more elegant now if you care. I betg you can reduce the rules to an 8-row
table full of things like 1, -1, or something like that.
First, you can't define red area by a single vector, since it's disjoint. You need the same number of vectors as the number of distant red regions.
Second, do we assume that different red figures neither intersect nor share a border? In the next clause I do.
Third, under assumption in point 2, the quadrant will have all red outer side iff there exists a contiguous red figure that intersects both its axes (i.e. rays). To determine this for all quadrants, you should only traverse all (P) points in the order they're given. This takes linear time and solves the problem.

Resources