I'm looking for a good way to make one body attract other bodies like a gravitational field. I am wondering if there is some built in way to do this or am I going to need check proximity and do impulses per body?
-Jedd
Bodies attracting bodies
-
- Posts: 1
- Joined: Mon Jul 13, 2009 2:57 pm
- Contact:
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Bodies attracting bodies
No, there is no built in way to do it, and yes you will have to apply forces.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 2
- Joined: Sat Jul 18, 2009 6:22 pm
- Contact:
Re: Bodies attracting bodies
I've been thinking about doing the same thing,
I iterated over two loops
Ball is just a class that has a cpBody property - "ballBody." "bodies" is an array of all of the bodies on the screen. I call this in my main game loop "tick" :
and then later on apply the netForce to each body.
This seems to work, except, I'm getting an outrageous increase in speed over time. I do apply cpBodyResetAllForces before hand, and the netForces all add to 0, so I don't know where the crazy increase in energy is coming from.
Hope this helps, and if anybody has anything to say on it, it is appreciated.
I iterated over two loops
Ball is just a class that has a cpBody property - "ballBody." "bodies" is an array of all of the bodies on the screen. I call this in my main game loop "tick" :
Code: Select all
for (int i = 0; i < [bodies count]; i++)
{
Ball *bodyi = [bodies objectAtIndex:i];
for (int j = i + 1; j < [bodies count]; j++) //notice that iteration goes from i+1, not 0
{
Ball *bodyj = [bodies objectAtIndex:j];
cpVect displacement = cpvsub(bodyi.ballBody->p, bodyj.ballBody->p); //get distance between the two bodies
cpVect normDisplacement = cpvnormalize(displacement); //normalize this
float forceScale = -400000* bodyi.ballBody->m * bodyj.ballBody->m / (cpvlengthsq(displacement));
//and multiply it by the magnitude of the force
cpVect force = cpvmult(normDisplacement, forceScale);
bodyi.netForce = cpvadd(bodyi.netForce, force);
bodyj.netForce = cpvsub(bodyj.netForce, force);
}
}
This seems to work, except, I'm getting an outrageous increase in speed over time. I do apply cpBodyResetAllForces before hand, and the netForces all add to 0, so I don't know where the crazy increase in energy is coming from.
Hope this helps, and if anybody has anything to say on it, it is appreciated.
-
- Posts: 2
- Joined: Sat Jul 18, 2009 6:22 pm
- Contact:
Re: Bodies attracting bodies
lol.
My... solution... if you can call it that,
My... solution... if you can call it that,
Code: Select all
for (int i = 0; i < [bodies count]; i++)
{
Ball *bodyi = [bodies objectAtIndex:i];
if (cpvlengthsq(bodyi.netForce) > 2000000) {
bodyi.netForce = cpvmult(bodyi.netForce, 1/1.2);
if (cpvlengthsq(bodyi.netForce) > 2500000) {
bodyi.netForce = cpvmult(bodyi.netForce, 1/1.5);
if (cpvlengthsq(bodyi.netForce) > 3000000) {
bodyi.netForce = cpvmult(bodyi.netForce, 1/1.8);
if (cpvlengthsq(bodyi.netForce) > 3500000) {
bodyi.netForce = cpvmult(bodyi.netForce, 1/2.5);
if (cpvlengthsq(bodyi.netForce) > 4500000) {
bodyi.netForce = cpvmult(bodyi.netForce, 1/2.9);
if (cpvlengthsq(bodyi.netForce) > 9500000) {
bodyi.netForce = cpvmult(bodyi.netForce, 0);
}
}
}
}
}
}
cpBodyApplyForce(bodyi.ballBody, bodyi.netForce, CGPointZero);
}
Who is online
Users browsing this forum: No registered users and 8 guests