cpBody function no const cpBody *

Discuss any Chipmunk bugs here.

cpBody function no const cpBody *

Postby pat » Sun Aug 29, 2010 2:12 pm

Hey, I donno if this would be considered a bug, but its a little annoying to have to cast const cpBody pointers to do stuff like cpBodyLocal2World. Is there any reason why things that are read only like getters and functions where theres no modification to the body not in const form?

Code: Select all
// Convert body local to world coordinates
static inline cpVect
cpBodyLocal2World(cpBody *body, cpVect v)
{
   return cpvadd(body->p, cpvrotate(v, body->rot));
}

// Convert world to body local coordinates
static inline cpVect
cpBodyWorld2Local(cpBody *body, cpVect v)
{
   return cpvunrotate(cpvsub(v, body->p), body->rot);
}


It's not really just in the cpBody functions where i've ran into this, like some of the polyshape functions take const but others don't and basically don't modify anything.
Code: Select all
// Returns the minimum distance of the polygon to the axis.
static inline cpFloat
cpPolyShapeValueOnAxis(const cpPolyShape *poly, const cpVect n, const cpFloat d)
{
   cpVect *verts = poly->tVerts;
   cpFloat min = cpvdot(n, verts[0]);
   
   int i;
   for(i=1; i<poly->numVerts; i++)
      min = cpfmin(min, cpvdot(n, verts[i]));
   
   return min - d;
}

// Returns true if the polygon contains the vertex.
static inline int
cpPolyShapeContainsVert(cpPolyShape *poly, cpVect v)
{
   cpPolyShapeAxis *axes = poly->tAxes;
   
   int i;
   for(i=0; i<poly->numVerts; i++){
      cpFloat dist = cpvdot(axes[i].n, v) - axes[i].d;
      if(dist > 0.0f) return 0;
   }
   
   return 1;
}



Or maybe this is by design to discourage pointers?

Just thought I'd bring this up since i run into it pretty often
User avatar
pat
 
Posts: 53
Joined: Tue Jul 20, 2010 7:55 pm

Re: cpBody function no const cpBody *

Postby slembcke » Sun Aug 29, 2010 9:03 pm

Just inconsistencies I guess. I don't often think to add const. I did with the vector operator functions because I wanted to ensure that they got optimized well. Is there any particular reason you are defining them as const to begin with?

In general things like shapes and bodies are not constant. Tagging getter functions maybe makes sense, but you can't do much with a read only shape or body.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: cpBody function no const cpBody *

Postby pat » Mon Aug 30, 2010 7:05 am

yea i figured optimization could be a reason for it, though i'm not sure that it would make a difference since its pointers, with the cpBodyL2W/W2L functions those cpv calls take const cpVect's (which i wouldnt think would matter in the long run since its still doing a copy).

Like i'm working on a world editor and stuff like plotting shapes i ushaully keep the body constant incase the project gets more of a team and people think they should be playing with other stuff besides whats not constant (which is really th only reason for const i'd say).

The other times when i'll use const is so that the person writing code with my code knows that that value that there sending is not going to be modified, Which i think would be a good thing in chipmunk to do for people who are new to game programming and don't know what is actually gonna be modifying stuff and not. Its pretty standard to do this with c++, i'd imagine the same with c but i don't really have the background to say that.
User avatar
pat
 
Posts: 53
Joined: Tue Jul 20, 2010 7:55 pm

Re: cpBody function no const cpBody *

Postby slembcke » Mon Aug 30, 2010 10:53 am

Well, I added const to static inline functions and macro templated functions in the headers. I think that should cover most of the cases that you were talking about. I'll have to make a deeper inspection later if I want to get all of them.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: cpBody function no const cpBody *

Postby pat » Mon Aug 30, 2010 11:48 am

Sweet, thats great. I think that should cover most everything
User avatar
pat
 
Posts: 53
Joined: Tue Jul 20, 2010 7:55 pm

Re: cpBody function no const cpBody *

Postby BLWNKL » Thu Sep 02, 2010 7:52 pm

Hi all...
In trying to learn how to use cpBodyLocal2World, I discovered that the object (sprite) I wish to use as the "target" of a projectile actually has no cpBody (since it's not included in the space because I don't want it to actually participate in collisions per se). A good way to think of this is as "an aiming point" in the space.
So I'm wondering if (based on the discussions in this thread about const cpBody objects) this approach makes sense? Or is there a better way to accomplish what I describe?
BTW, I'm struggling with the notion of "world coordinates". Are they different from the iPhone view coordinate system, where 0,0 is upper left?
Thanks in advance for any thoughts you may share.
BLWNKL
 
Posts: 3
Joined: Sat Jun 05, 2010 10:13 am

Re: cpBody function no const cpBody *

Postby ShiftZ » Fri Sep 03, 2010 5:51 am

BLWNKL
There is no such things as iPhone coordinate system, and cpBodyLocal2World doesnt concerned with screen coords. You have to have somekind of Viewport object, whitch can translate from screen coordinates to world coordinates and back taking in a count camera position, gl projection matrix ect.
ShiftZ
 
Posts: 114
Joined: Sat Mar 07, 2009 7:23 am


Return to Bugs

Who is online

Users browsing this forum: No registered users and 1 guest

cron