2D Game control question - rotation & movement - iphone

I was wondering if anyone could help me get started or pointed in the right direction. I'm looking to make a simple driving game using core animation. I'm a pretty good obj-c programmer, but when it comes to movement and math, I fail. I want a steering wheel to control the direction of the car, and a forward/backward to control acceleration & deceleration. If anyone can help me with the steering wheel code, I would be greatly appreciated! So basically I need help making a circle that can rotate with 1 finger drag, and i'll pass it's transform values to my car view ( i think i can handle the accel/deccel code) Any takers? :) Also, I have 2 invites for dribbble.com to give away, anyone who helps me, id be glad to give one out to.
grady, totowtwo, thanks for both your answers, I appreciate it. I;ve gotten to the point where I can rotate a "steering wheel" and it will rotate my "car", I also got it to move forwards and backwards. Just need to moving to be more realistic... Here is a link to the xCode project, http://www.cl.ly/7VBU, kept it very simple so if anyone looks at it - it will be easy to change / add code. So, if anyones wants to look at it,and help me make the movement more realistic, i'd be forever in your debt! :) thanks!

A starting point would be to recognize that a typical car steering wheel has 900 degrees of movement, whether you want that for the finger wheel or not is a design decision. The wheels in a car typically point about 30 degrees off +z at maximum rotation of the wheel. You could use a simple linear transformation from steering wheel angle to wheel angle.
From there, Car delta angle is a function of wheel angle times forward distance. As the car moves forward, you can imagine tracing the tangent of a circle with an infinite radius when you are pointing straight ahead, and a circle of smaller radius (~4x car length) when steering wheel is full tilt.
Of course, feel free to discard the design rules of typical vehicles. Give your users fun drive trains like holonomic robots have!

I am no expert but you could probably do something like:
CGPoint centerOfWheel;
CGPoint currentTouchPoint;
float radians = atan2(currentTouchPoint.x-centerOfWheel.x, currentTouchPoint.y-centerOfWheel.y);
then apply the transformation to the wheel ... of course you will want to make the transformations cumulative on the view over a distance, as continued turning of the wheel will result in continued turning.
also to make it feel more real, and less responsive you may want to use a lesser degree of turn, so that turning the wheel in once circle doesn't result in a whole circle turn of the vehicle. you will have to play around.

Related

SpriteKit move node with physics rather than updating position

My main character moves by touching and holding him and then moving your finger left or right to move the character. To do this I just simply update the node's x position (walks left and right on a flat surface) in touchesMoved() with the x position of the touch location, and apply an animation depending on the direction he's moving.
I want to kind of take this to the next level and accomplish the same effect, but using physics, so that when I'm done moving him and release my finger, he may slide a little bit in the direction he was moving given the speed I was moving him at, if that makes sense. Does anyone know how I can accomplish this effect?
Would I have to do as I'm currently doing and update the position as it moves, but also apply a force/impulse at the same time? Kind of confused on how to approach this
Moving the physics body via force, impulse, or velocity will automatically update the player position.
So you will have to play around with the correct way to accomplish your goal. But based on this, what I would suggest is replace your .position code with .physicsBody!.velocity code in your touchesMoved. Then, on your touchesEnded, you can apply a bit of an impulse to give the player that little bit of an "on ice" effect to keep them going a tad.
You will need to experiment with how much velocity you want to get the character to move at the correct speed, and then you will need to play with the impulse figures as well to get it just right.
This could get a bit tricky though, in touchesMoved... because at some point you will want to reset the velocity to 0 (so they stop moving when your finger stops).
To do that, you will need to to use the .previousLocation from your touch object, and compare the distance of X moved. if the distance X moved is >0 (or some deadzone threshold) then apply the velocity; if the deltaX is 0, then set the velocity to 0.
This may actually be more complicated than just using .position to move the character, then having the character slide a bit with physics on touchesEnded.
You will have to play with it to get it right.

Unity physics about collision and energy

I have been working on a Unity ping pong game using the Leap Motion. I use rigidbody.MovePosition() to move the paddle. However, when I hit the ball (which uses gravity), the paddle launches it too far. Even when I change the masses of both, it doesn't do anything.
What variable should I change to reduce this energy going into the ball?
From the following link.
"MovePosition will put your object at the target location, no matter what. It may push aside other objects in a realistic way, or may blast them out of the way, or may just pass right through them. But it will gladly move you through a solid wall or a mountain.
If you're using MovePosition on a rigidbody to add from where you currently are, it looks like AddForce. With AddForce, the physics step does all the work: applies your velocity, sees the collision and handles the bounce. With MovePosition, the physics step sees you're mysteriously overlapping a solid object. If it isn't too much, it will bounce you apart."
You won't need to use MovePosition. Instead, you can figure out the direction of the shot (based on the position of the ball relative to the paddle). Then you can add an impulse force in that direction to the ball.
Pseudo-code (from the following link):
Vector3 shootDir = ballPosition - paddlePosition; // Calculate direction of the shot
shootDir.Normalize(); // Normalize the shot vector
ball.AddForce(shootDir * speed, ForceMode.Impulse); //Add impulse force in correct direction.
Credit due to Owen Reynolds and Tim Michels.

How to get a ball/sphere to stop

I got a field in unity3d that has some depressions in it (like small holes). The field's slope always leads towards the nearest depression.
A sphere is dropped at random somewhere in the field, rolls around a bit until it stops in one of the depressions.
The problem is, this is taking too long. It could roll around for 5-10 seconds until it stops. I'd like to stop faster.
Any ideas how I can achieve this?
Edit: The main issue is when the ball is next to the depression, but it has speed that is 90 degrees from the hole, then it starts going in circles and takes a while to stop.
Ok, after getting some advice in the comments, and experimenting, this is the way I solved it:
Apply a small measure of strength towards the depression
If the current velocity is more than 30 degrees away from the center of the depression, slow the ball (apply strength in the opposite direction of the velocity)
IF the ball gets very near the center of the depression, stop it and place it in the center
Thanks for all the tips. If anyone comes up with a better way, I'm still open to suggestions.

Programming controls for sprite

I am trying to make a snake kind of game on the iPhone. For that I plan to have a simple dot as the sprite which draws a line. The dot is controlled by two buttons on the left and the right of the screen enabling it to turn left or right. Now I really do not have any idea how to make the sprite move automatically forward and how to program the turning. It should not be exactly as in snake where it abruptly turns left or right but it should more be a smooth curve. I hope you're getting my point and I'd appreciate all kinds of thoughts! Thanks a lot!
Somewhat trying to make it like this:
http://www.bnet.im/images/screen_curve.png
There are a lot of ways of doing this, but one simple way would be to store the angle of the snake's trajectory (I've called it theta) and move it a fixed amount in every call to Update.
Assuming that the snake class inherits from CCNode:
-(void)Update:(ccTime)dt {
self.position = ccp(self.position.x + cos(theta)*dt,
self.position.y + sin(theta)*dt);
}
You could then update theta from your event handling logic by increasing it or decreasing it when the user taps to turn left or right.

Body moves too fast after ApplyLinearImpulse()

I have top-down game for iPhone (no gravity) and I'm using Cocos2d and Box2d.
I try to move a bullet by this code:
// 'targetPosition' is a point of touch
b2Vec2 touchInWorld = b2Vec2(targetPosition.x/PTM_RATIO, targetPosition.y/PTM_RATIO);
b2Vec2 direction = b2Vec2(touchInWorld.x - ballBodyDef.position.x, touchInWorld.y - ballBodyDef.position.y);
b2Vec2 force = b2Vec2(direction.x, direction.y);
force.Normalize();
ballBody->ApplyLinearImpulse(force, ballBodyDef.position);
The problem is that the ball moves waaaay to fast if the sprite is small (10x10 px).
If the sprite is 50x50, then the speed is smaller and looks okay.
It's driving me crazy because I can't control the speed at all.
And not only that, if I don't put force.Normalize() the the speed is different depending on the direction of touch ...
Everything was working great when I was using just Cocos2d and animations. I tried using Box2d to implement collisions but it seems such a HUGE amount of effort that I am considering doing the physics myself :(
First thing , i am not sure but why are you dividing by PTM_Ratios here, i don think you need that, may be you are handling it some other way.
Second, 10x10 travels faster than 50x50 because you are setting SetMassFromShapes, so assuming you are creating a 50/PTM_RATIOSx50/PTM_RATIO body for 50x50 sprite, the body has greater mass than 10x10 body. so it travels slow.
Third, you are using Distance for Impulse so definatly it you are not going to Normalize you are going to get a Proportional Impulse to your distance
and for the Hackish Solution , if nothing else works for you just Decrease the PTM_RATIO!