XNA sword sprite collision detection - xna

I am new to XNA and I am having problems with collision detection when I rotate my sword. I am using a rectangle bounding box approach and I don't know how I can rotate my rectangle together with the sword sprite.
http://picpaste.com/pics/f10268c108e885498a4ae603ab030a60.1328941784.png
The orange line represents the sword and the blue rectangle represents the bounding box, I want to also rotate my bounding box to the position where the arrow goes.
Any suggestions on how can i solve my problem is really appreciated. Thanks!

I have a sample showing how to detect rotated rectangle collision, you'll need a similar approach for detecting rotated bounding box collisions.
My sample uses the Separating Axis Theorem which is a very common approach. You can read my sample here on Rotated Rectangle Collision
There's also a ton of fantastic resources out there on helping to try and get a handle on the concept of rotated rectangle collision detection. Here's a few I used in constructing my sample.
http://www.metanetsoftware.com/technique/tutorialA.html
http://www.codeproject.com/KB/GDI-plus/PolygonCollision.aspx?print=true
Now that you know the term to search for, you should be able to find even more that meet your learning style.

Related

A suitable algorithm for estimating the height(z) of a flying object?

I'm trying to figure out the height of a flying object, like the tennis ball in the picture below. Currently, I'm not using a stereo camera, and I'd like to tackle this problem only with a monocular camera. Any help will be appreciated!
Strictly speaking, you can't because the ball could be anywhere along the ray to the observer. To some extent you can use the apparent ball diameter to estimate the distance to the observer, but this will be pretty inaccurate. If the ball is measured close to the player, you'd better rely on the player's anatomy.
Another option is to determine the location of the feet of the player, as you known that they rest on the ground plane, and you can use the white lines as a reference. Then you can admit that the ball is at the vertical of a foot.
In any case, you need to be familiar with projective geometry and calibration techniques.
Yet another option is to use a camera that is far away and pointing horizontally (ideally with a telecentric lens, but this is not affordable). In this case, all points of equal height appear approximately on the same horizontal on the image.

how to manage countor bounding rect in opencv

I have been testing background subtraction using gaussian state model. I am using opencv
2.1.0. I can generate binary image of foreground of the scene. Now all I want to do is Draw
countour bounding rectangle to highlight the moving object. I have used cvCountourBoundingRect
to obtain the rectangle covering countour. The issue I am facing is in case of multiple
countour, sometime nearby rectangle overlaps. Here, can anyone suggest me to prevent
overlapping of rectangle? In ideal case, two rectangle should not be overlapped. It rather
should be draw a bigger rectagle which covers all two rectangles.
Any suggetion will be greatful.
There's no ready possibility to do this in OpenCV. But actually the algorithm is very easy:
Cycle through all rectangles and check if two rectangles overlap each other. This topic will be useful: Determine if two rectangles overlap each other?
For every overlapped pair of rectangles create rectangle that contains both of them. To do this you should select one corner from first rectangle and another corner from second rectangle and these two corners will create rectangle for you. I don't think that it's a hard task - just simple math.

Detecting where the ball hit the bounding box? XNA

just got started with XNA and this example would help out a lot!
I´ve done a simple test where i got some terrain and a ball, when the ball hits the terrain it stops. This i do by setting the ball position to just on top of the terrain on collision.
To my question, im using bounding box for this, but it doesnt look very well if the ball hits the terrain from the left or right side since it respawns on top. How do i check of it hits on side?
Short glitch of the code right now:
if (playerOne.BoundingBox.Intersects(terrain.BoundingBox)
playerOne.Position.Y = terrain.BoundingBox.Top;
Where playerOne is the ball.
Thanks in advance!
There is no built in way for XNA BoundingBox to help with determining which side has collided. Some people have split up the bounding box into 6 boxes (one for each face of your current box, like walls around a room) but this still gives a little trouble near the corners where you may have overlap and have to resolve which box you think hit first.
The ultimate solution is to create a Plane for each face of your current bounding box and if the bounding box detects a collision, then check for collision against each plane. When you have collision, it may be against 1, 2, or 3 planes at once. If it's against more than 1, you then need to determine point/plane contact positions for each involved plane & whichever is closer to the ball's previous position, that's the side it hit first.
I strongly recommend this tutorial, it covers your exact situtation by breaking the bat into different areas and setting various conditions for collision rebound by using the Math Helpers and radians.
http://ross-warren.co.uk/pong-clone-in-xna-4-0-for-windows/6/
It's side on but you should be able to adapt the principles.

XNA Collision Detection in Platformer

I've been working on a simple 2D platformer engine. So far I've got a sprite moving around (not yet animated), and 3 platforms for a "Jetpac" type game (old ZX Spectrum game - I'm sure if you google it you can play it in a flash box).
Now I'm onto the problem of implementing collision detection so the sprite can actually walk on the platforms. I think this will be the biggest job and then it's quite easy to continue. But how to implement collision detection with platforms?!
I have bounding boxes for all platforms, as well as the character, and later on there will be bounding boxes for enemy sprites, but that can be handled later. Basically, what is the easiest way to allow a sprite to walk on a platform, and not go through it using Bounding Box?
A bit more information:
Character class controls drawing the sprite and updating the sprite using a Vector2 position variable, updated with a Vector2 motion variable.
A Platform class controls the drawing of the platforms (there are 3), so plat1, plat2 and plat3 are all types of Platform.
The platforms need to be fully solid on all sides, but allow the sprite to walk on.
Any help?
Basics for simple collision detection with rectangles:
Use the rectangle struct for your bounding boxes. You can then use the intersects method to compare your platform bounding boxes to your character bounding box.
Basics for maintaining performance:
If you have large levels with a lot of platforms your game might get slow if you compare all your platforms with the character. You can for example use the axis aligned bounding box technique to avoid this. The basics of AABB is that you sort your bounding boxes along the x and y axis and therefore will get an aproximate location of the BBs.
Last but not least:
Look at the platformer tutorial.
Have you considered the option of using a physics engine in your game? For a simple platformer this might be overkill, but you can easily add additional physical effects.
2D physics engines I can advise you to look into are:
Box2D, which has a native XNA port (see here)
Chipmunk, which once had a port to XNA, but it seems to have gone missing.

Rendering a point light using 6 spot lights?

I'm trying to render 6 spot lights to create a point light for a shadow mapping algorithm.
I'm not sure if I'm doing this right, I've more or less followed the instructions here when setting up my view and projection matrices but the end result looks like this:
White areas are parts which are covered by one of the 6 shadow maps, the darker areas are ones which aren't covered by the shadowmaps. Obviously I don't have a problem with the teapots and boxes having their shadows projected onto the scene, however as you can see the 6 shadow maps have blindspots. Is this how a cubed shadow map is supposed to look? It doesn't look like a shadowmap of a point light source...
Actually you can adjust your six spots to have cones that perfectly fill each face of your cubemap. You can achieve this by setting each cone's aperture to create a circumscribed circle around each cubemap face. In this case you don't have to worry about overlapping, since the would be overlapping parts are out of the faces' area.
In other terms: adjust the lights' projection matrix' FOV, so it won't the view frustum that includes the light cone, but the cone will include the view frustum.
The a whole implementation see this paper.
What you're seeing here are a circle and two hyperbolas -- conic sections -- exactly the result you might expect if you took a double ended cone and intersected it with a plane.
This math may seem removed from the situation but it explains your problem. A spotlight creates a cone of light, and you can't entirely fill a solid space with a bunch of cones coming from the same point. (I'd suggest rolling up a bunch of pieces of paper and taping them together at the points to try it out.)
However, as you get far from the origin of your simulated-point-source, the cones converge to their assymptotes, and there is an infinitesimally-narrow gap in the light.
One option to solve this is to change the focus of the cones so that they overlap slightly -- this will create areas that are overexposed, but the overexposure will only become obvious as you get farther away. So long as all of your objects are near the point light source, this might not be much of an issue.
Another option is to move the focus of all of the lights much closer to their sources. This way, they'd converge to their assymptotes more quickly.

Resources