Thanks, so i fixed a mistake, i had to use
cgp.y-newYPosition in all the code instead of
cgp.y, and add this condition when
cgp.y-newYPosition and
hero.position.y are both negative, or only one of the two :
Code:
float diffDist;
if (hero.position.y<0 && cgp.y-newYPosition<0) {
diffDist = fabsf(hero.position.y) - fabsf(cgp.y-newYPosition);
diffDist = fabsf(diffDist);
}
else {
diffDist = fabsf(hero.position.y - cgp.y-newYPosition);
}
So this works to find the point of destination. But he still only goes up.
I've tried both ways with :
hero.position.y+finalY and
finalY-hero.position.y, but i'm not sure to understand why you would use finalY-hero.position.y?
Here is the output of my character going up, when i ask him to go below his position : the point of destination is correct (example: -618 and -618), but he still does not want to go down towards this point :
Quote:
3 clicks down, but he always goes up (cgp.y-newYPosition is the point where i clicked) :
hero.position.y: -499.634827, cgp.y-newYPosition: -618.634827,
diffDist: 119.000000, finalY: -119.000000, hero.position.y+finalY: -618.634827, finalY-hero.position.y: 380.634827
hero.position.y: -516.035339, cgp.y-newYPosition: -662.035339,
diffDist: 146.000000, finalY: -146.000000, hero.position.y+finalY: -662.035339, finalY-hero.position.y: 370.035339
hero.position.y: -544.891785, cgp.y-newYPosition: -703.891785,
diffDist: 159.000000, finalY: -159.000000, hero.position.y+finalY: -703.891785, finalY-hero.position.y: 385.891785
Would you know why?
This is my last update :
Code:
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for( UITouch *touch in touches ) {
CGPoint location = [touch locationInView: [touch view]];
CGPoint cgp = [[CCDirector sharedDirector] convertToGL: location];
b2Vec2 locationWorld = b2Vec2(cgp.x/PTM_RATIO, cgp.y/PTM_RATIO);
float diffDist;
if (hero.position.y<0 && cgp.y-newYPosition<0) {
diffDist = fabsf(hero.position.y) - fabsf(cgp.y-newYPosition);
diffDist = fabsf(diffDist);
}
else {
diffDist = fabsf(hero.position.y - cgp.y-newYPosition);
}
//Y-AXIS
float finalY;
if (cgp.y-newYPosition > hero.position.y) finalY = fabsf(diffDist);//if touch above hero.position, impulse == up (+)
else finalY = (0 - fabsf(diffDist));//else, impulse == down (-)
//X_AXIS
float finalX = cgp.x - hero.position.x;
b2Vec2 impulseDirection = b2Vec2(finalX/PTM_RATIO, (finalY-hero.position.y)/PTM_RATIO);
//b2Vec2 impulseDirection = b2Vec2(finalX/PTM_RATIO, (hero.position.y+finalY)/PTM_RATIO);
impulseDirection.Normalize();
forceOnCharacter = 10 * hero.body->GetMass();
hero.body->ApplyLinearImpulse( forceOnCharacter*impulseDirection, hero.body->GetPosition() );
}
}
Thanks