Cocos2d Accelerometer Movement - iphone

I was wondering how you would move a CCSprite right and left by tilting the device right and left. Like in the falling balls app.

Try this:
http://www.cocos2d-iphone.org/wiki/doku.php/tips:using_accelerometer_for_sprite_movement
it is an example code to move the sprite on a plane surface along the x and y accelerometer axis as per the tilt.

The first solution provided is dependent on Chipmonk which is a 2d physics engine. The accelerometer can be used to move a CCSprite without the need of a physics engine. There are several ways to achieve what you want.
This is a great blog with awesome tutorials to get you moving in the right direction.
http://www.raywenderlich.com

Related

How to make a rotating plane rotate (move) an object sitting on it

I'm trying to make a car (or any object) in rotate when sitting on a plane surface or inside a tube shape. Currently the car acts like it has 0 grip on those surfaces (even tough the car moves without any problems when accelerating, but the objects don't move the car)
What I've tried so far:
putting a Physics Material on the tube & plane but has no effect
tuning the parameters of the Physics Material, no effect
marking the animation's Update Mode of the tube and plane as "Animate Physics", no effect
increasing the traction of the car (uses RCC), no effect
increasing the mass of the car..nothing
increasing mass of wheels, nothing
tried googling the problem, either didn't write the right terms or just couldn't find anything
Any help would be appreciated.
Thanks!
You need to rotate the plane and the tube using their Rigidbody with the AddTorque method or similar. If you just rotate them via their transform it has no effect on the physics system.

How to roll a cube on it's edges on Unity 3D

I want to move a cube by making it roll on it's edges. The cube will be standing on a x-z grid and each movement it takes will make it stand on a different square of the grid.
The player will controll the movement and will only be able to make the cube roll on one direction at a time (left, right, forward or back), but the cube must always stand exactly on top of one of the grid's squares.
I don't think applying a force to the cube will help cause it could move it a little bit too much or too little. I want to achieve something like this: https://www.youtube.com/watch?v=yaAIUYuNi84 but only on the x-z plane. Notice how on each corner the cube can stop and change direction with ease, because it never moves too much or too little.
Any ideas on how to approach this?
If you are new to the Unity it will be useless to trowing you a bunch of codes so i am telling you the way of doing it so you can implement your own codes.
You can create 4 empty game object which will always follow the cube on the floor and when you want to roll the cube you will rotate the cube around the empty objects.
You can find the codes for following the cube and rotating the cube on youtube, and for the starters searching is allways good.
So i hope you can manage it out, if you cant please write me again where did you stuck and i will be happy to answer you :)

SKPhysicsBody, rotate a sprite after applying impulse to change direction

I am looking for some info on how I can get my sprite to change its rotation by changing the angle of the sprite.
Basically here is how it is set up. Imagine a rocket that is sitting in a stationary position. The physics body is the shape of the rocket, with the tip pointing to the top. Now I apply an immediate impulse to the rocket to send it into the air. While in the air, I would like to use the accelerometer to rotate the sprite. With the rotation, I am expecting the direction of the sprite to change. Unfortunately, The sprite goes straight up and down instead of the direction it is rotated, even though the texture and physics body is rotated. I am having a hard time trying to find info on what techniques are used to achieve the results I want. Most examples have a continuous force, but I only have the initial force with no change after that. I have played around with linearDamping and angularDamping, but that was no help. I have also tried attaching another physics body to the tip to make the tip more heavy to see if the direction will change, but that didn't help either. My only work around that worked is to change the gravity direction, But I would like to avoid this. Any help or direction would be appreciated.
I think the solution you may be looking for is in Sprite Kits SKFieldNode class.
https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKFieldNode_Ref/#//apple_ref/occ/clm/SKFieldNode/linearGravityFieldWithVector:
The linearGravityFieldWithVector: is probably your best bet. It applies a linear gravity field in a direction you set.
You can also check out electricField which is similar however you can change the charge to positive or negative.

Cart physics movement Unity

I want to make a cart following rails so i made a cyclindric path and an invisible sphere that move along so i can follow the position of the sphere and apply to my cart .
There is no problem with position tracking however the rotation lookat have some issues every thing work fine when i climb forward
but if is a backward decent there is a flip on the object rotation then the camera which follow the cart rotation
I only want a solution that keep my cart looking straigth to the rail and the invisible sphere to look that he snap on rail and follow there angle.
Here is a Unitypackage of my project so that you can understand more what is the probem :
https://mega.nz/#!IY8WCagb!UUZYmWVHCVcoSZ11_gn-L5IMnJFVVtvkrO0mZxikw9o
Thanks in adance for your answer and your help
Try having two spheres, one for each pair of wheels. The one sphere will obviously travel the rails a short distance in front of the second sphere. Position of your cart is in the center of the two spheres, direction of view is from rear to front sphere.
pos = Vector3.Lerp(front_sphere_pos, rear_sphere_pos, .5f);
orientation = Quaternion.LookRotation(front_sphere_pos - rear_sphere_pos);
Depending on the size of the rails and the carts it may be that the spheres touch. In that case you have to disable collision between the spheres and only have them collide with the rails.

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)?