Dynamic Images and Player Moving Image collision Detection - iphone

I am working on small game and in that bonus stage is there. There are few stars(UIImageView) available dynamically created. When Player touches those stars it gets points. Player(UIImageView) can move on sensing accelerometer data from left to right and also can Jump if stars are at some height.
However I am unable to fit the logic for collision between Player and Star Image as those stars are being created dynamically at random positions how to sense intersections between Star and Player.
Should I use some timer which continuously checks for intersection between star and Player? This is not proper solution I believe as it will consume too much memory.
Please suggest me some mechanism so that I can proceed further. How to do this ?

No need for timers, just put your collision detection in the same place where you are handling player movement.(i.e., after you have moved player's UIImageView, test to see if it is touching any star)
(BTW, you should keep an array with references to the available stars to be able to test them for collisions)

Related

How do you wrap a level around based on the players position

I want to create a circular room in a 2D level. How can I handle this problem?
My thought process was to break the level into chunks and move their position depending on where the player currently is. This would allow the level to wrap around depending on where the player travels. I can do this manually with each part but i'm looking for a better solution that can handle this programatically. I'm open to better ways to solve this problem as well.
Is the space 2D? if so, you could place two invisible colliders at the extremities of the room (one at the beginning and one at the end), and change the player's position when he collides with them. To ensure that the transition is smooth, place them a little outside of the camera space: the player won't be rendered during transition, and you would obtain a teleport effect from side to side.
As another suggestion, you can lock the player to being in the center chunk with a camera just showing that chunk. Everytime he gets through a collider on the end or the start of the middle platform you delete the opposite side platform and place it in the far end of the platform the player is now seeing, effectively making the new platform as the middle one.

Adding Collision Detection to SKTileMapNode

What's the recommended way of adding collision detection to an SKTileMapNode for a side scroller?
Say I have a simple tilemap with just one single tile to use as the ground, how can I detect when my player sprite lands on one of the filled ground tiles?
I'm not entirely sure this will work, but it seems you could add a node to each of a SKTileMapNode's tiles and, based on whatever texture component is on it, give it a physics body appropriately shaped and setup for matching that texture.
Here is someone attempting something like this:
https://forums.developer.apple.com/thread/50043
You don't ask for it, but for greater granularity (eg a tile that's partially covered and needs a sloping physics floor element), it seems this would be the place to start:
https://developer.apple.com/reference/spritekit/sktiledefinition
But I can't find exactly how to know what part of a texture is on any given tile of a tile-map. There must be a way to do this, but I'm just not quite seeing it.

Collision Detection Between Two Objects in a Subview

I got the following question. I have two objects on my Subview. One of them is a player who is able to move on tiles. The other one is a wall. The player should not be able to move through or on the wall. Right now the wall is seen as nothing or a background (not a obstacle).
One idea I have is saving all the coordinates of the walls and checking if the coordinates of the player match the coordinates of a wall and undo the movement. In my eyes this idea is pretty inefficient and could probably done better.
Are there any other solutions?
If this is a game, use Sprite Kit, which gives you collision detection and automatic "bounce" behavior. If it is momentary animation, use UIKit Dynamics, which gives you collision detection and automatic "bounce" behavior. Otherwise, you just have to implement collision detection yourself (by looking to see whether the frame of one view intersects the frame of another) and perform the "bounce" yourself.

Cocos2d - Creating collidable Sprites?

Hello everyone I an very new to cocos2d, so I apologize if this is a simple question. I would like to create to sprites that collide when they hit each other.
For instance, one sprite, spriteA, is in a fixed position on the screen. And another sprite, spriteB, is moving around the screen. SpriteB will collide with spriteA. If that doesn't make sense or you don't understand it, tell me and I will elaborate further. Any help is appreciated. Thank YOU!
Try using Chipmunk or Box2d physics systems. These are included with Cocos2d and work by having a physics simulation that updates with every time the graphics change on screen.
The physics simulation will tell you if two objects are overlapping, and will provide physical attributes like weight, slipperiness (friction), speed and direction, which creates reactions like bouncing, sliding, realistic loss of speed and changes of direction on impact.
If you are new to physics simulation, here's a 30 second run down:
Create "bodies" in the physics simulation that represent each graphical sprite
Bodies are usually defined more simply than their graphical counterparts, like a circle, square or simple polygon shape
In order to update the graphics on the screen accurately, first you establish a pixels to meters ratio. Meters are notional (i.e. made up) measurement that is used in the physics sim
Each time the physics simulation "ticks", you update the graphics on screen accordingly
So if a body in the physics sim moves 1 meter, you might transform the pixel sprite 32 pixels
Here's a good tute on collision detection using Box2d.
http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone
enjoy
Its actually very simple:
Just schedule a timer: [self schedule:#selector(checkForCollision:)];
Specify the method: - (void)checkForCollision:(ccTime)dt {}
In the curly braces, make CGRects for each sprite using CGRectMake.
Then in the same method, just call: if (CGRectIntersectsRect) {}
That easy!
Well technically speaking when the 2 sprites interact, or share at least one common point, then they are colliding. I'm a little confused by your question. Are you asking for direction on how to make the sprite move on screen, or are you asking how to handle the actual collision(such as calling a method if they collide)?

Dragging a Sprite (Cocos2D) while Chipmunk is simulating

I have a simple project built with Cocos2D and Chipmunk. So far it's just a Ball (body, shape & sprite) bouncing on the Ground (a static line segment at the bottom of the screen).
I implemented the ccTouchesBegan/Moved/Ended methods to drag the ball around.
I've tried both:
cpBodySlew(ballBody, touchPoint, 1.0/60.0f);
and
ballBody->p = cgPointMake(touchPoint.x,touchPoint.y);
and while the Ball does follow my dragging, it's still being affected by gravity and it tries to go down (which causes velocity problems and others).
Does anyone know of the preferred way to Drag an active Body while the physics simulation is going on?
Do I need somehow to stop the simulation and turn it back on afterwards?
Thanks!
Temporarily remove the body from the space.
If you want the object to have inertia when you release it, that's a different story. The cleanest way is to attach a fairly stiff spring between the ball and a temporary sensor body that moves under the control of your finger. When you let go with your finger, the ball will retain whatever kinematics it had while you were dragging it. Be sure not to remove the ball from the space in this case.
You aren't updating the velocity of the object when you aren't using cpBodySlew(). That is why it falls straight down.
A better way to do it is to use a force clamped pivot joint like the official demos do to implement mouse control. http://code.google.com/p/chipmunk-physics/source/browse/trunk/Demo/ChipmunkDemo.c#296