How to change the direction of gravity in Sprite Kit - swift

I'm trying to build a game about billiards. The screen is like a table, the default direction of the gravity system is downward, that is, the Y axis. And I want to change this direction to Z axis, so the ball has both gravity effect and will not fall down. Does anyone know how to do this? Thanks.

physicsWorld.gravity in SpriteKit is a 2D vector (dx,dy) and thus has no concept of the z axis.
for a top-down billiard game you might try turning gravity off self.physicsWorld.gravity = .zero and then use velocity and linearDamping on your billiard ball nodes to simulate realistic rolling.

Related

cylinder simulation on top of each other in unreal engine 4

I created 2 cylinders and put one on top of the other. Then I clicked to simulate physics and on each cylinder I added the following image blueprint, added a 50-point rotation on the z axis of each cylinder in opposite directions.
It turns out that in the simulation, when I perform, the cylinders rotate in one direction and move on the ground in the other direction. If it turns clockwise it moves left, and vice versa, and should be the other way around.
Can anyone help me solve this? It's for both cylinders to work together and I see how their simulation is accelerating with a constant rotation, but that's not what happens
If you want to simulate physics you should be applying forces to the cylinders using Add Torque in Radians or Add Torque in Degrees rather than modifying the rotation directly.
Alternatively, if you want to precisely control the cylinders, do not simulate physics. Instead, disable Simulate Physics and animate the rotation and position of the cylinders directly as you are.

Unity3D rotate GameObject to face direction of travel

I am creating a program using Unity3D in which the player fires an arrow at a target. What is the best way to ensure that the arrow is flying through the air at a rotation which depends on it's angle of movement? (ie the arrow head is facing upward of ascent and downward on descent)
I've had a look at the transform.LookAt and Quaternion.lookRotation functions I'm but now sure how they can apply. Here's the code which dictates the arrows velocity:
newArrow.GetComponent<Rigidbody>().velocity = new Vector3 (5*(transform.position.x - mousePos.x), 10*(transform.position.y - mousePos.y), 0);
Set the constraints on the rigidbody. In your case it should only rotate around the x axis so that it aims up and then down.
For the rest, you just set the initial rotation on throw:
newArrow.transform.rotation = Quaternion.LookRotation(velocity);
This may need some tweak based on your model initial orientation.

How to change gravity direction on Sprite Kit?

I am using Sprite Kit in Xcode and I was wondering how to change gravity direction.As default gravity direction to "X" you can imagine on below axes graphic.What about if I would like to change to "Y".
My goal is giving to object the falling effect.Its like falling from hight point and touching the ground than getting respond with physics!
(Could be dices on board game)
//Default gravity direction is X
SKSpriteNode *myNode =[SKSpriteNode spriteNodeWithImageNamed:#"ball"];
myNode.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:self.frame.size.width/2];
[self addChild: myNode];
Thanks in advance!
You can apply a vector to the Physics World of your scene using this code
self.physicsWorld.gravity=CGVectorMake(0,-10);
In SpriteKit, X and Y are the default coordinates that you see on the screen, and the Z coordinate is the order in which the objects are positioned (the zPosition). Since SpriteKit uses a 2D game engine, you do not have a third dimension, Z, to utilize. You can change the gravity between Y and X (Top/Bottom and Left/Right of screen respectively), but not between the Z coordinate. If you want to recreate a "dice falling" effect, I would recommend you create a Sprite called Dice scaled to a large amount, and once you add it to the scene you scale it down in x amounts of seconds.
[self runAction:[SKAction scaleBy:negativeFloatHere duration:3]];
This will make the dice appear to be falling, and you might want to add some spinning animations for it if you with. If you want to use the 3D engine, go try out Metal or SceneKit

Move a 3d rigidbody to its facing direction in Unity3d

I have the following scene:
I want the robot to move to each facing direction, so I tried:
rigidbody.AddForce(rigidbody.transform.TransformDirection(Vector3.up* speed));
And the robot is moving up like jumping.
I tried:
rigidbody.AddForce(rigidbody.transform.TransformDirection(Vector3.forward* speed));
but the robot is falling down
Any idea how can I solve this?
He is most likely falling down because he is falling over due to Physics.
You can give his Rigidbody rotation constraints to freeze his rotation in X and Z axes - so he can still turn to face a direction, but he cannot fall over. The use your "forward" code to move him.
These constraints can also be accessed from code, so you can for example turn them off if he is killed, so he falls over:
rigidbody.constraints = RigidbodyConstraints.None;

stick a sprite to another when it collide with it

so I have a sprite that is created every second and that is moving at a random position (sprite1) and another sprite that has a fixed position (sprite2). I would like that when sprite1 collide with sprite2, sprite1 is like sticked to it (it stops moving and is sticked to it) . How can I do this please ? sorry for my english I'm french :/
p.s : sprite2 is rotating with accelerometer, so if sprite1 collide with it I would like that it rotate too :)
I think, you can try to use box2d to do this. It will help to detect collisions and to manage rotations, movement, etc.
I think, you can do it simply in Cocos2d.
1) First set the rect for sprite1 and sprite2 using CGRectMake(x,y,width,height)
2) As you told sprite1 is moving at random position and sprite2 is fixed to particular position, you can check them collide by using CGRectIntersectsRect([sprite1 bounds],[sprite2 bounds]).
3) if it intersects, set sprite1.position = sprite2.position
Note: you said sprite1 is rotating, rect can be fit only to the regular bodies. if you want exact collision or physical properties for sprite better you can go for box2d.
If you don't want to use Box2d (which can handle circle collisions), you can try something like this:
1.) Detect collision, is the distance between the two circles center point (x,y), less than the sum of the two circles radius.
2.) Make the Sprite1 stick to Sprite2, Stop the movement of Sprite1, and save the relative delta (x,y) to Sprite2, then whenever Sprite2 moves or rotates apply the same delta movement and rotation to Sprite1.