Hi there,
I've been trying to move a sprite with velocity defined by a touch point, I have managed to do this using this:
(btw I'm in portrait mode)
UITouch *touch = [touches anyobject];
CGPoint loc = [touch locationInView:[touch view]];
loc = [[Director sharedDirector] convertCoordinate:loc];
CGPoint velocity = ccp(loc.x - object.position.x, loc.y - object.poistion.y);
Now this basically works, but the object's speed changes according to the touch point, and I'm looking for a constant speed.
I think this can be achieved using angles but I just can't get it. I can find the angle but I don't know how to direct the object (through velocity) to the point touched, it just goes random.
UITouch *touch = [touches anyobject];
CGPoint loc = [touch locationInView:[touch view]];
loc = [[Director sharedDirector] convertCoordinate:loc];
double degrees = (atan2(loc.x - object.position.x, loc.y - object.position.y) *180) / 3.14159265;
CGPoint velocity = ccp(kSpeed * cos(degrees), kSpeed * sin(degrees);
I tried this one, but the sprite goes random wherever it wants...
Can anyone explain how to do this and elaborate about the convertCoordinate method as it is very confusing.
Thank you in advance!