rotating an object on an arc

Posted by gardian06 on Game Development See other posts from Game Development or by gardian06
Published on 2012-06-05T23:34:08Z Indexed on 2012/06/06 4:48 UTC
Read the original article Hit count: 205

Filed under:
|

I am trying to get a turret to rotate on an arc, and have hit a wall.

I have 8 possible starting orientations for the turrets, and want them to rotate on a 90 degree arc. I currently take the starting rotation of the turret, and then from that derive the positive, and negative boundary of the arc. because of engine restrictions (Unity) I have to do all of my tests against a value which is between [0,360], and due to numerical precision issues I can not test against specific values.

I would like to write a general test without having to go in, and jury rig cases

//my current test is:
// member variables
public float negBound;
public float posBound;

// found in Start() function (called immediately after construction)
// eulerAngles.y is the the degree measure of the starting y rotation
negBound = transform.eulerAngles.y-45;
posBound = transform.eulerAngles.y+45;
// insure that values are within bounds
if(negBound<0){
    negBound+=360;
}else if(posBound>360){
    posBound-=360;
}

// called from Update() when target not in firing line
void Rotate(){
    // controlls what direction 
    if(transform.eulerAngles.y>posBound){
        dir = -1;
    }
    else if(transform.eulerAngles.y < negBound){
        dir = 1;
    }
    // rotate object
}

follows is a table of values for my different cases (please excuse my force formatting) read as base is the starting rotation of the turret, neg is the negative boundry, pos is the positive boundry, range is the acceptable range of values, and works is if it performs as expected with the current code.

|base-|-neg-|-pos--|----------range-----------|-works-|
|---0---|-315-|--45--|-315->0,0->45----------|----------|
|--45--|---0---|--90--|-0->45,54->90----------|----x----|
|-135-|---90--|-180-|-90->135,135->180---|----x----|
|-180-|--135-|-225-|-135->180,180->225-|----x----|
|-225-|--180-|-270-|-180->225,225->270-|----x----|
|-270-|--225-|-315-|-225->270,270->315-|----------|
|-315-|--270-|---0---|--270->315,315->0---|----------|

I will need to do all tests from derived, or stored values, but can not figure out how to get all of my cases to work simultaneously.

//I attempted to concatenate the 2 tests:
if((transform.eulerAngles.y>posBound)&&(transform.eulerAngles.y < negBound)){
    dir *= -1;
}

this caused only the first case to be successful

// I attempted to store a opposite value, and do a
void Rotate(){
    // controlls what direction 
    if((transform.eulerAngles.y > posBound)&&(transform.eulerAngles.y<oposite)){
        dir = -1;
    }
    else if((transform.eulerAngles.y < negBound)&&(transform.eulerAngles.y>oposite)){
        dir = 1;
    }
    // rotate object
}

this causes the opposite situation as indicated on the table.

What am I missing here?

© Game Development or respective owner

Related posts about rotation

Related posts about logic