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


Thursday, November 3, 2011

UnBlock Me Game

This is the function I wrote that is doing the snapping
The endFoundBlockPos position is set when the touchEnds and the
startFoundBlockPos is set when we start the touch and we found the
block before you move the block get the starting position.
Also these values should be assigned according to the direction in
which the block is moving. If the block is moving verticaly for
example startFoundBlockPos = FoundBlock.y and when the touch ends
endFoundBlockPos = FoundBlockPos.y; then I get the difference of the
two. The take the difference divide it with 50, since we know that the
the field is 300 by 300 and out blocks are have the maximum width and
height of 150. So if I divide the difference by 50 and there is no
remeinder I know that the block has been moved by 50 or 100 or 150
then I just need to add the difference to the startFoundBlockPos
andset the block pos to that depending on the direction it is moving.
If there is a remender I have to check how much is the remeinder if
the remeinder is less than 24 I just ignore it if it is greater than
24 I take the difference divide by 50 muiltiply by 50 then add
startFoundBlockPos plus 50. Any way go through the code and try to
understand it.. This function is called as soon as the touchends in
the touchends function and after you set endFoundBlockPos but before
you set the FoundBlock = 0;

Like this:
endFoundBlockPos = FoundBlock.x or y depending on direction.
[self.snapBlock: FoundBlock];
FoundBlock = 0;
endFoundBlockPos = 0;
startFoundBlockPos = 0;

-(void) snapBlock:(Block *) inBlock
{
int diff;
float foundBlockNewPos;

diff = (endFoundBlockPos - startFoundBlockPos);

if(diff % 50 == 0)
{
foundBlockNewPos = (startFoundBlockPos + (endFoundBlockPos -
startFoundBlockPos));
NSLog(@" No remeinder");
}
else if(diff % 50 !=0 )
{
if (diff % 50 <= 24 && diff % 50 > 0)
{
if ((diff % 50) > 24)
{
foundBlockNewPos = (((diff/50)*50) + startFoundBlockPos + 50);
}
else
{
foundBlockNewPos = (((diff/50)*50) + startFoundBlockPos);
}

NSLog(@" remeinder inside less not less 0: %i\n", diff % 50);

}
else if (diff % 50 <= 24 && diff % 50 < 0)
{

if ((-(diff % 50)) > 24)
{
foundBlockNewPos = (((diff/50)*50) + startFoundBlockPos - 50);
}
else
{
foundBlockNewPos = (((diff/50)*50) + startFoundBlockPos);
}

NSLog(@" remeinder inside less 0: %i\n", diff % 50);

}
else if (diff % 50 > 24) {

foundBlockNewPos = (((diff/50)*50) + startFoundBlockPos + 50);
NSLog(@" remeinder inside greater");

}

}

if (inBlock.dir == Horiz_BLK)
{
NSLog(@" MOVING IN X");
inBlock.x = foundBlockNewPos;
}

if (inBlock.dir == Vert_BLK)
{
NSLog(@" MOVING IN Y");
inBlock.y = foundBlockNewPos;
}