How to manage borders of a sprite using SpriteKit - swift

I am just starting with SpriteKit, and I want to make a ball fall into a box...
I designed an open 2d square to act as a box. Whenever I launch the project, the ball doesn't enter the box, it rolls to one side. The background for my box is transparent.
If I haven't explained it well enough, I would basically like to know if I can make a sprite (ball) enter my other sprite (box), from one angle, but not be able to get into the box from a side angle...

Related

How can I create a Plane in Unity, that always shows up when using the AR-Camera in an App

I have an AR app, where I want to have a plane with text on it, that is always shown when using the AR-Camera.
The Programm only uses the AR-Camera. The Plane doesn´t show up at all so far, even though it does in the preview.
The Plane doesn´t show up anywhere in the AR environment.
I don´t really know what to add since I think the question is simple. If you need further details just let me know.
Little Update: After the start of the app and after the Unity Logo shows up, the screen is black for a few seconds and during that time I can see my Panel on the black background. As soon as the AR-Camera is activated, the Panel is gone.
Here is how to do it in 3 simple steps:
You need to add a Plane object to your Unity's scene
You have to align the Plane in the view of your main Camera's object, to your liking
You have to set the Plane as a child of your Camera's object by simply dragging your Plane entry to Camera entry in the Scene Tree Editor Pane (located in most cases on the left side of your Editor's window)

Side scroller style scene with gravity

I've started learning sprite kit and I think I've got the basics but now I'm struggling with something.
I want to create a game that has a 'Streets of rage' type feel to it whereby the user can move up and down, but isn't jumping, they're still on a 2D plane. But I also want them to be effected by gravity e.g stairs etc. like the following picture.
Am I right in assuming that I should have my background image with colliders around the blue and brown edges, and then create a physicsbody collider located at the feet of my player/players so that it looks like they can move against the background, but when their feet reach the top it would stop?
Could I then place other obstacles like rocks etc on that path that they would be able to collide into, but that could also sit over the path and the sky? How would I handle the fact that these could be constantly colliding depending on the position?
I appreciate there isn't any code here, but I'm trying to understand the concept around this before I jump in coding a solution.
Thanks
I would use a categoryBitMask to separate the different planes of objects.
And I would play with collisionBitMask/contactTestBitMask depending from the plane the player is in, in addition to the z-order.
Thus you can have a rock that collides your player if they are both in the same line else the player would walk behind/front.

How to move a tile (sprite) with drag and drop in Unity 2D?

I would like to move a tile with drag and drop in Unity 2D. The tile is a sprite. The scene is an 'Unblock me' or 'Blocked in' like gameplay.
Because the tiles in real life correspond to physical objects it seemed be to a good idea to model them with colliders and rigidbody. The border of the table surrunded with invisible colliders. I hoped these will constrain the moves of the tiles realistic, when the player moves them.
Then I implemented a simple (mouse based) drag and drop behavior which is worked perfectly except the moved tile penetrates to other tiles and the border, and sometimes jumps over them. Then I learned if I am overriding physics by explicitly setting transforms position (which I do exactly in my drag and drop implementation), this will happen. OK I accept, I should set only forces, ect. on rigidbody never directly the position.
Now the question:
I am stuck here. I still want to drag and drop like user experience, and some realistic visual result. When in the real life a player moves a tile, it seems it is "glued" to its finger. How can I achieve this (ot at least similar) with just applying forces? Any suggestions or point similar existing sample/blog code?
(I know as a backup plan I can omit all the physics and constrain the tile positions by code, and create some tweens to move the tiles. Is the real solution (what I am asking for) so complicated I should vote on this backup plan?)
Edit
According to comments I've added a video:
There is nothing wrong with the way you are manipulating your dragging. The beauty of developing is being able to do things in your own way. If it works for your game, then don't fret.
Now, i recommend:
Create a new physic material. Assets > Create > Physic Material
Set your new physic material inspector settings both to 0
Attach the physic material to all your wall object colliders. This should allow for your object being dragged to move smoothly against the walls without chopping.
Do a check to see if your mouse is over another collider. If so, then stop the movement in that direction.
Since your movements seem to always be on a single axis, on collision, tell your object to snap to the edge of the wall object. You know the Wall position and scales, also you have the position and scales of the object being dragged. with that you can write a function that will offset it to the correct position when the collision occurs.
Let me know if any of that works out :P

To follow a ball character like monkey kick off game

I am working on a game which has working similar to game monkey kick off. I have one ball bouncing on a place at the left side of screen and depending upon the position of the ball I apply linearImpulse to the ball on user touch so that it appears that the ball is kicked.
But what happens is when i apply impulse the ball goes out of the screen bounds. I dont want the ball to go out of the horizontal screen bounds whereas it can go out of the screen vertically.I tried using CCFollow but it doesn't give the realistic feel to the game flow.
I tried THIS tutorial, but dint help much.I managed to scroll the background,Only this part remains.
Any Ideas on how the ball should not move out of the horizontal screen boundary..? but in case of vertical boundary on the other hand it can go out of the bounds.
If you're not using physics, it's a simple bounds check. Assuming the screen width is 320 points this will keep the ball inside the screen horizontally:
CGPoint ballPos = ball.position;
ball.position.x = fmaxf(0, fminf(320, ballPos.x));
ball.position = ballPos;
UPDATE: I noticed you mentioned linear impulse. So you're using a physics engine. In that case, create a horizontal wall on both sides. See the cocos2d Box2D or Chipmunk templates how to enclose the screen with a collision border, then use only the left and right side borders in your game.

mouse joint is not working to restrict the ball in the half part of the screen

Hi guys I Am developing the application in cocoas2d using the box 2d frame work but unfortunately
i'm not able to restrict the gray ball in the half screen area of the image shown here
i want that ball not to go opposite part of the screen
I Have Used The b2Mousejoint For to move the ball around the screen
b2PrismaticJointDef restrict on any particular axis
But
i want to restrict on the particular rect area of the screen
You could create your custom distance joint which will restrict global axes of the ball. But it will be hard if you never write your own physics engine.
There are 2 easier ways to implement what you want.
Create 4 static "border" boxes around the area where the ball must stay. Then place the ball and the boxes into one collision group.
However, the response of the "border" boxes will not be instant. Therefore, the ball at high speed will sometimes "sink" into the boxes, then be popped out.
You can correct the position and reset the speed of the ball manually in code when it crosses the bounds of the desired area. But it may lead to the unstable physics simulation.