Jump to content

  • Log In with Google      Sign In   
  • Create Account


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

Topics I've Started

Calculating how to decelerate to reach point

13 March 2015 - 12:02 PM

Hello everyone!


I am trying to make a simple game where you can move a satellite across a flat map and have it shoot lasers down to the surface. Needless to say that it is not an accurate simulation - you can move it in whichever direction you choose, but it's movement has a lot of inertia. The movement is limited to the 2d plane.


Each satellite has a vector for position, velocity, and target coordinates. Each loop, the velocity is scaled by a friction factor (0.93). What i want to know is this: how can i calculate wether i should add thrust to the satellite without the velocity becoming so high that it doesn't have time to break (by friction) in the distance left to the target? I realize that calcuating it precisely will end up in a differential equation, so all i need is an approximate "max velocity" where it will no longer apply thrust, to avoid overshooting.


Any help is very appreciated!

Problem passing a constant string into a funciton in C

27 November 2014 - 07:14 AM

I am trying to create a linked list of contacts in C, and sort them by person length, as an assignment for school. I kinda wanted to use a constructor to simulate object oriented programming. Only problem is, that when i try to enter the name as a string in my constructor function, i get a compile error.


This is how my code looks:

#include "stdafx.h"
#include "stdlib.h"
#include "string.h"

#define MAXL 32

typedef struct post {
    char enamn[MAXL];
    char fnamn[MAXL];
    int langd;
    post *prev;
    post *next;

void sortlist(post *first);

post * newPost(post *first, const char en, const char fn, int ln);

void printlist(post *first);

int _tmain(int argc, _TCHAR* argv[])
    post *first;
    newPost(first, "Kalle", "Halvarsson", 185);
    newPost(first, "Johan", "Lovgren", 180);
    return 0;

post * newPost(post *first, const char *fn, const char *en, int ln) {
    post *p;
    //Reservera minne till posten:
    p = (post*)malloc(sizeof(post));
    strcpy(p->fnamn, fn);
    strcpy(p->enamn, en);
    p->langd = ln;
    first->prev = p;
    p->next = first;
    *first = *p;
    return p;

void removePost(post *first, post* whichpost) {
    post *temp = NULL;

    whichpost->next->prev = whichpost->prev;
    whichpost->prev->next = whichpost->next;


void swap(post *a, post *b) {
    post *tnext = NULL;
    post *tprev = NULL;
    tnext = a->next;
    tprev = a->prev;
    a->next = b->next;
    a->prev = b->prev;
    b->next = tnext;
    b->prev = tprev;

void sortlist(post *first) {
    post *curr = first;
    while(curr->next != NULL) {
        //This function is incomplete
        curr = curr->next;

void printlist(post *first) {
    post *curr = first;
    while (curr != NULL) {
        printf("Förnamn: %s\n",curr->fnamn);
        printf("Efternamn: %s\n",curr->enamn);
        printf("Längd: %s\n",curr->langd);
        curr = curr->next;

The error message i get is:


Error    1   : 'newPost' : cannot convert parameter 2 from 'const char [6]' to 'const char'



What am i doing wrong? Overall feedback on the code is very welcome, although note that it is still very unfinished.


Thanks in advance.

Getting the roll euler angle from a quaternion

24 September 2013 - 08:45 PM

Hello everyone! 

I have found myself in a situation where I need to convert the roll euler angle from a quaternion. I am using it to represent the roll of a tall ship, where angle "0" is where the ship is in its normal state with the masts in the air. I know I shouldn't use eulers, but there are limitations forcing me to do so. I also use the modulo to put the angle in the span of 0 to 252 (again, don't ask).


Here is the formula i have tried so far:


        public float getRoll () {
            float sqw = .w*.w
            float sqx = .x*.x
            float sqy = .y*.y
            float sqz = .z*.z
            float u = sqx + sqy + sqz +sqw
            float test = .x*.y + .z*.w
            if (test > 0.4999*u) {

                return 0
            } else { if (test < -0.4999*u) {

                return 0

            return Atan2(2*.x*.w -2*.y*.z, -sqx +sqy -sqz +sqw) * RADTODEG

While the ship is turning around, it appears to be working fine while within 180 degrees of the ships yaw rotation, but on the other half the ship is suddenly standing with its keel in the air. What is wrong??


Thankful for answers.


EDIT: I found a strange relationship regarding the upside down-error. Hang on:

If I multiply the angle returned by the function, by the sign of the dot product of the ships heading and the positive X axis, the ship will keep straight. It works decently now, although this feels kinda like an ugly hack. I am not sure if the roll value is actually correct either, it is hard to tell. Any help on this?

Collision Response

05 September 2013 - 09:41 AM

Hello everyone! This is my second time posting on this website, and so far I have very much appreciated the professional help its members have to offer. I am creating a custom physics engine for a game mod and am having some trouble dealing with collision detection and response. This might sound like a common thread topic, but in my case, some special circumstances are making the process more difficult.


First of all, the game has trouble dealing with a situation where, when a loop which is run every frame, contains a function which in turn contains its own loop. This usually causes the game to bog down or the entire game loop to be aborted. This happens even if the function only contains an empty loop iterating from 1 to 10. The game is much more tolerant towards nesting loops inside eachother when inside the same funciton.


Second, the game is written in a OOP-style scripting language to which there are no pre-existing libraries to include. This means i have to build everything from scratch - i currently have things like vector/quaternion libraries and a module for adding linked lists to classes. So before you suggest raycasting solutions, please explain how to best implement this in a cheap way, keeping in mind the loop issue.


Some background to my system:


Every frame, two separate loops are run - one for all rigid bodies, applying gravity, friction, velocities and so forward, another for collisions. There, I check first if the objects collide with eachother, and second if they collide with static objects in the game (such as walls, represented by AABBs). I do this by first doing a basic boundary check with an AABB or sphere, and I also plan to implement a SAT test for object-object collisions. If collision occurs, a class instance is created containing information about which two objects are colliding, and if it is a object to object collision, or object to static. Every frame I also loop through all these instances and cleaning out any doubles (two objects colliding will render two collision instances). 


My problem:


After this, it is time to push the objects apart, and this is what I find tricky. The way I define wether two objects have collided is to check if all axes intersect, but then how do i know on which axis to push them apart? Pushing them along the vector between the objects sound silly to me, because wether I push an object sidewards into a wall, the force should only push it outwards in the direction of the planes normal, and not stop the sidewards motion. How do I find which plane the object is colliding with?


NOTE: Something I just came up with while typing this, can I do it by calculating the normals of each surface of the box, and then do a dot product between them and the velocity vector of the object, picking the normal which has the lowest dot value?


Second thing is when an object is resting upon another object, like a soldier standing on a tank. I understand you are to apply some kind of constraint to bind the soldier to the tank, how does this work?


Thirdly, what is the best way to do collision check on fast moving objects, such as bullets? Is it possible to somehow check a line against a box without using loops, and to low expense?


As a side note I can mention that the requirements of precision in this engine are very low. I am doing all i can to simplify things while still having them look acceptably good. Any suggestions overall about how to structure a physics engine or collision detection are very welcome.

Enforcing angle limits on a turret

02 September 2013 - 08:13 AM

Hello everyone!


I am trying to make a space RTS where ships have 2 rotation DOF (pitch, yaw) and 3 translation DOF (x,y,z). Each ship has a set of turrets, some of which have their pivot angles restricted. The turret itself pivots around it's yaw axis, and the barrels on their pitch axis, and each axis has its own restrictions. Each turret has one quaternion for the base orientation, and one for the barrel orientation. The turret also has a set traverse speed on each axis. 


My problem is that I don't know how to find the rotation needed on each axis to traverse the turret towards it's target. Ofcourse, i also need to find wether it can reach the target at all, or if it should find something else to shoot at. My first thought was to convert the target coordinates into the turrets local space and find the angles using trig (is this possible?). What way would you do it? Do i need to save the local euler orientation or can this be done entirely with quaternions? What is the fastest and easiest way of doing this? 


These are the functions i want to make:

private bool isObjectInField (object o)

public bool rotateTowardsPoint (vector v)

But first, some background:


*I am making this inside the Warcraft III engine. The language I use is object oriented and similar to Java. 


*Z axis is "up", and X is "forward" (in model space).


*I am using my own vector and quaternion libraries which have most of the funcitons necessary. I would, however, like to refrain from using SLERP since it is too slow. 


Any help is very much appreciated. I can uderstand most code languages, so do feel free to post code examples.