rotate to face a map position

rotate to face a map position

Discuss game development here, from a distinct game project to an accessible third-party mutator, down to the interaction and design of individual units if you like.

Moderator: Moderators

Post Reply
User avatar
Tiberium
Posts: 18
Joined: 29 Jun 2012, 19:42

rotate to face a map position

Post by Tiberium »

Hey does anyone know how i can rotate a building to face or point at a map x , z position? i have the position just need help turning it into a rotation so when the building is made it will rotate and face that point. thanks :)

Edit: for ease of explaining lets say i want it facing the team start position no matter wher i build it.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: rotate to face a map position

Post by smoth »

Rotation is best handled in cardinal directions if you REALLY want to rotate in a non cardinal, you can, but I am not sure if it'll work correctly with pathing or yardmaps.

you'll have to use a lua gadget and
Spring.SetUnitRotation
( number unitID, number rotx, number roty, number rotz ) -> nil
you'll have to resolve the rotation vectors which are 0<->1
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: rotate to face a map position

Post by gajop »

I assume you are talking about facing, which can go in only 4 ways for some odd reason (probably legacy). The code for that is trivial and can be calculated based on delta x and delta z (delta being the difference of two points), you can probably figure that part yourself.

It's also possible to rotate a unit, here's my code for it

Code: Select all

            local dx = x2 - x1
            local dz = z2 - z1
            local len = math.sqrt(dx * dx + dz * dz)
            if len > 0 then
               local angle = math.atan2(dx / len, dz / len)
               Spring.SetUnitRotation(unitId, 0, -angle, 0)
            end
PS: The rotation API is a mess in Spring though, too many different stuff that require constant transformations, f.e:
Spring.GetUnitDirection -> rotation vector
Spring.GetUnitHeading -> weird COB thingy
Spring.SetUnitRotation -> radian angles
Spring.SetFeatureDirection -> rotation vector
gl.Rotate -> degree angles
User avatar
Tiberium
Posts: 18
Joined: 29 Jun 2012, 19:42

Re: rotate to face a map position

Post by Tiberium »

Thank you! that code you showed worked perfectly. yeah i failed at explaining but i didnt mind if it was a rotation or facing since i wanted the building to face the nearest map edge it was always either going to be north south east west. The building is an airfield i just wanted it to face the right way when a plane comes from off screen to drop off new units. i just wish i asked you guys before i wasted all day lol thanks again.
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: rotate to face a map position

Post by jK »

gajop wrote:a mess
???
1. Spring uses gradients in all newer code (TA-ism is everywhere in the fooDefs)
2. gl.Rotate is OpenGL not Spring!
3. COB doesn't know floating point (only 16bit integers!), so obv. it must use some proprietary unit
4. using vectors instead of angles is always valid esp. cause they give more data than 1 angle (vector = 2angles + radius)!
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: rotate to face a map position

Post by gajop »

jK wrote:
gajop wrote:a mess
???
1. Spring uses gradients in all newer code (TA-ism is everywhere in the fooDefs)
2. gl.Rotate is OpenGL not Spring!
3. COB doesn't know floating point (only 16bit integers!), so obv. it must use some proprietary unit
4. using vectors instead of angles is always valid esp. cause they give more data than 1 angle (vector = 2angles + radius)!
1. I should have been more clear. Spring's Lua API regarding rotations is messy, or at the very least lacks some calls.
2. I've mostly put it there to show we're using radians as well as degrees. That's a minor nuisance, just wish it was documented (the Spring radians part).
3. It stands out, you cannot directly set it, because "it doesn't make sense in Lua" - unlike the GetUnitHeading?
4. What's the benefit of a radius? I thought the vectors are supposed to be normalized, or at least that's how I converted angles into them. Having both would be preferable though, since angles are quite nice to use for opengl operations, while vectors are good for any further vector math (such as is in this thread).

Basically it's the inconsistency that's the "messy" part. Units and features use different methods to set rotation. Units have different methods of getting and setting rotations.

This wasn't really intended as whining... Just wanted to point out that there's bound to be some confusion when trying to use this for the first time, once I actually figured out what units those functions used, I had to open gimp and derive the formulas.
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: rotate to face a map position

Post by jK »

And I told you it is not inconsistence (except having a spSetUnitRotation instead of spSetUnitDirection).
And how often should I repeat myself gl.Rotate is OPENGL, if you worked with it is it consistence in itself NOT WITH SPRING! And this is desired cause it makes the live easier for anyone who worked already with OpenGL.
Neither would you ever put an unit/feature direction in gl.Rotate and if you do this 1 line won't hurt you.

Also as said a vector has 2(!) angles + radius, so even when the vector is normalized, you still get the pitch as additional information.

PS: I know stuff seems complicated when working with new interfaces etc. But it's not the job of an interface to implement 12 different functions to get the facing of an object, just cause it would save you a few lines of code. And I am not attacking you, but ask you to turn down a gear when claiming it is "a mess".
Last edited by jK on 25 Jul 2012, 05:05, edited 2 times in total.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: rotate to face a map position

Post by smoth »

Tiberium wrote: i just wish i asked you guys before i wasted all day lol thanks again.
Always ask, you never know how many hours you'll waste if you don't!

Best of luck to ya!
Google_Frog
Moderator
Posts: 2464
Joined: 12 Oct 2007, 09:24

Re: rotate to face a map position

Post by Google_Frog »

In what way is the cardinal direction limitation of structure facing legacy? I was not aware rotatable units could have yardmaps.

Spring.SetUnitRotation on structures sounds like something which isn't really suppose to be done. There are probably ways in which it is not supported, I would be very surprised if the yardmap rotated along with it. So test this solution.

Another way would be to rotate your base piece in the LUS. This isn't supported but it definitely has defined behaviour which should not be broken.
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: rotate to face a map position

Post by jK »

Technically the yardmap limitation doesn't exist anymore in 89.0 (yardmap is rotated continuous now). But I still don't advise rotating them cause the blockmap used in pathing has a too low resolution and units won't be able to leave factories when they are rotated.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: rotate to face a map position

Post by smoth »

The above applies to nocardinal rotation only right jk?
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: rotate to face a map position

Post by jK »

it was a reply to
In what way is the cardinal direction limitation of structure facing legacy?
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: rotate to face a map position

Post by gajop »

Not sure what the yardmap is, but if that's the place units are built, I don't think it rotates as it should (I'm using 89.0)? Or at the very least the units built aren't always rotated with the building, f.e:

Image

Units still often find a way out, but it really depends on the size of the unit and the building where they're constructed. Example video: http://www.youtube.com/watch?v=aJAdDz1Rfyk (sorry the video is lagging, still had some debug output going, and recording slowed it)
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: rotate to face a map position

Post by jK »

Use F2. Also kbots have very small footprints, so they have it easier to leave factories, test vehicles.
User avatar
Pressure Line
Posts: 2283
Joined: 21 May 2007, 02:09

Re: rotate to face a map position

Post by Pressure Line »

The yardmap is what controls passability for units.

eg
x = units cannot pass through
o = units can pass through

the arm kbot lab in TA has a yardmap like this when facing north/south when it is open

Code: Select all

xxooxx
xxooxx
xxooxx
xxooxx
xxooxx
xxooxx
when facing east/west it looks like

Code: Select all

xxxxxx
xxxxxx
oooooo
oooooo
xxxxxx
xxxxxx
if you rotate a building the yardmap (afaik) does not rotate with. This means you will have fun stuff like units walking through factory walls to exit.

By 'rotate' i mean "turn piece foo to y-axis bar now" in the unit script, this may or may not apply to 'set HEADING to bar' or the LUS equivalent. Also may or may not apply to Spring.SetUnitRotation.
Post Reply

Return to “Game Development”