🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Rotation with limited turn rate in degrees

Started by
45 comments, last by the incredible smoker 5 years, 12 months ago
9 hours ago, Oberon_Command said:

It's all a pretty illusion drawn up to entertain the player. Most of the time, gameplay logic doesn't need complicated math or elaborate approximation methods to look good. A few vectors and a scale and some simple trigonometry will often go a long way.

Agree, but if we look at how games change over years, there's always a move towards more realism. Comparing PacMan physics (moving in 4 directions at constant velocity) to actual physics engines is quite a difference. So the requirements in math are constantly growing, also in all other fields like graphics, sound or AI. There's less and less application for simple approximations. Everything becomes real science it seems, up to the point when we will be able to create photorealistic worlds with plausible physics. (But yes, gameplay logic is an exception - games should have simple rules based on simple math i guess.)

Advertisement
21 hours ago, JoeJ said:

 Using trig when it's not necessary 

 

Your code has many maths, trig functions, also division.

I see things like acos, sin cos,

Why dont you take a position that follows the target position ?

 

And how do you handle without limits ?, is the turret very dumb ?

S T O P C R I M E !

Visual Pro 2005 C++ DX9 Cubase VST 3.70 Working on : LevelContainer class & LevelEditor

6 hours ago, JoeJ said:

Agree, but if we look at how games change over years, there's always a move towards more realism. Comparing PacMan physics (moving in 4 directions at constant velocity) to actual physics engines is quite a difference.

Maybe in AAA first-person shooter type games where "realism" is a sign of a big budget. Outside of that sort of game, though, I'm not sure I agree. Certainly I don't see indie and mobile games moving more towards physical realism. Even in AAA, most of the "realism" is brought about through art style and rendering methods rather than physics simulations. Even in physics-heavy games, physics engines like Havok and PhysX provide a lot of the heavy lifting.

The point I was making was that if you can get away with simple math, use the simple math. It'll save everyone time in the long run; it'll be easier to maintain, less fragile, and designers will have a better idea of how to tweak it (how do I explain to a designer or artist what the parameters on a PID controller do when they have no math or engineering background?). The fewer moving parts there are, the less bugs you will have from features interacting with one another unexpectedly.

41 minutes ago, the incredible smoker said:

Your code has many maths, trig functions, also division.

I see things like acos, sin cos,

The trig is to convert to spherical coords so i can apply the angle limits. But it would better to store 4 planes instead angles and to project the target vector to those planes if it is in the wrong half space, like so:

if (1) // angle limits, but using planes to avoid trig
    {
        vec dir = turret.mountOrientation.Unrotate(targetDir); // move to local mount space

float distanceToPlane = turret.planeLimitLeft.Dot(dir); // actually we do not need a plane but just a unit legnth direction, because the planes lie at the origin of mount space (i assume the mount space origin to be the center of rotation)

if (distanceToPlane > 0) // wrong half space?

{

dir -= dir * distanceToPlane; // now dir is projected to the limit plane to prevent the turret from rotating out of limits

}

// do the same with the 3 other planes, normalize dir but care for the case it has zero length

        targetDir = turret.mountOrientation.Rotate(dir); // back to global space
    }

Notice you get the distance to the plane using the dot product, this is the most important thing to understand the dot product. Extending this thought, you can understand geometrically how a 3x3 matrix times 3D vector multiplication rotates the vector, as it uses 3 dot products to find the distances along 3 basis vectors and their planes to calculate the transformed (rotated) resulting vector.

Understanding that, you can also understand 3x3 matrix multiplication, which is just rotating the 3 basis vectors of one matrix to the other. So this is how matrix rotations works.

I mention this because i guess you're not there yet and i remember how much it helped me figuring this out. The same works in 2D.

 

The final acos is used to determinate an angle, because the OP requested rotating at max angular speed. Without this requirement, we could get rid of this as well. To prevent the turret from snapping to the target in an instant, we could just use simple interpolation with previous state:

vec newTurretDir = prevTurretDir * 0.98 + targetDir * 0.02; // behaviour depends on rate of updates!

Then construct the orientation from newDir (as we did in your last topic) with an additional up vector.

 

... but notice an acos here and there will nor affect your performance. Also this is just gameplay logic, not engine code. Use a profiler to find your real bottlenecks ;)


 

1 hour ago, the incredible smoker said:

Why dont you take a position that follows the target position ?

You need to replace this line: static vec target (10, 3, 0); with your real target.

 

1 hour ago, the incredible smoker said:

And how do you handle without limits ?, is the turret very dumb ?

The most recent code contains limits (to restrict rotational freedom of the turret), earlier versions did not.

The turret is dump, it only rotates towards a given target at constant max speed. To handle moving targets, you would need to calculate a offset to the target, e.g. using Alvaros tutorial he has linked to.

 

 

1 hour ago, Oberon_Command said:

Outside of that sort of game, though, I'm not sure I agree. Certainly I don't see indie and mobile games moving more towards physical realism. Even in AAA, most of the "realism" is brought about through art style and rendering methods rather than physics simulations. Even in physics-heavy games, physics engines like Havok and PhysX provide a lot of the heavy lifting.

Yes i agree - i always think about the future, and the vision of an alternate reality is my personal motivation to keep interested in gamedev, although i've lost most interest in the games themselves. But i'm sure we'll get there, probably weaker mobile devices will be no more limitation then (streaming). I don't think realistic worlds necessarily means realistic games - games should always keep unrealistic to be fun or interesting. Realism is only good to get more 'Wow! It's almost real!' to satisfy gamers requests on constant progress, but also to enable new iseas and even genres.

But i disagree with using physics engines would be a way to offload the heavy lifting to some experts, because the development of them belongs to game development as well. So the requirements in expertise are growing. The need for a group of people to focus on 'just' a physics engine, or just a graphics engine, or just a game is just another proof for this.

Ok, for a moving target with the vanilla calculation bla bla.

Homing missiles need to be smarter, not that vanilla smart, assuming enemys will be slower then missiles.

When they ask for slow rotation, they dont need exact angle per step, the angle per step may differ, it is not a problem i assume unless you have some reason.

 

For fast homing missiles no math functions needed, here is my theory how i would try :

That is why i say you take a vector that moves towards the (moving) target vector as cheapest solution, you dont have any angle at all.

Just set the direction and matrix.

There might be a collision check needed between the aim vector and the missile self ( big circle ) so the rocket cant rotate 180 degrees in 1 frame by accident, there the rocket wont home to a target exactly behind the rocket, so what, fire some more, and has a nice natural behaviour thing to it automaticly.

I suggest to check the self collision for each axis you move, so the aimvec will avoid the rocket eventually.

It would be nice to have the aim vector moving in straight lines, use fast inverse square root for that.

 

 

 

 

S T O P C R I M E !

Visual Pro 2005 C++ DX9 Cubase VST 3.70 Working on : LevelContainer class & LevelEditor

Ok, i see i've completely missed the OP question. Not sure why i have a thought of a turret, but i see it is just about homing missels - no turrets at all. sorry for that.

2 hours ago, the incredible smoker said:

There might be a collision check needed between the aim vector and the missile self ( big circle ) so the rocket cant rotate 180 degrees in 1 frame by accident, there the rocket wont home to a target exactly behind the rocket, so what, fire some more, and has a nice natural behaviour thing to it automaticly.

But you could use this simple code to guide the missile as well:

vec newMissileDir = (prevMissileDir * 0.98 + (targetPos-missilePos).Unit() * 0.02).Unit(); // behaviour depends on rate of updates!

It already cares for 180 degree issues and is the simplest and fastest thing to do i guess, there is not even a need to check for zero vectors on normalization, except the missile already hits the target.

6 hours ago, Oberon_Command said:

how do I explain to a designer or artist what the parameters on a PID controller do when they have no math or engineering background?)

Same Soviet manager says about real automation - we no need automation becouse have no workers with backfround. Results is known. Looks like same situation into overseas game industry nowadays. Any guys that have required background so highly contributed into real industry so gaming companies have no chance to hire tham. It why AAA+  can not competite by gamepley even with 10 years old games maked by tiny teams with required background.

#define if(a) if((a) && rand()%100)

21 minutes ago, Fulcrum.013 said:

Same Soviet manager says about real automation - we no need automation becouse have no workers with backfround. Results is known. Looks like same situation into overseas game industry nowadays. Any guys that have required background so highly contributed into real industry so gaming companies have no chance to hire tham.

No, that isn't it. It's more that most designers I've encountered studied... well, game design rather than engineering. Art subjects instead of mathematics. Literature instead of programming. Their job isn't to do math, it's to invent fun and meaningful interactive experiences. It's a rather different skillset from programming. They delegate the business of actually doing the math and making the toys to programmers. :)

Remember, we aren't automating industrial processes or writing scientific software here. We're making art, and our software enables artists not (other) engineers.

A few designers I know did start out as programmers, but most of them went into design because they wanted to get away from all the math and focus on the creative aspects.

21 minutes ago, Fulcrum.013 said:

It why AAA+  can not competite by gamepley even with 10 years old games maked by tiny teams with required background.

That's more to do with AAA budgets being so large that they can't afford the risk inherent to trying gameplay ideas that are too new. They've got shareholders to please. Indie companies affect fewer people if they fail and don't have the budget clout to compete on even terms with the "big boys", so they can (and must) do riskier, experimental things.

But this is offtopic.

27 minutes ago, Oberon_Command said:

Their job isn't to do math, it's to invent fun and meaningful interactive experiences.

Any processes into world, real or dummy ,described by differential equations. So  making of fun require a very high tech. Literature is some text that game shows. Art obhects is some textures drawn on objects. But it not a game, it just a data that program shows even without any processing. But interaction of objects and behavior simulation is 100% of engineering. Just look for modern MOBA for example. It is no any literature or art. It is 100% tech object that require to be engineered to make fun from gameplay. MMORG or single player strory-driven games is same, but just have some text and order of levels to pass thru added and nothing else.

#define if(a) if((a) && rand()%100)

This topic is closed to new replies.

Advertisement