• Create Account

# Mizmoon

Member Since 02 Sep 2013
Offline Last Active Mar 25 2015 04:54 AM

### In Topic: avoid ships colliding?

29 November 2014 - 06:50 PM

I was having the exact same problem once, and my solution was to give each ship an offset vector which represented how much the ship had to deviate from its course. The vector was zeroed at the start of each AI pass, and then you go through all neighboring ships in range, and add the vector between them and your ship scaled by a factor determined by their distance to you. This means that if there is another ship at both your left and your right, and they are at equal distances, the net offset will be zero. If there is one behind you and one to your left, the offset will be to your upper right, etc. If you need the offset as an angle, it is quite easily done with some trigonometry, followed by adding the offset angle to the facing of your ship to get the new facing. You might have a problem in the case where two ships are on your front left and right, and the best path would be to pass between them (while the offset vector points backwards), but it should be possible to fix with some exceptions.

Sorry for the semi-necropost, i hope it works out for you.

### In Topic: Problem passing a constant string into a funciton in C

29 November 2014 - 05:25 PM

Thank you all for your replies. To Buckeye, i know perfectly well how to use the debug tool, sadly the debugger in CodeLite only lets you keep track of local variables rather than function arguments (perhaps it can, though i haven't found an option for it). I am not new to programming, although i am new to pointers.

Anyhow, i scrapped the linked list approach since i found that managing the order swapping would become more complicated than it needed to be. I redid everything using a pointer array approach instead, and it is working well save for a few aspects. Specifically, i am struggling with a function where i am to remove a post from the array, and put the last post in the empty space before decrementing the counter. As i am pointing to a pointer array, the levels of indirection become a bit too much for me. Here is the new code, with comments added at the relevant part:

```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXP 45
#define TRUE 1
#define FALSE 0

typedef int bool;

typedef struct post {
char fname[32];
char lname[32];
int length;
} post;

post * post_new(char *fname, char *lname, int length);
post * post_search(post **list, char *str);
bool post_remove(post **list, int *num, post *p);
void post_sort(post **list, int num);
void post_print(post *p);
void post_printall(post **list, int num);
void post_clearall(post **list, int *num);

int main(int argc, char **argv)
{
post *entries[MAXP];
post *selected = NULL;
int num_entries = 0;
int length;
bool printlist = FALSE;
bool printpost = FALSE;
bool printcommands = TRUE;
char fname[32];
char lname[32];
char input[128];

memset(entries, 0, sizeof(entries));

while(1) {
system("cls");
printf("=====================\n");
printf("=====================\n");
if (printcommands) {
printcommands = FALSE;
} else if (printpost && selected != NULL) {
post_print(selected);
printpost = FALSE;
} else if (printlist) {
post_printall(entries, num_entries);
printlist = FALSE;
}
printf("Number of contacts: %i\n",num_entries);
printf("What would you like to do?\n");
fgets(input, 128, stdin);

if (strstr(input, "-new")) {
printf("Enter first name:\n");
fgets(fname, 32, stdin);
printf("Enter last name:\n");
fgets(lname, 32, stdin);
printf("Enter length:\n");
scanf("%i", &length);
entries[num_entries] = post_new(fname, lname, length);
num_entries++;
} else if (strstr(input, "-list")) {
printlist = TRUE;
} else if (strstr(input, "-commands")) {
printcommands = TRUE;
} else if (strstr(input, "-sort")) {
post_sort(entries, num_entries);
} else if (strstr(input, "-clear")) {
post_clearall(entries, &num_entries);
} else if (strstr(input, "-remove")) {
selected = post_search(entries, input);
if (selected != NULL) {
post_remove(entries, &num_entries, selected);
} else {
printf("No matches found.");
}
}
}

return 0;
}

printf("Commands:\n");
printf("'-remove [name]' - remove best matching contact\n");
printf("'-clear' - clear the contact list\n\n");
printf("'-sort' - sort the contact list\n");
printf("'-list' - show all contacts\n");
printf("'-commands' - sort command list\n\n");
}

post * post_search(post **list, char *str) {
int input, i, n=0;
int index[MAXP];
char buffer[128];

for (i=0;i<MAXP;i++) {
if (list[i] != NULL) {
if (strstr(str, list[i]->fname) != NULL || strstr(str, list[i]->lname) != NULL) {
index[n] = i;
n++;
}
}
}

if (n>1) {
printf("There are multiple matching posts. Did you mean:\n");
for(i=0;i<n;i++) {
strcpy(buffer, list[index[i]]->fname);
strcat(buffer, " ");
strcat(buffer, list[index[i]]->lname);
printf("%i. %s",i+1,buffer);
}
scanf("%i",&input);
if (input <= n && input > 0) {
return list[index[input-1]];
}
} else if (n == 1) {
return list[index[0]];
}
return NULL;
}

void post_print(post *p) {
printf("First name: %s", p->fname);
printf("Last name: %s", p->lname);
printf("Length: %i\n", p->length);
}

void post_printall(post **list, int num) {
int i;

for (i=0;i < num;i++) {
if (list[i] != NULL) {
printf("%i. ",i);
post_print(list[i]);
}
}
}

void post_clearall(post **list, int *num) {
int i;
for (i=0;i < *num;i++) {
if (list[i] != NULL) {
free(list[i]); //Is the freeing of the memory here correct? Or am i just freeing the local pointer?
list[i] = NULL;
}
}
*num = 0;
}

bool post_remove(post **list, int *num, post *p) {
int i;

if (p == NULL) return FALSE;

for (i=0;i<MAXP;i++) {
if (list[i] == p) {
free(list[i]);
list[i] = list[*num]; //This is where it's not working. The swap does not take place in the array, as you'd suspect.
list[*num] = NULL; //However, i am not sure on what way is correct, it is hard with all the levels of indireciton.
*num = *num-1; //This line works fine.
return TRUE;
}
}
return FALSE;
}

bool post_add(post **list, post *p) {
int i;

for(i=0;i<MAXP;i++) {
if (list[i] != NULL) {
list[i] = p;
return TRUE;
}
}
return FALSE;
}

post * post_new(char *fname, char *lname, int length) {
post *p = malloc(sizeof(post));

strcpy(p->fname, fname);
strcpy(p->lname, lname);
p->length = length;

return p;
}

void post_sort(post **list, int num) {
post *tmp;
int i;
int n = num;
int newn;

if (list[0] == NULL) return;

while (n>0) {
newn=0;
for(i=1;i<n;i++) {
if (list[i] != NULL) {
if (list[i-1]->length > list[i]->length) {
tmp = list[i];
list[i] = list[i-1];
list[i-1] = tmp;
newn = i;
}
}
}
n = newn;
}
}

```

Also, the level of this assignment is not very high, i am just overworking it in order to practice. You don't need to worry about giving direct answers as i have already solved the task itself (which was to sort a list). Thank you for your time.

### In Topic: Problem passing a constant string into a funciton in C

27 November 2014 - 07:45 AM

Aaahhhhhhhh, so simple, yet so fatal. Thank you SO much, i was pulling my hair over this for hours, haha.

EDIT: My second problem is, "first" is not properly set to "p" in the newPost function. Have i somehow mixed up my pointers? The list works if i instead do this in the main function:

```int _tmain(int argc, _TCHAR* argv[])
{
post *first = NULL;

first = newPost(first, "Kalle", "Halvarsson", 185);
first = newPost(first, "Johan", "Lovgren", 180);
first = newPost(first, "Ingrid", "Brinkenberg", 165);
first = newPost(first, "Ulrika", "Gillquist", 160);
printlist(first);
return 0;
}```

It feels a bit ugly though and i would rather if the "first" pointer was set inside the function.

### In Topic: Air Resistance approximation re-entry

06 January 2014 - 11:40 AM

Here is the basic formula for air drag:

Fd = 0.5 * Cd * Rho * Velocity^2 * A

Where:

*Fd is the drag force in newtons, applied to the opposite direction of the velocity.

*Cd is the coefficient of drag - this is determined in wind tunnel test. It is a constant which you can tweak to your preference, but there are some examples of the Cd of different primitives to be found here: http://en.wikipedia.org/wiki/File:14ilf1l.svg

*Rho is the density of air. This can be tricky since it changes with temperature, and temperature drops at higher altitudes. Air humidity also affects density. I don't know the exact equation for this, but i suggest you use a constant air humidity, and then sample data from this calculator (http://www.denysschen.com/catalogue/density.aspx) and then make a function out of these points. You might have to find samples of air temerature at different altitudes aswell, but that should not be hard.

*Velocity relative to the airflow.

*A is the reference area in square meters.

### In Topic: Collision Response

05 September 2013 - 01:18 PM

Hmm, i think i will need to give some extra info on this front.

@cr88192:

Trree structures is something i have considered for buildings, where one AABB can represent the building as a whole and child boundaries represent walls and such. To be fair though the play area of the game is not very large, and the ammount of objects quite few (12 players + about as many vehicles and turrets, upon that all the projectiles), and the need for optimization is not very large. This is not really a game per say, but a map for warcraft 3. The challenge is to create a "game within a game", where everything is replaced and the original game is only used for rendering and other basic functions. This is one of the reasons why it is accepted for the engine to be rather crude.The game loop runs every 0,03 seconds which is the lowest supported. The game does not handle rotational movement in collision responses.

About treating static objects separately, that is a good tip. Could I perhaps treat objects with a velocity length of 0 as static, and not test them against other objects? Small velocities are rounded down to 0 as of current.

Sphere tests seem a bit over-the-top for me, iIunderstand the equations for those are quite heavy.

Overall, I appreciate your input, but what I need is really details on how these things would be implemented.

@Norman Barrows:

Yeah, the loop issue propably inherits from the fact that it is just a scripting language. Infact, it is not even really OOP, i use a preprocessor that translates the classes into series of arrays and functions. But like I said, the object is to push the boundaries.

The common way of dealing with collisions is, like you said, to move everything, check for collision, move stuff around a bit, and then repeat the process four or five times until you're guessing everything is solved. I agree this is a very bulky way, although when checking collision object by object, I'm not sure how you would deal with situations where one body displaces another, for instance by pushing into it. You cannot move back to your former position, and all you can do is try to move it out of the way and hope it doesn't end up inside something else.

The first makeshift way I had when dealing with collision with static objects, I just checked for collision axis by axis before adding velocity, and if adding velocity to that particular axis made the object collide with something, i removed the velocity again. With large velocities though, objects would end up resting some distance away from the object they're colliding with, so in the end, you need to find a collision point. By the way, when saying I check if all axes intersect, i mean with AABBs. And no, that is not overkill, it is the simpliest, and perhaps only way of finding wether two boxes overlap.

The solution with creating a collision event (just bundles of data iterated through when doing response) was to keep the collisions manageable, and to deal with events where one object collides with several others. My idea was to maybe extend it into finding all the objects you intersect with and making the collision response taking them all into account at the same time. The operation is really just saving/retrieving data from an array and is really not heavy, right me if i'm wrong. Perhaps i am overdoing it though.

I seriously don't think stepped movement can replace raycasting as you proposed. The reason you have problems with fast objects in the first place is that they move faster than the game updates. If you were to move all objects a little bit at the time with shorter intervals, checking for collision as you go, everything would simply go slower and you would waste loads of processing power in the process.

PARTNERS