开发者

vertices smaller than sprite when rendered on iPhone screen

开发者 https://www.devze.com 2023-03-20 06:46 出处:网络
I am using vertex helper to get the edges of my sprites. I am using Cocos2d + Box2d on the iPhone. The problem I am getting is that the debug draw on the device I am testing on comes up but the vertic

I am using vertex helper to get the edges of my sprites. I am using Cocos2d + Box2d on the iPhone. The problem I am getting is that the debug draw on the device I am testing on comes up but the vertices rendered or shown is smaller than my sprites even after tracing the edges of the sprites carefully and using the same sprite.

OK here is a screen shot of the problem!

vertices smaller than sprite when rendered on iPhone screen

The green triangle is supposed to fit with the edges of the sprite. Also this is the code I used:

This is the GLESDebugDraw code

    - (void)setupDebugDraw {
debugDraw = new GLESDebugDraw(PTM_RATIO* [[CCDirector   sharedDirector]    
contentScaleFactor]);
world->SetDebugDraw(debugDraw);
debugDraw->SetFlags(b2DebugDraw::e_shapeBit);

}

       #import "Box2DSprite.h"
    @interface Stalag :Box2DSprite {

b2World *world;
}
- (id)initWithWorld:(b2World *)world atLocation:(CGPoint)location;

@end

#import "Stalag.h"



@implementation Stalag
- (void)createBodyAtLocation:(CGPoint)location {
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
bodyDef.position = b2Vec2(location.x/PTM_RATIO,
                          location.y/PTM_RATIO);
self.body = world->CreateBody(&bodyDef);
body->SetUserData(self);
b2PolygonShape shape;


int num = 8;
b2Vec2 verts[] = {
    b2Vec2(36.8f / 100.0, -79.2f / 100.0),
    b2Vec2(10.3f / 100.0, 33.2f / 100.0),
    b2Vec2(1.8f / 100.0, 80.6f / 100.0),
    b2Vec2(-4.6f / 100.0, 84.5f / 100.0),
    b2Vec2(-8.5f / 100.0, 80.3f / 100.0),
    b2Vec2(-22.6f / 100.0, 19.4f / 100.0),
    b2Vec2(-31.8f / 100.0, -45.6f / 100.0),
    b2Vec2(-37.5f / 100.0, -75.7f / 100.0)
};
shape.Set(verts, num);

b2FixtureDef fixtureDef;
fixtureDef.shape = &shap开发者_运维技巧e;
fixtureDef.density = 1000.0;
body->CreateFixture(&fixtureDef);
}
- (id)initWithWorld:(b2World *)theWorld atLocation:(CGPoint)location {
if ((self = [super init])) {
    world = theWorld;
    [self setDisplayFrame:[[CCSpriteFrameCache                              
sharedSpriteFrameCache]     spriteFrameByName:@"hill.png"]];
    gameObjectType = kStalagupType;
    [self createBodyAtLocation:location];
}
return self;
   }
    @end

Please can anyone tell me what do I do and what I am doing wrong?

Thanks


When specifying vertices coordinates you are dividing it by a "magic number" 100. Actually it is your PixelToMetersRatio (PTM_RATIO in cocos examples). Also you are using box2d debug draw. And when creating debug draw class your must pass PTM_RATIO in it's constructor. I think you are passing another number there (not 100). Does it solve the problem ?

0

精彩评论

暂无评论...
验证码 换一张
取 消