Car simulation (top down) following a path

Official forum for the Chipmunk Physics Library.

Re: Car simulation (top down) following a path

Postby slembcke on Fri Feb 05, 2010 2:54 pm

Same problem. I just have XP SP3 running under a VM.

A video of the issue might be just as good if you can make one of those easily.
I'm the guy that wrote Chipmunk. Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 1485
Joined: Tue Aug 14, 2007 7:13 pm

Re: Car simulation (top down) following a path

Postby carlessr on Fri Feb 05, 2010 6:07 pm

Ok, I will try and come up with something. I don't want to waste any more of your time, you've already been extremly helpful.

Thanks
Rich
carlessr
 
Posts: 18
Joined: Wed Jan 27, 2010 3:15 pm

Re: Car simulation (top down) following a path

Postby Tam Toucan on Fri Feb 05, 2010 6:14 pm

Just to say I get the same problem and I'm not running under a VM. Windows XP SP3, 32bit.
User avatar
Tam Toucan
 
Posts: 138
Joined: Tue Jun 23, 2009 4:26 pm

Re: Car simulation (top down) following a path

Postby carlessr on Sat Feb 06, 2010 4:02 am

Don't suppose you tried compiling it? Wonder if windows 7 has done something mad to the exe ..
carlessr
 
Posts: 18
Joined: Wed Jan 27, 2010 3:15 pm

Re: Car simulation (top down) following a path

Postby Tam Toucan on Sat Feb 06, 2010 4:39 am

Sorry I use cygwin, I'm a gcc person. I like my code to be portable ;)
User avatar
Tam Toucan
 
Posts: 138
Joined: Tue Jun 23, 2009 4:26 pm

Re: Car simulation (top down) following a path

Postby carlessr on Sat Feb 06, 2010 5:25 am

Occured to me last night, the reason it does't work is because it's debug. And unless you had VS2008 installed it wouldn't work (as the debug dlls aren't distributed).

So here's a release version.

http://www.sundaystudios.co.uk/chiptestrelease.zip

Cheers
Rich
carlessr
 
Posts: 18
Joined: Wed Jan 27, 2010 3:15 pm

Re: Car simulation (top down) following a path

Postby slembcke on Sat Feb 06, 2010 12:01 pm

Ok, that one worked. So is the one issue that if you click to the side of it it just rocks back and forth? That's because it has a particular turning radius and a drives forward or backwards depending on the location of the desired direction. If you set the maxBias of the gear joint to be higher then it will have a smaller turning radius.
I'm the guy that wrote Chipmunk. Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 1485
Joined: Tue Aug 14, 2007 7:13 pm

Re: Car simulation (top down) following a path

Postby carlessr on Mon Feb 08, 2010 3:36 pm

Hi,

Sorry for the delayed response (winter bugs!).

Anyhow I've been tweaking the numbers and I've got a pretty good sim going.

Here's my remaining queries;

- If I have additional vehicles (also controlled with a pivot and a gear) and they collide, no angular momentum is transferred between these bodies, is there a way to restore this?

- Additional dynamic objects in the world are under no effect from friction (as to be expected), I don't need to control these objects with a rotater, however I'd like them to slow down once bumped into. Is there an easy way to achieve friction for dynamic objects?


Thanks again
Rich
carlessr
 
Posts: 18
Joined: Wed Jan 27, 2010 3:15 pm

Re: Car simulation (top down) following a path

Postby slembcke on Mon Feb 08, 2010 5:22 pm

1) If both vehicles have the same amount of friction on them, it might be difficult to notice the effect. If you want things to spin in crazy ways, you are going to have to turn friction way down.

2) The objects in my demo have the same top down friction as the tank by using a slightly modified joint setup.
I'm the guy that wrote Chipmunk. Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 1485
Joined: Tue Aug 14, 2007 7:13 pm

Re: Car simulation (top down) following a path

Postby Spoonsx21 on Fri Mar 12, 2010 5:53 pm

Hello everyone,

I'm aware that this is a dead topic, but I personally found this post very helpful for setting up a similar path following sequence in my iphone program.

In my attempt to get what I wanted to work, I made a demo for the iphoneChipmunk xcode project. Since I don't know where to host this information, I'm going to post my Tank.m, and iphoneChipmunk.m files below. I'm sorry if this is against forum rules or what have you i was only able to attach the chipmunkdemo.h file i modified (only to make the reference ticks public instead of private.

Please note, this took a little bit of hackery to implement. So, you have to change the iphoneChipmunk.m file, go to the initialize function and replace it with this
Code: Select all
+ (void)initialize
{
   static BOOL done = FALSE;
   if(done) return;
   
   demos = [[NSArray alloc] initWithObjects:
      [OneWay class],
      [Tumble class],
      [TheoJansen class],
      [Springies class],
//      [PyramidTopple class],
      [Joints class],
      [Bounce class],
      [PyramidStack class],
      [Plink class],
      [Planet class],
      [Sensors class],
      [Tank class],
      nil
   ];
   
   done = TRUE;
}


Then download and replace the ChipmunkDemo.h file with the one attached to this post (if this doesn't work for some reason, just add "@interface Tank : ChipmunkDemo; @end" at the end of the file, and change the private variables to public".

The actual demo Tank.m is below. It's just like Tank.c, except it uses the objective wrappers to interact with chipmunk, and the tank will go to the point you touch.

Code: Select all
#import "ChipmunkDemo.h"

static ChipmunkBody *tankBody;
static ChipmunkBody *tankControlBody;
static cpFloat frand(void){return (cpFloat)rand()/(cpFloat)RAND_MAX;}

@implementation Tank



-(void)update
{
   
   
   for(int i=0; i<refTicks; i++){
      // turn the control body based on the angle relative to the actual body
      cpVect mouseDelta = cpvsub(mousePoint, tankBody.pos);
      cpFloat turn = cpvtoangle(cpvunrotate(tankBody.rot, mouseDelta));
      [tankControlBody setAngle: tankBody.angle - turn];
      
      // drive the tank towards the mouse
      if(cpvnear(mousePoint, tankBody.pos, 30.0)){
         tankControlBody.vel = cpvzero; // stop
      } else {
         cpFloat direction = (cpvdot(mouseDelta, tankBody.rot) > 0.0 ? 1.0 : -1.0);
         tankControlBody.vel = cpvrotate(tankBody.rot, cpv(30.0f*direction, 0.0f));
      }
   }
   [super update];
   
}
static cpVect
rand_pos(cpFloat radius)
{
   cpVect v;
   do {
      v = cpv(frand()*(640 - 2*radius) - (320 - radius), frand()*(480 - 2*radius) - (240 - radius));
   } while(cpvlength(v) < 100.0f);
   
   return v;
}

- (ChipmunkBody*)addBox:(cpFloat) size withMass:(cpFloat) mass
{
   
   cpVect verts[] = {
      cpv(-size,-size),
      cpv(-size, size),
      cpv( size, size),
      cpv( size,-size),
   };
   
   cpFloat radius = cpvlength(cpv(size, size));
   ChipmunkBody *body = [space add:[ChipmunkBody bodyWithMass:mass andMoment: cpMomentForPoly(mass, 4, verts, cpvzero)]];
   
   body.pos = rand_pos(radius);//cpv(frand()*(640 - 2*radius) - (320 - radius), frand()*(480 - 2*radius) - (240 - radius));
   
   ChipmunkShape *shape = [space add:[ChipmunkPolyShape polyWithBody:body count:4 verts:verts offset:cpvzero]];
   
   shape.elasticity = 0.0f; shape.friction = 0.7f;
   return body;
   
}





- (void)setupSpace
{
   
   space = [[ChipmunkSpace alloc] init];

   ChipmunkBody *staticBody = [space add:[ChipmunkBody staticBody]];// = cpBodyNew(INFINITY, INFINITY);
   
   
   
   [space resizeActiveHashWithDim:30.0f andCount:1000];
   space.iterations = 10;
   
   ChipmunkShape *shape;
   
   // Create segments around the edge of the screen.
   shape = [space add: [ChipmunkStaticSegmentShape segmentWithBody:staticBody from:cpv(-320,-240) to:cpv(-320,240) radius:0.0f]];
   shape.elasticity = 1.0f; shape.friction = 1.0f;
   shape.layers = NOT_GRABABLE_MASK;
   shape = [space add: [ChipmunkStaticSegmentShape segmentWithBody:staticBody from: cpv(320,-240) to: cpv(320,240) radius:0.0f]];
   shape.elasticity = 1.0f; shape.friction = 1.0f;
   shape.layers = NOT_GRABABLE_MASK;
   
   shape = [space add: [ChipmunkStaticSegmentShape segmentWithBody:staticBody from: cpv(-320,-240) to: cpv(320,-240) radius:0.0f]];
   
   shape.elasticity = 1.0f; shape.friction = 1.0f;
   shape.layers = NOT_GRABABLE_MASK;
   shape = [space add: [ChipmunkStaticSegmentShape segmentWithBody:staticBody from: cpv(-320,240) to: cpv(320,240) radius:0.0f]];
   shape.elasticity = 1.0f; shape.friction = 1.0f;
   shape.layers = NOT_GRABABLE_MASK;
   
   ChipmunkBody* body;
   for(int i=0; i<50; i++){
      body  = [self addBox: 10.0f withMass:1.0f];
      
      ChipmunkConstraint *pivot = [space add:[ChipmunkPivotJoint pivotJointWithBodyA:staticBody bodyB:body anchr1:cpvzero anchr2:cpvzero  ]];
      pivot.biasCoef = 0.0f; // disable joint correction
      pivot.maxForce = 1000.0f; // emulate linear friction
      
      ChipmunkGearJoint *gear = [space add:[ChipmunkGearJoint gearJointWithBodyA:staticBody bodyB:body phase:0.0f ratio:1.0f ]];
      gear.biasCoef = 0.0f; // disable joint correction
      gear.maxForce = 5000.0f; // emulate angular friction
   }
   
   // We joint the tank to the control body and control the tank indirectly by modifying the control body.
   tankControlBody = [space add:[ChipmunkBody staticBody]];
   tankBody = [self addBox: 15.0f withMass:10.0f];
   
   ChipmunkConstraint *pivot = [space add:[ChipmunkPivotJoint pivotJointWithBodyA:tankControlBody bodyB:tankBody anchr1:cpvzero anchr2:cpvzero  ]];
   pivot.biasCoef = 0.0f; // disable joint correction
   pivot.maxForce = 10000.0f; // emulate linear friction
   
   ChipmunkGearJoint *gear = [space add:[ChipmunkGearJoint gearJointWithBodyA:tankControlBody bodyB:tankBody phase:0.0f ratio:1.0f ]];
   gear.biasCoef = 1.0f; // limit angular correction rate
   gear.maxBias = 1.0f; // limit angular correction rate
   gear.maxForce = 500000.0f; // emulate angular friction
   
   
}


@end
Attachments
ChipmunkDemo.h
Demo.h file was modified slightly
(1.2 KB) Downloaded 7 times
Spoonsx21
 
Posts: 2
Joined: Fri Mar 12, 2010 5:39 pm

Previous

Return to Chipmunk Physics

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron