cocos2d ccTouchMoved detect sprite colision only on one side - ios

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.

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 find the normal vector of the contact point between two sprites

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.

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.

Collision Handling Between Circle and Line Segments

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.

Farseer Physics XNA Geom 'Tripping'

I have an issue similar to http://farseerphysics.codeplex.com/Thread/View.aspx?ThreadId=72364
I have a rectangle player geom, and many rectangle tile geoms lined up next to each other.
Occasionally when the player geom is crossing between them he seems to clip onto the corners of the tile geom and as a result rotate over.
Even when switching the moment of inertia to infinity which prevents rotation, the player geom "hops" when it clips the edge.
Here is a screenshot of the geoms tripping image http://notspike.com/PrototypeG/trip.png
Is there any fix for this? I've tried the Farseer forums but it seems pretty inactive
Here is a link to a video using a circle geom for the player
http://www.notspike.com/PrototypeG/trip.avi
I would suggest that you avoid the use of small tiles placed next to each other for the level's collision geometry. For example, although visually it's clear that you're using tiles, the "floor" from the video would best be described as a long contiguous rectangle. That way, you don't have this problem at all ... floating point math is already imprecise enough and tiling collision geometry is just asking for that to go wrong :-)
I've had this happen before when my shapes are small enough or the movements are fast enough for the interval between physics calculation "ticks" to be enough to allow the objects to overlap before the collision detection kicks in.

Resources