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.
Related
I am creating a game where the character can jump in the direction opposite of the surface he lands on. For example if he is on the ground he can only jump up. If he is on a right edge he can only jump left...etc etc.. Currently I have set up my SKNodes into leftWall, rightWall, bottomSurface, topSurface and have a huge if/else... this works but will become way more complicated as I add different surfaces with different angles etc...
I thought a better way to implement this would be to find the normal vector direction at the point of contact between the character sprite and a general wallNode sprites.
Can anyone help me determine A) the point of contact between sprites assuming the character has a circle physics body and the walls are always straight edges. and B) the normal to the contact point
thank you!
I don't think the vector or angle of impact will do you much good as your player's jump would likely be in an ark. As such, hitting the same surface at different points of the player's jump ark would yield different results.
I suggest you use unique category bit masks for your surfaces. A more advanced version of that would be to use only one category and utilize the SKSpriteNode's dictionary property to hold the angle value.
Based on the contact data you can then set the correctly angled jump for your player.
I would like to create a program that can identify arrows in a video feed and determine the direction they are pointing at (left or right). My aim is to use this program with an arduino robot in order to determine the direction in which the bot should move.
my problem is which method to use. I ve narrowed my options down to template matching or SURF. template matching is good because it is rotation independent, therefore it can determine between left and right arrows. However since the bot will be moving, the size of the template arrow might not be equal to that of the video feed, resulting in no matches.
SURF solves this problem however it is rotation invariant. This means that Left arrows and right arrows will be considered as the same thing.
Can anyone please suggest an approach I can use for this program.
Thanks in advance for any help
P.S I will be using OpenCV for implementation.
I managed to solve the problem by using canny edge detection and HoughLinesP. The system works pretty well but has a limited rotation range at which it will detect the direction correctly (approx 15 degrees).
basically I first performed colour detection to detect the arrow, then used houghlinesp to find its outline. Out of these lines, I eliminated all those which are horizontal or vertical, leaving just the ones at the tip as shown in red. I then used the end points of each line to determine the direction.
In the game Flight Control, planes can land only if they come from the right side of the airfield. How does that work? Where to begin?
I have a sprite that I can control with path drawing. Now I want to "land" my sprite when it comes to the right side of the airfield.
Without more data on your problem, I can guess this:
Once you detect a collision with the airstrip sprite, compare the coordinates of the plane sprite to those the airstrip sprite, and you should get an idea which side it's on.
What also matters is what direction the plane is going. In Air Control, planes that touch the landing side of the strip don't land unless they are also travelling in the correct direction.
This can be done by comparing your plane sprite's velocity vector to an acceptable landing vector for the airstrip in question, probably using a dot product of the normalized vectors. If the dot product between the two is close enough to 1, then you say it succeeded in landing.
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.
I'm implementing a small game and am having trouble getting the physics working properly.
In this game, there is one ball (a circle which moves from frame to frame, and may change radius) and several walls (line segments which also change and move from frame to frame). I can detect collisions properly, and making the ball bounce off in the correct direction is no problem.
Difficulties arise in situations where the ball intersects a line in one frame, then intersects it again in the subsequent frame, causing a double bounce. I could move the ball back along the normal of the line until it is in a valid position, but this causes really weird behaviour when the line in question is being hit along its axis (imagine a ping pong ball falling down on an upright toothpick and suddenly shifting aside so that it is on one side of the toothpick...). There are also a few issues when the ball intersects more than one line in a given frame (imagine four lines together making a rectangle and the ball intersecting the corner of said rectangle) -- which direction should it bounce off? In which direction should it shift?
I don't really have a specific question, but am looking for tips or some useful tutorials. All the 2D ones I've managed to find so far only cover rectangle intersections.
I'm using XNA if it makes any difference.
Thanks,
Cameron
This is an universal problem with most physics libraries. If you google for "penetration depth" (together with "physics", I suggest, or you might find something entirely different :D) you will find that even these libraries use tricks like yours.
There are two solutions to this:
The cheap one is to increase your update frequency. Move objects in 10 smaller steps instead of a single big one and you will have less penetration, so fixing it by offsetting the ball away from the wall will be less visible.
The expensive one is continuous collision detection. There are algorithms that can tell you, given a moving and a stationary object, the exact point in time both will will intersect. Google "swept sphere rectangle intersection" to find some of these.
You can then update like this: ball needs to move by 1.0 units. Check for collision. Collision occurs after 0.25 units, so move ball by 0.25 units, calculate reflection vector (so the ball bounces off the wall), repeat collision check with remaining 0.75 units (until you know the final position of the ball). This avoids penetrations entirely and even if your ball is moving so fast that it would normally skip over the wall in a single update, the collision will be detected.
It's generally accepted that, because of the timestep your collisions will intersect past an acceptable point in between updates.
Basically, you have to interpolate back to the point in between the last and current frame where the collision truly happened, move the object back to that point, and then calculate the forces, etc.