Programming controls for sprite - iphone

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.

Related

Unity 3D object with Rigidbody sliding

I have a cube with Rigidbody attached to it would slide slightly whenever I pressed the play button. If I leave it for awhile, it would slide to other side of the screen.
Anyone know how to solve this problem without using the "freeze position"? I don't know what I messed up in my project...
This is what the object looks like:
Note: I need to use the gravity.
Thank you!
Heh! The solution here is:
You had a rigidbody on the floor :)
You don't do that :) Never.
If the "floor" surface is flat, then,
it will not slide.
You've got something strange going on, such as
"floor" is NOT flat
a feature like "Wind" turned on
perhaps other objects invisible in the scene you have forgotten about are nudging it
PhysX does not have a "mind of it's own". There is some simple reason it is moving.
Let's say the "floor" is indeed on an angle, so it SHOULD move, but you WANT it to NOT move.
What you obviously do:
Just as in the real world, put something there to stop it moving.
A small invisible wall will do the trick. That's PhysX!
Usually rigidbody sliding happens when a lower rigidbody has lower mass than a higher rigidbody (forcing down the lower rigidbody). Typical problem with player having say mass 80 jumping on a cube with mass 1. In this case the collision is so violent that the cube will probably fly out (not only slide).
The situation is very similar to the real world. Try to stay on a box of milk if you have 120 kg (ok, ok, 80 :) ).
When you try to eliminate this behavior, you need either increase the mass of the lower object or decrease the mass of the higher one or set the lower rigidbody to kinematic.
The solutions above is not proper way of solving the problem. Unity has more features of physics than mass. If you get sliding on movement or because of other objects you should add proper drag value on your rigidbody. For example , lets say you have blocks spawning over top of other blocks and this creates horizontal sliding. In my case I add drag of 1 to the objects which has mass of ~ 1 kg. It depends on the scene and you should try different values on your case. Do not use bigger values and angular drag if it is not important.

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.

Need Unity character controller to make sharp 90 degree turns and not slide when turning

I'm using the first person controller for my characters movement. On a left arrow keypress, I'd like the character to instantly rotate 90 degrees and keep moving forward. Currently, when I hit the arrow key, the character makes the sharp 90 degree turn, but the forward momentum the character previously had takes a second to wear off so the character ends up sliding in the direction he was previously moving a short bit.
The closest example I can think of to visually explain what I'm trying to do is how the character turns sharp in Temple Run. How my game is currently working, if I had the character on a ledge make a sharp left turn, he'd likely keep the original momentum and slide off the edge right after he turns.
Since my character is running on the x/z axis, I'm wondering if there would just be some way to maybe swap the directional velocity/momentum? The speed the character had on the x axis would instantly be switched to the z when it turns and the other would be set to zero. I'm obviously open to any solution that accomplishes what I'm looking for.
I dug into the CharacterMotor class in the first person controller, but have yet to find what part I can tweak to accomplish this.
I'd greatly appreciate any help.
Thank you.
You can try to stop the velocity of the Rigidbody before turning.
this.rigidbody.velocity = Vector3.zero;
this.rigidbody.angularVelocity = Vector3.zero;
If you want the object to continue like it did, you can try playing around with it by saving the current velocity in a variable, setting it to 0, rotate it and then putting back the old velocity (still forward).
If it works with global vectors (so from the point of view of the world, not the object), then you can try negativing the velocity, actually causing it to go 'backwards'. I can't test it for now but either way I think you need to set the velocity to zero first before turning the character.

2D Game control question - rotation & movement

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.

Collision Detection between two rectangles

Fairly simple question that Im sure you will laugh at me for.
I have two rectangles playerRect and wall.
I have an if statement with the condition being ..
if (CGRectIntersectsRect(playerRect,wall)) {
//handle collision here
}
The problem I'm having is working out which side actually hit the wall rectangle.
I need to know because then I stop te player from moving depending on which side hit.
Thanks for any help
Disco
I would add some direction property to my 'Player' object. This way when you detect a collision, you just check to see which way the player was moving prior to the collision and react accordingly.
Create a CGRect for each side of your object with just a width of 1 (or height of 1 depending on the side) and look for intersections with the sides instead of the entire object. If your object is moving faster than 1 pixel per collision check, then you would check the sides in addition to checking the entire object