Monday, May 6, 2013

Making a Guided Missile in libGDX with Box2D

To see an example, take a look at this video:


Or this one with overhead strikes:

Finally here is a video that shows a large number of missiles chasing another missile:


To create a guided missile like this, you have to do a few things:

  1. Have a target, that is updated every frame with its current position
  2. (Optional) set some fixed waypoints on the way to the target to alter the flight path, this creates the overhead strike you see in the video, and is one way to improve accuracy
  3. Create a "steer point" that will guide your missile towards the next waypoint/target, but corrects for velocity of the chaser and the chased
  4. Turn the body of the missile until it is facing the steer point.
  5. When we are happy with the facing of the body, we accelerate it.
  6. If you want the missile to explode, you can check distance from target, or use Box2D's contact listener
Setting the targeting and updated it's Vector2 position should be easy enough, so I won't explain it here. Let's skip to step 3.

Assuming we have a target, let's face the target
let's correct for velocities: 


Now let's seek the waypoint:


And that's about it.  Here is the code for constraining the angle to 180 and -180 degrees:
Now you need to implement acceleration and contact/explosion handling on your own.

Saturday, May 4, 2013

Air Resistance in Box2D

I've seen many questions asking how to implement air resistance (drag) in box2d, and the most common solution is to use body.setLinearDamping.  After just a little bit of research, I discovered that calculating air resistance is not that difficult.  Here is an example in Java using box2d.
Note: this is not a full simulation of aerodynamics, it only affects linear velocity.
After reading the source, check out the algorithm in action in this video.

Sunday, August 19, 2012

C - counting the number of times a char appears in a string

Code: https://gist.github.com/3394648

This was a nice exercise that helped me get more familiar with malloc and pointers.  I was really happy when I got the two dimensional array pointers working on the first try!

Monday, July 23, 2012

making my own strcat() in C

As an exercise, I made my own strcat() function in C:

https://gist.github.com/3164052 for the pretty version.


#include <stdio.h>
#include <string.h>
#define STRINGSIZE 80

int cat(char *a, const char* b, const size_t max_size)
{  
    //puts("FUNCTION BEGIN");
    size_t size = strlen(a);
    a += size;
    while (*b)
    {
        if (size >= max_size)
        {  
            *a = '\0';
            printf("ERROR: function cat() has run past max_size: size = %lu\n", size);
            return -1;  // I feel like I should undo the changes
                        // since I'm returning an error.
        }
        *a++ = *b++;
        size++;
    }
    *a = '\0';
    //puts("FUNCTION FINISHED");
    return 0;
}

int main()
{
    char strA[STRINGSIZE] = "I am a small cat ";
    char strB[STRINGSIZE] = "with whiskers who likes milk.";
    printf("A: '%s'\nB: '%s'\n", strA, strB);
    puts("-= Let's call cat() =-");
    int result = cat(strA, strB, STRINGSIZE);
    if (result == -1)
        printf("main() - ERROR in function cat(): -1\n");
    printf("A: '%s'\nB: '%s'\n", strA, strB);
    printf("cat() returned: '%d'\n", result);
    return 0;
}



Sunday, July 15, 2012

Strings, Arrays, Pointers, and searching in C

After a bout of wrist pain, I'm strating to slowly ease back into programming.  Here is some C code to search through strings for words in sys args, and highlight the found words with brackets.:

Thursday, June 21, 2012

Decimal, Hexadecimal and Binary in C

A progress report on my attempt to learn C:
After doing some exercises while going through K&R, I decided to do an exercise of my own, and created a C program that converts a user-inputted decimal into hexadecimal and binary.  It was really quite fun to make.  Here is the code:


#include <stdio.h>
#include <math.h>

int main (){
    char large_text[100];
    int i = 0;
    char c;

    printf("\nInput: ");
    for (i = 0;(c = getchar()); i++){
        if ((c == '\0') | (c == 10)) {
            large_text[i] = '\0';
            break;
        }
        large_text[i] = c;
    }
    printf("The input was: ");
    int i2 = 0;
    while (i2 <= i){
        printf("%c", large_text[i2]);
        i2++;
    }
    printf("\n");

    // convert to decimal
    i2 = 0;
    int ten_to_the;
    int value;
    int decimal = 0;
    while (i2 < i){
        ten_to_the = i - 1 - i2;
        value = (large_text[i2] - '0') * (int)(pow(10,ten_to_the));
        printf("char: %c\tdec: %d\t*ten_to_the: %d\tproduct: %d\t",
            large_text[i2], large_text[i2] - '0', ten_to_the, value);
        i2++;
        decimal = decimal + value;
        printf("decimal is: %d\n", decimal);
    }

    // convert to hexadecimal
    char hex[24];
    int slice = decimal;
    int remain;
    int i3 = 0;
    while (slice > 0){
        remain = slice % 16;
        slice = slice / 16;
        switch (remain){
            case 10 : c = 'a'; break;
            case 11 : c = 'b'; break;
            case 12 : c = 'c'; break;
            case 13 : c = 'd'; break;
            case 14 : c = 'e'; break;
            case 15 : c = 'f'; break;
            default : c = remain + '0';
        }
        hex[i3] = c;
        i3++;
    }
    hex[i3] = '\0';
    // reverse the hex string for proper order
    char newhex[24];
    newhex[i3] = '\0';
    for (i = 0; (c = hex[i]); i++){
        newhex[(i3-1)-i] = c;
    }

    // convert to binary
    char bin[100];
    slice = decimal;
    remain = 0;
    i3 = 0;

    while (slice > 0){
        remain = slice % 2;
        slice = slice / 2;
        switch(remain){
            case 0 : c = '0'; break;
            case 1 : c = '1'; break;
            default : c = '!';
        }
        bin[i3] = c;
        i3++;
    }
    bin[i3] = '\0';
    // reverse the string for proper order
    char newbin[100];
    newbin[i3] = '\0';
    for (i = 0; (c = bin[i]); i++){
        newbin[(i3-1)-i] = c;
    }
    printf("==============================================\n");
    printf("Decimal:%d\tHex: 0x%s\tBinary: %s\n", decimal, newhex, newbin);
    printf("==============================================\n");
    printf("Program finished...\n");
    return 0;