Box2D Forums

It is currently Sun May 19, 2013 1:42 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Wed Jun 27, 2012 9:14 pm 
Offline

Joined: Tue May 01, 2012 7:17 pm
Posts: 28
Hi,

my character always goes up, even when i ask him to go down when i click below his position.
For example, when he's at y=30 , and i click at y=12, i would like him to go to y=12 with an impulse :
hero.body->ApplyLinearImpulse( forceOnCharacter*impulseDirection...

But he still goes up. Is it because i'm using "forceOnCharacter" ? (so 12 would become 120 ?) If i remove forceOnCharacter, the player doesn't jump anymore.

Here's my code : (the calcul seems correct (see the output below) )

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];
       
        CCLOG(@"hero.position.y: %f, cgp.y-newYPosition: %f", hero.position.y, (cgp.y-newYPosition));
       
        //TOUCH/HERO Y-AXIS
        float diffDist = fabsf(hero.position.y) - fabsf(cgp.y);
        //example: -50(touch), -100(hero), ... or : -100(hero), -150(touch) : calculate diff without the sign
        float finalY;
        if (cgp.y > hero.position.y) finalY = fabsf(diffDist);//if touch above hero.position, impulse == up (+)
        else finalY = (0 - fabsf(diffDist));//else, impulse == down (-)
       
        float heroBodyY = hero.body->GetPosition().y;
       
        CCLOG(@"diffDist: %f, finalY: %f, hero.position.y+finalY: %f", diffDist, finalY, hero.position.y+finalY);
        CCLOG(@"hero.position.y %f, hero.body->GetPosition().y : %f", (hero.position.y)/PTM_RATIO, heroBodyY);
       
        //TOUCH/HERO X-AXIS
        float finalX = cgp.x - hero.position.x;
       
        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() );


and here's the output, when i click above his position, or below, he always goes up :

Quote:
UP
hero.position.y: 293.414215, cgp.y-newYPosition: 423.000000
diffDist: -129.585785, finalY: 129.585785, hero.position.y+finalY: 423.000000
hero.position.y 9.169194, hero.body->GetPosition().y : 9.169194


DOWN //he still goes up
hero.position.y: 409.867218, cgp.y-newYPosition: 61.000000
diffDist: 348.867218, finalY: -348.867218, hero.position.y+finalY: 61.000000
hero.position.y 12.808351, hero.body->GetPosition().y : 12.808351


Thanks for your help


Top
 Profile  
 
PostPosted: Wed Jun 27, 2012 9:58 pm 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
This line looks wrong:
Code:
float diffDist = fabsf(hero.position.y) - fabsf(cgp.y);

That would result in stuff like abs(-50) - abs(50) equaling 0, which is obviously not the difference between -50 and 50.

It should be like this:
Code:
float diffDist = fabsf(hero.position.y - cgp.y);


Top
 Profile  
 
PostPosted: Wed Jun 27, 2012 10:51 pm 
Offline

Joined: Tue May 01, 2012 7:17 pm
Posts: 28
Thanks, you're right, but the result is the same, i checked again, but the output seems correct : when i click above (like y=416), the finalY is correct, and hero.position.y+finalY is also correct. Same thing when i click below: the player should go to y=42. But the player still goes up.

Quote:
UP
hero.position.y: 294.703766, cgp.y-newYPosition: 416.000000
newYPosition 0.000000, cgp.y: 416.000000
diffDist: 121.296234, finalY: 121.296234, hero.position.y+finalY: 416.000000


DOWN
hero.position.y: 295.079956, cgp.y-newYPosition: 42.000000
newYPosition 0.000000, cgp.y: 42.000000
diffDist: 253.079956, finalY: -253.079956, hero.position.y+finalY: 42.000000


The code updated : (newYPosition starts when the player goes under the half of the screen in the y-axis)

Code:
for( UITouch *touch in touches ) {
      CGPoint location = [touch locationInView: [touch view]];
        CGPoint cgp = [[CCDirector sharedDirector] convertToGL: location];
       
        CCLOG(@"hero.position.y: %f, cgp.y-newYPosition: %f", hero.position.y, (cgp.y-newYPosition));
        CCLOG(@"newYPosition %f, cgp.y: %f", newYPosition, cgp.y);
       
        //TOUCH/HERO Y-AXIS
        float diffDist = fabsf(hero.position.y - cgp.y);
        //example: -50(touch), -100(hero), -150(touch) : calculate diff without the sign
        float finalY;
        if (cgp.y > hero.position.y) finalY = diffDist;//if touch above hero.position, impulse == up (+)
        else finalY = (0 - fabsf(diffDist));//else, impulse == down (-)
       
        CCLOG(@"diffDist: %f, finalY: %f, hero.position.y+finalY: %f", diffDist, finalY, hero.position.y+finalY);
       
        //TOUCH/HERO X-AXIS
        float finalX = cgp.x - hero.position.x;
       
        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() );


Any other idea?

Thanks


Top
 Profile  
 
PostPosted: Wed Jun 27, 2012 11:30 pm 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
In this line:
Code:
b2Vec2 impulseDirection = b2Vec2(finalX/PTM_RATIO, (hero.position.y+finalY)/PTM_RATIO);


Shouldn't that be finalY-hero.position.y ?

Adding to the hero's position just gives a new destination and does not give a relative direction (or distance).


Top
 Profile  
 
PostPosted: Thu Jun 28, 2012 3:37 am 
Offline

Joined: Tue May 01, 2012 7:17 pm
Posts: 28
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


Top
 Profile  
 
PostPosted: Fri Jun 29, 2012 1:07 am 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
Heh, sorry, I was thinking finalY was an absolute position, not a relative one. So, forget my finalY-hero.position.y suggestion. x]

You don't need to do that whole bit just to get the difference. You only need to do this:
Code:
diffDist = fabsf(hero.position.y - (cgp.y - newYPosition));

And that gives you the difference between the two positions (a positive value).

Anyway, I was thinking why you need to do hero.position.y + finalY, and you don't need to do that for the impulse direction; finalY is already the direction relative to the hero's position. The reason why it goes up is because hero.position.y + finalY will result in an absolute position.

So you only need to do this:
Code:
b2Vec2 impulseDirection = b2Vec2(finalX/PTM_RATIO, finalY/PTM_RATIO);


Top
 Profile  
 
PostPosted: Fri Jun 29, 2012 11:54 am 
Offline

Joined: Tue May 01, 2012 7:17 pm
Posts: 28
Quote:
diffDist = fabsf(hero.position.y - (cgp.y - newYPosition));


You're right

Quote:
finalY is already the direction relative to the hero's position


You're right

:mrgreen:

For diffDist, if i click at y=-200, or at y=-600 (if i add his position in the vector) relative to the hero,
It would still be -200 or -600 relative to the position of the hero? I would expect him to go towards -600 instead of -200, but not above his position?

Anyway, thanks for your help :mrgreen:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group