PhysicsBody affected by change of anchorPoint - sprite-kit

I have a situation where i need to use the resizeToHeight method (to zero), but i want to achieve a sliding door effect, where the resize happens from the bottom and not the center of the image (with an anchorPoint of (0.5,0.5), the resize pretty much happens in the middle.
Now, if i change the anchorPoint to (0,0), the resize occurs the way i want it, but the physicsbody of the object is not really affected by the change of the anchorPoint, thus messing with my collision detection (the invisible frame actually also collides and not the visible part of the image).
Based on what i could find online, it looks that maybe it's not the best idea to change the anchor point to CGPointZero. If that is the case, how can i handle this properly ? Or if CGPointZero is the way to go about it, how do i handle the physicsBody discrepancy ?

disconnect the sprite from the physics body, ie have a node that represents the body and another that represents the door image, that way you can move, scale them independently.
Best solution is probably to use a SKNode with physics and add the SKSpriteNode as child so you can offset it in relation to the physics body in any way you like, without having to constantly synchronize their position/rotation.

Related

How to align sprites of smaller sizes to a moving gameobject sprite?

I used to have a game object used as a sword with sprites with a specific size, despite including quite a bit of transparent space, to ensure these are aligned properly with the player game object:
This ensures the sword follows the player as he jumps by making use of the hero transition like this transform.position = hero.transform.position; , and though there may be issues with sprite changes I would address these later.
However, since I want to have several different equipment, and other sprites of this same sword might need a bigger dimension to look good (such as a sword attack while standing on the ground), I could either make even bigger sprites which would eventually affect performance due to transparent pixels loading, or I thought of making sprites with specific sizes:
(if this works I'd make sure to draw and put them close together instead of being separate)
And although when I prepare the animation I make sure to shift the position of the new sword to where it would be based on the player sprite on its own air attack animation (thus I had to modify this frame by frame,
The sword doesn't seem to follow the player, even when its game object still uses the script that makes use of the player's transform position:
I'm assuming something else has to be changed frame by frame, but what could it be? Is there a way to align or anchor a smaller sprite to follow the pivot of a bigger sprite?
All rotation or changing of sprites is done relative to the sprite's Pivot Point.
When you currently swap your sprites, your sword looks like it is rotating on it's blade rather than the handle.
Change the Pivot point to the handle, and it will do most of the work.
The rest is just making sure the handle of the sword follows the character's hands.

How to make Unity Parent Game Object ignore child Sprite whose dimensions are bigger?

I'm making a game object to place sword sprites in it for the parent object. To accomplish this, I've made sword sprites which are bigger than the parent sprite (parent is 256x256, sword is 384x384), so that the weapon size is not limited by the size of the parent sprite:
I've made it so all of these images' pivots are at the center, so in theory when the sword attack animation plays the sword game object should show the sprites from the center of the parent. While that does appear to happen, it seems the inclusion of these bigger images affect the parent game object dimension:
So, aside from addressing possible delays and z-index, it seems that because the image is bigger, the parent game object gets pushed down to ensure the top of the bigger image (the sword in this case) is stuck to the parent's top. Is there a way then to ensure child image dimensions are ignored so that the parent image doesn't displace?
So it turns out that, because I had all sprites' pivots as Bottom Center and I had changed the sprites specific to this animation to Center (so the sword and the hero would align) that the position of the sprites would change with respect to the rest, producing the abrupt down shift. The lack of synchronization was also not due to (at least not strictly because of) lack of optimization, but rather because the sword animation's duration was 15 milliseconds while the body and all its other child sprites' animations for the attack were 18 milliseconds, so the system may have tried to compensate somewhere. Changing the sword animation to last 18 milliseconds (by adding 3 more at the end without a sprite) synced them.

Detect When SKPhysicsBodies Overlap

I have two SKSpriteNodes, one is a thin vertical rectangle dividing the screen and the other is a ball bouncing from side to side. When the ball touches the divider a contact is triggered as expected. However, while the ball is moving through the divider I would like to continue to trigger the contact function for each frame.
How can I detect when two SKPhysicsBody objects overlap? I had some success using the intersectsNode() function but that uses the sprite's image size, which doesn't always match the physics body outline so I'm looking for an alternative.
Thanks!
See the allContactedBodies method in the docs.

nodesAtPoint not finding my nodes

I have an SKSpriteNode with a texture with a significant alpha margin around it. The texture is 92x92 points; the touchable frame of the node ends up being much smaller (40x40) because of the alpha margin. I would like the touch to register if it happens anywhere within the 92x92 texture.
I detect the nodes in touchesBegan with nodesAtPoint. However, if you touch in the alpha margin, the node is not detected. I tried changing this by overriding calculateAccumulatedFrame in my SKSpriteNode, but doesn't seem to have done anything. My method is rarely and unpredictably called. I assume if a node does not have children, SpriteKit refers to the frame property without using the calculateAccumulatedFrame method.
Any solutions?
I do not know how to actually change the way SpriteKit behaves regarding alpha in textures, but as an alternative solution, I overrode my scene's nodesAtPoint.
If you have to do this for some reason, keep in mind that CGRect's origin point is at the lower-left of the rect and not the center like a SKNode.

Creating a Body - Cocos2d/Box2d

I have a ball and another sprite. When the ball collides with the sprite it simulates falling.
My only problem is the other sprite is just on big image and the ball is on top of it, but there are spaces on the sprite and a lot of corners. I need to determine if the sprite has touch one of the corners. I know this is hard to understand.
So, my question is, is it possible to make a body without
b2PolygonShape blockShape;
and
blockShapeDef.shape = &blockShape;
OR
is there an alternative I can use? I cannot set the image as a box and it would take way to long to set edges because there are so many corners.
I have already set up the collision detection.
I really need help with this.
Thanks!
If you want it to react properly, you have to make a polygon using every single corner coordinate.
But don't be lazy about it. You can use SpriteHelper for creating *b2PolygonShape*s out of your sprites.
Or another alternative: VertexHelper