Question about rotation around center of object using CBasicAnimation - iphone-sdk-3.0

I am trying to spin a wheel on its axis -- setting it in motion using the mouse. Specifically, I am trying to spin a roulette wheel.
I calculate a delta x and delta y by getting the difference of x1 x2 and y1 y2. I can see in console via NSLOG msgs that it is ok. I use these values to calculate velocity.
CABasicAnimation *fullRotation;
fullRotation = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
fullRotation.fromValue = [ NSNumber numberWithFloat:0 ] ;
fullRotation.toValue = [NSNumber numberWithFloat:(((360*M_PI)/180)) ;
fullRotation.duration = duration ;
fullRotation.repeatCount = repeat ;
[myview.layer addAnimation:fullRotation forKey:#"360"];
The above code gives me fluid animation, but the wheel always completes a full circle, which would be not very realistic.
Can anyone suggest a method for spinning the wheel by smaller amounts that still gives fluid animation? When I use the above, the wheel doesn't move at all for smaller amounts.
Thank you!
Piesia

It sounds like you need a little physics modeling to get the right action on the wheel. I'm not familiar with the APIs and I don't see where the code takes into account the velocity imparted by the finger movement, but I will dust off my physics lessons from 20 years ago with the hope it will help.
The initial velocity of the wheel is going to be determined by the motion of the finger at the time of release. We know that the linear velocity of the wheel at some point will be equal to the angular velocity multiplied by the radius to the point where we're measuring the velocity.
linear velocity = angular velocity * radius
As the wheel spins, it's going to encounter friction from the spindle against the wheel and this will cause it to slow down. Friction is a force applied over time until the wheel comes to a stop.
Friction = Moment of Inertia of the Wheel * angular acceleration
or
Friction / Moment of Inertia of the Wheel = angular acceleration
For the purposes of your application this will be a constant and you'll probably want to experiment with different values for this until it "feels right."
To calculate the amount of time, solve the following equation for time:
0 = angular velocity + angular acceleration * time
Angular acceleration will be negative because you're slowing down. Once you have the amount of time, you're going to calculate the number of revolutions by the following:
revolutions = ((angular velocity * time) + (0.5 * angular acceleration * time^2)) / (2 * pi)
time^2 is time squared, or time * time.
I hope this helps.

Related

how to do physics for space war?

I am trying to do a basic version of space war(http://en.wikipedia.org/wiki/Spacewar_%28video_game%29) but I cannot figure out how to do the inertia part
that is my code :
I should let the ship accelerate or slow down based on where it faces
model is the ship
vx and vy are velocity of x and y direction
theta are rotate degree
20 is for make it move slow
vx=model.vx+(cos (degrees model.theta))/20,
vy=model.vy+(sin (degrees model.theta))/20
but it does not seem right
Can someone help me?
I am horrible in physics!
A very accurate and efficient integration is to compute: PosNext = 2 * PosCurrent - PosPrevious + Acceleration*Timestep^2
It is called Verlet integration scheme. For Velocities you just update by: VelocityNext = (PosNext-PosCurrent)/TimeStep.
You can use your sine and cosine with the acceleration constant. Euler forward is not very accurate, try to avoid it.

When to start braking when heading for a position?

With only 1 dimension, you want to get to position X and stop there. you have a maximum acceleration A you can apply yourself; each frame you choose what direction to accelerate.
So if you have a velocity V, and want to stop at position X as fast as possible, how much of your maximum acceleration A do you apply yourself?
(If you are far away, you apply your maximum acceleration, but when you are close, you start braking. So based on your velocity and remaing distance, you need to decide when you begin to brake.)
The governing equation is x = v t + 0.5 a t^2
where x is distance, v is velocity, t is time, and a is acceleration in compatible units.
With no other constraints, in order to minimize your travel time, you will always apply maximum acceleration: Accelerate in the direction of the goal until you are halfway there, then accelerate in the direction of the origin until you stop.
If you have a maximum velocity, accelerate until you reach that maximum velocity, which will happen at some distance X away from the origin. When you are distance X away from the goal, accelerate back toward the origin.
Just dropping in with the answer to a more advanced version of this question I solved lately. You have v_0 at x_0, and want to reach position X, and have velocity V when reaching there. Acceleration is applied each small time step, e.g. 60 times per second.
So using the equation of motion x = v t + 0.5 a t^2, solve it for t with positive/negative acceleration applied, to see how fast one can reach x, regardless of end speed.
Also calculate how long it would take to reach V by applying the positive or negative acceleration. Whichever t is the highest, is the positive or negative acceleration to apply.
This causes objects to smoothly follow pahts such as sin(x), and if they are thrown off, they will elegantly slide back into it.

How to simulate Mouse Acceleration?

I've written iPhone - Mac, Client - Server app that allows to use mouse via touchpad.
Now on every packet sent I move cursor by pecific amount of pixels (now 10px).
It isn't accurate. When i change sensitivity to 1px it's to slow.
I am wondering how to enhance usability and simulate mouse acceleration.
Any ideas?
I suggest the following procedure:
ON THE IPHONE:
Determine the distance moved in x and y direction, let's name this dx and dy.
Calculate the total distance this corresponds to: dr = sqrt(dx^2+dy^2).
Determine how much time has passed, and calculate the speed of the movement: v = dr/dt.
Perform some non-linear transform on the velocity, e.g.: v_new = a * v + b * v^2 (start with a=1 and b=0 for no acceleration, and then experiment for optimal values)
Calculate a new distance: dr_new = v_new * dt.
Calculate new distances in x/y direction:
dx_new = dx * dr_new / dr and dy_new = dy * dr_new / dr.
Send dx_new and dy_new to the Mac.
ON THE MAC:
Move the mouse by dx_new and dy_new pixels in x/y direction.
NOTE: This might jitter a lot, you can try averaging the velocity after step (3) with the previous two or three measured velocities if it jitters to much.

gravity simulation

I want to simulate a free fall and a collision with the ground (for example a bouncing ball). The object will fall in a vacuum - an air resistance can be omitted. A collision with the ground should causes some energy loss so finally the object will stop moving. I use JOGL to render a point which is my falling object. A gravity is constant (-9.8 m/s^2).
I found an euler method to calculate a new position of the point:
deltaTime = currentTime - previousTime;
vel += acc * deltaTime;
pos += vel * deltaTime;
but I'm doing something wrong. The point bounces a few times and then it's moving down (very slow).
Here is a pseudocode (initial pos = (0.0f, 2.0f, 0.0f), initial vel(0.0f, 0.0f, 0.0f), gravity = -9.8f):
display()
{
calculateDeltaTime();
velocity.y += gravity * deltaTime;
pos.y += velocity.y * deltaTime;
if(pos.y < -2.0f) //a collision with the ground
{
velocity.y = velocity.y * energyLoss * -1.0f;
}
}
What is the best way to achieve a realistic effect ? How the euler method refer to the constant acceleration equations ?
Because floating points dont round-up nicely, you'll never get at a velocity that's actually 0. You'd probably get something like -0.00000000000001 or something.
you need to to make it 0.0 when it's close enough. (define some delta.)
To expand upon my comment above, and to answer Tobias, I'll add a complete answer here.
Upon initial inspection, I determined that you were bleeding off velocity to fast. Simply put, the relationship between kinetic energy and velocity is E = m v^2 /2, so after taking the derivative with respect to velocity you get
delta_E = m v delta_v
Then, depending on how energyloss is defined, you can establish the relationship between delta_E and energyloss. For instance, in most cases energyloss = delta_E/E_initial, then the above relationship can be simplified as
delta_v = energyloss*v_initial / 2
This is assuming that the time interval is small allowing you to replace v in the first equation with v_initial, so you should be able to get away with it for what your doing. To be clear, delta_v is subtracted from velocity.y inside your collision block instead of what you have.
As to the question of adding air-resistance or not, the answer is it depends. For small initial drop heights, it won't matter, but it can start to matter with smaller energy losses due to bounce and higher drop points. For a 1 gram, 1 inch (2.54 cm) diameter, smooth sphere, I plotted time difference between with and without air friction vs. drop height:
For low energy loss materials (80 - 90+ % energy retained), I'd consider adding it in for 10 meter, and higher, drop heights. But, if the drops are under 2 - 3 meters, I wouldn't bother.
If anyone wants the calculations, I'll share them.

Vertical circular motion : time(x/y) versus velocity equation

I wanted to simulate the following through animation :
A ball starts with a certain velocity at the bottom most point of
a vertical circular loop and keeps rolling in it until its velocity permits.
For this, I wanted to find velocity/x/y vs. time equation.
For e.g. if the ball had mass : 5Kg, radius of the circular loop = 10m,
and initial velocity of the ball is 200 m/s, what will its velocity and (x,y) position
be after 5 seconds?
thanks.
Sliding, frictionless case with a point-particle ball
In this case we aren't worrying about rotational energy and are assuming that the ball is actually a point particle. Then, in order for the ball to stay on at the top, the centripetal force condition has to be satisfied:
m * v_top^2 / r = m * g
so
v_top = sqrt(r * g)
So the minimum initial velocity is determined by:
1 / 2 * m * v0^2 >= 1 / 2 * m * v_top^2 + m * g * 2 * r
v0 >= sqrt(5 * r * g)
This is similar to what Pete said, except that he forgot the centripetal force condition to stay on at the top.
Next, the acceleration tangential to the track is given by:
a = - g * sin(theta)
but a = r * alpha = r * d^2(theta)/dt^2 where alpha is the rotational acceleration. Thus, we get
r * d^2(theta)/dt^2 = g * sin(theta)
However, I don't know of an analytical solution to this differential equation and Mathematica was stumbling with finding one too. You can't just move the dts to the other side and integrate because theta is a function of t. I would recommend solving it by numerical means such as a Runga-Kutte or maybe the Verlet method. I solved it using Mathematica for the parameters you gave, but with the ball moving so quickly, it doesn't really slow down much in going around. When I lowered the initial velocity though, I was able to see the speeding up and slowing down by plotting theta as a function of time.
Adding in other things like a finite ball radius, rotational energy and friction are certainly doable, but I would worry about being able to solve this first case before moving on because it only gets more complicated from here. By the way, with the friction you will have to choose some kinetic coefficient of friction for your given materials which will of course be proportional to the normal force exerted on the ball by the track which can be solved for by summing the force components along the radius of the circle and don't forget to include the centripetal force condition.
If you haven't done this sort of physics before, I definitely recommend getting a introductory good book on physics (with calculus) and working through it. You only need to bother with the sections that apply to mechanics though that is a very large section of the book probably. There might be better routes to pursue though like some of the resources in this question.
If there are no acceleration (x,y) =(xstart+ vx*time ,ystart + vy*time) and speed remain the same, and it is not related to the radius
Since the velocity is constant you will have an angular velocity of omega = vel / radius. You will obtain how many radians you ball will move per second over its circular path.
To get the position at time t you just have to exploit polar coordinates:
x = x_center + sin( 3/2*PI + omega*t)*radius
y = y_center + cos( 3/2*PI + omega*t)*radius
This because you start from bottom point of the circle (so its 3/2*PI) plus how many radiants you move every second (we obtained it from tangential velocity). All multiplied for the radius, otherwise you will consider a unity circle.
EDIT: Since you wonder how to find a position of an object that is subject to many different forces I can tell you that usually a physical engine doesn't care about finding equations of moving objects. It just applies forces to objects considering their intended motions (like your circular one) or environmental factors (like gravity or friction) and calculates coordinates step by step by applying forces and using an integrator to see the results.
Ignoring friction, the forces on the ball are gravity and the track.
First, there are two main cases - is the velocity enough for the ball to loop-the-loop or not:
initial energy = 1/2 m v² = 0.5 * 5 * 200 * 200
potential energy = m g h = 5 * 9.8 * 20
so it will go round the whole loop.
Initially the ball is at the bottom of the loop, theta = 0
The acceleration on the ball is the component of g along the track
a = g⋅sin theta
The distance travelled is theta * radius. It is also the double integral of acceleration against time.
theta ⋅ radius = double integral of acceleration against time
Integrating acceleration once gives velocity, integrating velocity gives distance.
so solve this for t:
theta ⋅ r = ∫(∫ g⋅sin theta.dt).dt
then your x and y are trivial functions of theta.
Whether you solve it analytically or numerically is up to you.
With dynamic friction, friction is usually proportional to the normal force on the bodies. So this will equal the centripetal force - proportional to the square of the angular velocity, and the component of gravity normal to the track (g sin theta)
You didn't tell anything about how you want your velocity to change. Do you have any friction model? If there is no friction, then the formulas are simple:
length = velocity*t
x = sin(length)*radius
y = -cos(length)*radius
If the velocity is changing, then you have to change length to something like
length = integral over dt[0..t] (velocity dt)
The only thing I wanted to add is the if this is real ball (sphere) with mass 5kg then it must have a diameter dia=(6*m/(PI*rho))^(1/3) where rho is the density of the material. For steel (rho=7680) the diameter is dia=0.1075 meters. Therefore the pitch radius (radius at which the center of gravity of the ball rides on) is equal to R=10-(dia/2) or R=9.9466 meters.
The problem gets a little more complex when friction is included. For one you have to consider the direction of friction (assuming dry friction theory). That depends on the amount the ball rotates in its axis and that depends on moment of inertia of the ball.
When you do the simulation you might want to monitor the total kinetic energy + the total potential energy and make sure your are not adding energy to the system (or taking away). [Don't forget to include the rotational component for the kinetic energy]
Get a standard book on dynamics, and I am sure a similar problem is already described in the book.I would recommend "Vector Mechanic for Engineers - Dynamics".