Rotating a bike midair problems

Official forum for the Chipmunk2D Physics Library.
Post Reply
Toolism_spokko
Posts: 4
Joined: Wed Aug 31, 2011 10:40 am
Contact:

Rotating a bike midair problems

Post by Toolism_spokko »

Hello there Chipmunk community!:)

I'm currently working on code that gets a motorcycle moving and rotating in mid air. Im using Objective-Chipmunk.
The Body is basically the simple vehicle from the chipmunk joint examples. I have the chassis and 2 wheels, and the wheels are connected with the chassis using GrooveJoints and DampingSprings.

I make the motorcycle accelerate by applying a force that is always pointing in the chassis rotation vector. This is done on touch and when i release the touch I am just applying a force of 0 to make the body stop accelerating. This works rather ok.

I have a problem though when it comes to airborne rotation. When the bike jumps and is in mid air i wish to make it rotate either to the left or to the right. What is the best way to do that?
Currently I have tried the following:
- applying torque and when i no longer wish for it to rotate i set the torque to 0. (This approach yields very strange results and is definetly not working)
- increasing angular velocity by a given amount each frame when the finger is on the rotate button. When the finger is released i use the resetForces function to get rid of any excess angular velocity. This sort of works..

To summarize :
- what would be the best way to rotate an object that is currently flying through the air. I wish to rotate it only on its anchor point that is currently in the middle of the object.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Rotating a bike midair problems

Post by slembcke »

You might find it's easer to control the rotation of the bike using a cpSimpleMotor constraint than using straight torques. Set the motor's rate to the speed you want to spin the bike at (we'll call this spinRate), and find a nice value for the maxForce (we'll call this spinForce).

Normally you'd have the maxForce be 0, so it would do nothing. Then when you want to spin, you set the rate to be +/- spinRate depending non which direction you want it to spin and set the maxForce to spinForce. When you want it to stop spinning, set the rate to 0 but leave maxForce at spinForce. Experiment and see what works.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Toolism_spokko
Posts: 4
Joined: Wed Aug 31, 2011 10:40 am
Contact:

Re: Rotating a bike midair problems

Post by Toolism_spokko »

Thanks for the reply!
I did the rotation using the simple motor. But there are many problems.
I basically made the motor connect my bike chassis with the space.staticBody. Everything works great except for one thing.
After I wish the bike to stop rotating I have to set BOTH the rate and maxForce of the motor to 0. Otherwise it behaves strange.

I have another question though, not sure if I have to make another thread or ask here . But here goes anyway.

I experimented with just straight up changing the rotation angle of the chasiss to make it rotate. And it works great! (maybe not very realistic, but its all about gameplay fun not realism to me) BUT there is one problem. The wheels connected to the chassis do not follow the rotation of the chasis. Is there any way to just straight up BOLT them to the chasis so that when i change the chasis angle, the rotating chasis will enforce the wheels to also move according to its rotation?

Here is the code (I use the tilt of the device from the accelerometer to add or remove a certain radian value to the angle)

Code: Select all

/// b is the chassis body
if(!onGround)
{
	cpFloat spin = 4.0f*M_PI*1.0f/60.0f;
	cpFloat extraSpin = accelValues.y * spin;
	b.angle += extraSpin;
}
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Rotating a bike midair problems

Post by slembcke »

You mean they don't follow at all? That's a bit weird.

If it's just that follow in a mushy sort of way, it's because you aren't updating the angular velocity of the body too.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Toolism_spokko
Posts: 4
Joined: Wed Aug 31, 2011 10:40 am
Contact:

Re: Rotating a bike midair problems

Post by Toolism_spokko »

Hmm you are correct I do not update the angular velocity. How would I update it though? I'd have to make the calculations myself and I don't really know how. Would you be able to provide me (given the previous code of rotation I presented) a way to make the angular velocity follow the rotation?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Rotating a bike midair problems

Post by slembcke »

It's just distance/time. So if you add 2 radians to the angle each step and your time step is 1/27 of a second, then the angular velocity would be 2*27.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Toolism_spokko
Posts: 4
Joined: Wed Aug 31, 2011 10:40 am
Contact:

Re: Rotating a bike midair problems

Post by Toolism_spokko »

Thank you for your answer.

Can you also adress my previous question? I went through all the joints and watched the joints tutorial video. BUT I still dont see how if I have body A which is a wheel and body B which is the chassis, what joint should I use if I wanted to have the wheels fixed to the chassis? Every joint i tried always leaves some distance between the two bodies. The only thing that I found sort of working is if I use a dampenSpring with a distance of 0 and a big ass hardness value, but this causes the wheels to sometimes jump by themselves as if the spring would force the upward motion sometimes.

Here is a drawing of what I want to achieve : https://docs.google.com/drawings/d/1-V7 ... t?hl=en_US
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Rotating a bike midair problems

Post by slembcke »

A pivot joint should hold the two bodies together as solidly as is possible. I figured your problem was that you were changing the position without the velocity and making it hard for the joints to hold things together. If that isn't working then there is something else that is the problem.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests