#### Archived

This topic is now archived and is closed to further replies.

This topic is 5541 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, I''m trying to make AI for spaceships, so that they can commence in dogfights in a 3D universe. I''ve looked through Craig Reynolds'' Steering Behavior For Autonomous Characters and I think I understand the concept. However, I''m struggling a bit with the math involved. First, here''s how I''m thinking to implement it. I''m treating each ship as if I''m looking through the eyes of the ship. Therefore, to get to pointing at any off the other ships, each ship needs to actually rotae around to get the ship in view. Of course, I could use the following:
shipVelocity += normalize(shipPos - targetPos);

But then you have the problem of the ships not always rotating correctly like a real spaceship would do...especially when they''re facing compelete opposite directions. Therefore, instead, I''ve gone the following route:
leftSide = shipVelocity * upVector; // find cross product;

rightSide = -leftSide;

shipVelocity += leftSide;  // or rightSide depending on which

// direction I needed to turn


So, am I going about this in the right way? Also, I''m not sure how I would go about calculating the upVector. I understand that finding a perpendicular vector to an existing vector will actually yield an infinite amount of vectors...a plane. But I still think there must be a way to calculate it based on what little info I have. Thanks for any help on this! I''m on an incredibly tight deadline here to finish this up...my boss kinda threw this at me at the last moment. So, any help would be GREATLY appreciated! Thanks, fixxorion

##### Share on other sites
You''ll need to have at least two vectors, one being the direction of velocity, and the other being the axis of rotation. You have the right idea about adding acceleration perpendicular to the velocity/axis vectors, but beyond that it''s going to take some physics.

I don''t know exactly how you want the ship to behave. Do you want it to turn as fast as it can to face the target, or do you want it to turn in such a way that by the time it''s facing the target, it''s in the same location? I''ll assume the former, just because it makes slightly more sense from an AI standpoint.

Your turn rate will basically determine your centripetal acceleration in conjunction with the radius of your turn. Let''s say it''s in degrees per second. We know there are 360 degrees in a circle, and it''s circumference is 2*PI*R. So, if the ship can turn at X degrees per second, then that''s a ratio of X/360 of the total circumference. The final result is X*PI*R/180 meters per second for the ship''s velocity. Recall (or not ) from physics that centripetal acceleration is v2/R. Plugging everything in, we get:

Ac = X2PI2R/32400

X is the known turn rate (deg/s), and R is the desired turn radius. You can then compute the magnitude of acceleration, the direction of which is always perpendicular to velocity/axis.

This brings us to the topic of constraints. The ship might have a maximum velocity. In that case, you have to find the turn rate that yields this velocity, recognizing that it may be less than your maximum turn rate. On the other hand, you could have a maximum turn rate that limits the ship from traveling it''s maximum velocity. Lastly, you might have a maximum centripetal acceleration, for whatever reason. In that case, your desired turn radius will have to be constrained (plug in Ac, solve for R).

##### Share on other sites
Zipster,

Thanks for the quick response!

I''ll try to implement the physics as soon as possible. It feels like forever since I took physics.

About the up vector, if I assume that the ship initially points at (0, 0, 1) and the initial up vector is (0, 1, 0) can''t I do something like the following:

vector3 changeUpVector(vector3 shipVelocity){    vector3 change = shipVelocity - vector3(0, 0, 1);    vector3 upVector = vector3(0, 1, 0) + change;    return upVector;}

Is that correct? I really want this code to be as accurate and efficient as possible, so any suggestions or ideas would be much appreciated!

Thanks,

fixxorion

##### Share on other sites
Argh! All I want is for the one space ship to follow the other! I thought I had it, but I just can't figure it out!! They follow each other in the XZ plane, but when they move in the Y-Axis, it doesn't work. Can someone please help me!!

I'll be forever grateful!

Thanks,

fixxorion

[edited by - fixxorion on October 8, 2003 12:08:29 PM]

##### Share on other sites

Try and make two balls chase each other in a 3D scene.

p1X, p1Y, p1Z
p2X, p2Y, p2Z

And they need to know which direction they should be going.
tyAcc = ( p1Y - p2Y)
txAcc = ( p1X - p2X)
tzAcc = ( p1Z - p2Z)

And then they need to start moving in that direction
yAcc += tyAcc;
xAcc += txAcc;
zAcc += tzAcc;

Get that running by itself and leave it in a backup folder.

Write another app that causes two ships to point towards each other, but not move. The code will probably be very similar to the chase code.

Once you have two different versions of your problem partially solved you''re ready to go for the final solution. At least you''ll have a working platform to test with.

Good luck
Will

##### Share on other sites
RPGeezus,

Thanks for the help. I''ve got what you suggested working already. I have a 3D scene in which I had two "ships" chasing each other, and that worked good. The only problem is that when the two ships were facing complete opposite directions the vectors would slow down, and then the ships would "snap" pointing to the other ship. I need some way to get the ships to have to actually turn around. Any ideas?

Thanks,

fixxorion

##### Share on other sites
To force ships to turn in an arc, you need to add a limit to the amount your direction vectors can change. (Ex. max out at 5 degrees/second or a sliding scale based on speed, Higher speeds have a lower turning rate, so should take longer to turn around)

I''m not really good with 3d games, I work only with 2d,... I hope that will change when I get a new com and can RUN 3d games easily,...

##### Share on other sites
Either method works, but mine gives you nice circular turns And your constraints are built into the process, so it''s easier to control velocity and turn rate.

##### Share on other sites
Would you not rotate the ship a little bit each step, then apply whatever thruster power the ship has in the new direction to the velocity of the ship (just like if a player were flying the ship)? Obviously, you''d only want to thrust if you were facing the direction of your enemy.

##### Share on other sites
I think it all depends on what type of look he''s going for. If he wants his space ships to act like fighter-jets then they will accelerate in to the turn.

If he wants his space ships to act more like space ships then the ships will probably reorientate themselfs before accelerating.

The fighter-jet approach probably looks a lot cooler, but, is less realistic.

About your ships turn vectors being wierd.. So, if they are facing perfectly opposite directions they wont turn properly? This kind of makes sense.. You''ll have to ''bias'' the turn. That is, encourage your ship to turn to the right (+x angle) when facing the opposite direction of where it wants to be facing.. Once the turn has started then the problem will clear itself up.

I hope.

Best of luck,
Will

1. 1
2. 2
3. 3
Rutin
20
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633656
• Total Posts
3013190
×