gridsizeを使用してテクスチャを直接分割することにより、テクスチャ上にグリッドを作成しました。テクスチャを10x10に分割しました。目標は、指を使用してテクスチャ画像を変更することです。デバイスの中央で移動すると、網膜以外のデバイスと網膜デバイスで問題が発生します。たとえば、左下のあるポイントも移動します。なぜこれが起こるのかわからない。
- (id)init
{
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init]))
{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
if ([CocosDistort isRetinaDisplay])
originalImage = [UIImage imageNamed:@"IMG_1968-hd.PNG"];
else
originalImage = [UIImage imageNamed:@"IMG_1968.PNG"];
texture2D = [[CCTexture2D alloc] initWithImage:originalImage];
[self body_init];
self.isTouchEnabled = YES;
}
return self;
}
- (void)draw
{
glDisableClientState(GL_COLOR_ARRAY);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4ub(224,224,244,200);
[self body_redraw];
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
glEnableClientState(GL_COLOR_ARRAY);
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
isFirstTouch=YES;
CGPoint location = [self convertTouchToNodeSpace: touch];
if ([CocosDistort isRetinaDisplay])
{
mousex = (location.x * 2);
mousey = (location.y * 2);
}
else
{
mousex = location.x ;
mousey = location.y ;
}
firstPoint=location;
grab = [self body_grab:mousex:mousey];
return YES;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
isFirstTouch=NO;
CGPoint location = [self convertTouchToNodeSpace: touch];
mousex1 = 0.0;
mousey1 = 0.0;
if ([CocosDistort isRetinaDisplay])
{
mousex1 = (location.x * 2) - mousex;
mousey1 = (location.y * 2) - mousey ;
}
else
{
mousex1 = location.x - mousex;
mousey1 = location.y - mousey ;
}
if ([CocosDistort isRetinaDisplay])
{
mousex = (location.x * 2);
mousey = (location.y * 2);
}
else
{
mousex = location.x ;
mousey = location.y ;
}
[self body_dynamics:mousex:mousey];
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
grab = -1;
}
- (void)body_dynamics:(int)x:(int)y
{
if (mass[grab].x[0] > x && mass[grab].x[1] > y)
{
grab2 = grab - GRID_SIZE_X;
grab3 = grab2 - 1;
grab4 = grab - 1;
}
if (mass[grab].x[0] > x && mass[grab].x[1] < y)
{
grab2 = grab - GRID_SIZE_X;
grab3 = grab2 + 1;
grab4 = grab + 1;
}
if (mass[grab].x[0] < x && mass[grab].x[1] < y)
{
grab2 = grab + GRID_SIZE_X;
grab3 = grab2 + 1;
grab4 = grab + 1;
}
if (mass[grab].x[0] < x && mass[grab].x[0] > y)
{
grab2 = grab + GRID_SIZE_X;
grab3 = grab2 - 1;
grab4 = grab - 1;
}
if (grab != -1 && !mass[grab].nail &&!isFirstTouch)
{
mass[grab].x[0] = mass[grab].x[0] + mousex1;
mass[grab].x[1] = mass[grab].x[1] + mousey1;
mass[grab].x[2] = -(CLIP_FAR - CLIP_NEAR)/4.0;
mass[grab2].x[0] = mass[grab2].x[0] + mousex1;
mass[grab2].x[1] = mass[grab2].x[1] + mousey1;
mass[grab2].x[2] = -(CLIP_FAR - CLIP_NEAR)/4.0;
mass[grab3].x[0] = mass[grab3].x[0] + mousex1;
mass[grab3].x[1] = mass[grab3].x[1] + mousey1;
mass[grab3].x[2] = -(CLIP_FAR - CLIP_NEAR)/4.0;
mass[grab4].x[0] = mass[grab4].x[0] + mousex1;
mass[grab4].x[1] = mass[grab4].x[1] + mousey1;
mass[grab4].x[2] = -(CLIP_FAR - CLIP_NEAR)/4.0;
}
}
- (int)body_grab:(int)x:(int)y
{
float dx[2];
float d;
float min_d;
float min_i;
int i;
for (i = 0; i < GRID_SIZE_X*GRID_SIZE_Y; i++)
{
dx[0] = mass[i].x[0] - x;
dx[1] = mass[i].x[1] - y;
d = sqrt(dx[0]*dx[0] + dx[1]*dx[1]);
if (i == 0 || d < min_d)
{
min_i = i;
min_d = d;
}
}
return min_i;
}
- (void)body_redraw
{
int k;
int i, j;
if(mass == NULL)
return;
glBindTexture(GL_TEXTURE_2D, [texture2D name]);
k = 0;
for (i = 0; i < GRID_SIZE_X - 1; i++)
{
for (j = 0; j < GRID_SIZE_Y - 1; j++)
{
GLfloat vertices[]= {
mass[k].x[0],mass[k].x[1],mass[k].x[2],
mass[k + 1].x[0],mass[k + 1].x[1],mass[k + 1].x[2],
mass[k + GRID_SIZE_Y + 1].x[0],mass[k + GRID_SIZE_Y + 1].x[1],
mass[k + GRID_SIZE_Y + 1].x[2],
mass[k + GRID_SIZE_Y].x[0],mass[k + GRID_SIZE_Y].x[1],mass[k + GRID_SIZE_Y].x[2]
};
GLfloat tex[]={
mass[k].t[0], mass[k].t[1],
mass[k + 1].t[0], mass[k + 1].t[1],
mass[k + GRID_SIZE_Y + 1].t[0], mass[k + GRID_SIZE_Y + 1].t[1],
mass[k + GRID_SIZE_Y].t[0], mass[k + GRID_SIZE_Y].t[1]
};
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, tex);
glDrawArrays(GL_LINE_STRIP, 0,4);
k++;
}
k++;
}
}
- (void)body_init
{
GLint width = texture2D.contentSizeInPixels.width;
GLint height = texture2D.contentSizeInPixels.height;
int i, j, k;
if (mass == NULL)
{
mass = (MASS *) malloc(sizeof(MASS)*GRID_SIZE_X*GRID_SIZE_Y);
if (mass == NULL)
{
fprintf(stderr, "body: Can't allocate memory.\n");
exit(-1);
}
}
k = 0;
for (i = 0; i < GRID_SIZE_X; i++)
for (j = 0; j < GRID_SIZE_Y; j++)
{
mass[k].nail = (i == 0 || j == 0 || i == GRID_SIZE_X - 1
|| j == GRID_SIZE_Y - 1);//value is 0/1
mass[k].x[0] = i/(GRID_SIZE_X - 1.0)*width;
NSLog(@"mass[%d].x[0]:: %f",k,mass[k].x[0]);
mass[k].x[1] = j/(GRID_SIZE_Y - 1.0)*height;
NSLog(@"mass[%d].x[1]:: %f",k,mass[k].x[1]);
mass[k].x[2] = -(CLIP_FAR - CLIP_NEAR)/4.0;
NSLog(@"mass[%d].x[2]:: %f",k,mass[k].x[2]);
mass[k].v[0] = 0.0;
mass[k].v[1] = 0.0;
mass[k].v[2] = 0.0;
mass[k].t[0] = i/(GRID_SIZE_X - 1.0);
mass[k].t[1] = j/(GRID_SIZE_Y - 1.0);
k++;
}
}
}