Collision Detection Between Two Objects in a Subview - swift

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.

Related

How do I create an outline for my SKSpriteNode around the png texture in swift?

I am making a game in Swift(SpriteKit) and when the character in the game picks up a power-up, I want him to get an outline in purple so the player can see that his power-up is ready to use. I want the outline to hug all the curves of the player exactly. Is there any way to draw an outline around a sprite node that isn't a basic shape(like circle or square) but to outline a complex shape?
Here is an example of a shape similar to my sprite node:
And here is another example of the player with the visual outline that I want to add in SpriteKit(I just sketched it but I want it to be exact obviously):
They player also has his legs animating by flipping through an atlas of the animation, and preferably the outline would stay around the legs when the walking animation is happening. Is this possible?
To be clear, I just want an outline for visual purposes, so the player knows their "power up" shield is active so if they get hit it won't damage them.
I haven't dabbled with SpriteKit for a few years, and if memory serves me correctly perfect collision detection while animating is not available out of the box.
After checking the documentation I see that SKPhysicsBody has a init(texture:size:) initializer. This should at least get you a bit closer.
This guide from Apple is probably worth a read. (Pixel perfect collision detection is expensive).

Spritekit physics failing to detect second adjacent item

Kinda what the title says, I have a "wall" made out of multiple segments specifically for modular purposes. I have a contactTestBitmask that forces the player to stop moving in the direction of the wall once collided, but, if you slide along the wall to another segment, the player just rolls straight through it. I cannot do a collisionBitmask as the player runs fast enough to "phase" through the walls. I also cannot do collision as my camera and lighting are also dependent on the player's movement and position, and will not interfere with the walls (I think). If anyone could help point me in the right direction to allow me to make solid modular walls, I'd greatly appreciate it.
Figured out the source of my problem, contact IS happening, I just have wonky flags and the system doesn't like that. I need a contact currently happening method, I guess

Dynamic Images and Player Moving Image collision Detection

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)

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

iPhone: Camera following player in cocos2d

I'm making an iPhone game in cocos2d.
I was wondering how I would make the camera / the view follow a specific sprite?
would I use the CCCamera class?
Yes, CCCamera would work. However, it has some drawbacks that make it undesirable for some uses. Moving the layers respectively all other objects relative to that sprite may be a better solution. It depends on the game.
First, read up what the different approaches and their drawbacks are, you can get a lot out of this cocos2d forum thread:
http://www.cocos2d-iphone.org/forum/topic/5363
It would be helpful if you could describe what your game is about and why you need the camera attached to that sprite.
For example, if you're thinking of a running game like Canabalt, i would not use the camera to scroll over the world, but instead scroll everything relative to the player (towards him) with the player sticking at about the same x coordinate while running. Perfect examples of games where you would not move the camera at all are the iCopter games, they are basically simplified versions of Canabalt. Notice that the player sprite always stays at the exact same x coordinate, and the game world just scrolls
Scrolling the camera itself in my opinion makes the most sense if you have a large game world that the player can traverse in all directions, and the number of objects are simply too numerous and also moving about in various directions, so updating their positions individually each frame would be both overkill and prone to errors. And since the game world is so huge, you would want to use the camera's position to limit what is drawn on screen.
use CCFollow actions
Like these :-
[self runAction:[CCFollow actionWithTarget:(u r hero) worldBoundary:CGRectMake(0,0,1050,350)]];
it will helps