Thursday, November 10, 2011

SlingShot Direction

This week i have been trying to figure out what i considered to be the hardest part of my project, so i decided to go back to basics, "vector math" thats right. I was trying to figure out the shooting direction of a slingshot based on the player start touch and end touch point. What I did was to get the start point for the bubble that is been dragged at the beginning of the touch and then get the last position of this bubble at the end of the touch. Then take the start position subtract the last position, then take the difference and calculate the magnitude. Divide the difference position with the magnitude that gives me the direction I which the bubble should go after it is released. Since I plan on using constant velocity, then take this constant velocity value and multiply it with the direction in which that bubble should go, and add it to the current position of the bubble that will make it move in that direction. This is the simplest physics, because initial I wanted to use Chipmunk but it come with a lot of files and this came is simple and I wanted to be as simple as possible but at the same time make the game functional and fun. I know this may sound easy to do others but hey i am learning this stuff. If this not the right way please give me a better advice.

-(void) setShotDirection

{

CGPoint v;

float xm;

float ym;

float magnitude;

v.x = startBlockLastPos.x - endBlockLastPos.x;

v.y = startBlockLastPos.y - endBlockLastPos.y;

xm = v.x*v.x;

ym = v.y*v.y;

magnitude = sqrt((xm+ym));

if (magnitude != 0)

{

shotDirection.x = (v.x/magnitude);

shotDirection.y = (v.y/magnitude);

NSLog(@"mag: %f shotdirection(%f,%f)\n", magnitude, shotDirection.x, shotDirection.y);

}

}

So long and stay thirst my friends


No comments:

Post a Comment