Vector math help [solved]

In this forum you will find and post information regarding the modding of Star Wars Battlefront 2. DO NOT POST MOD IDEAS/REQUESTS.

Moderator: Moderators

Post Reply
MileHighGuy
Jedi
Jedi
Posts: 1194
Joined: Fri Dec 19, 2008 7:58 pm

Vector math help [solved]

Post by MileHighGuy »

Hello Gametoast,

I'm trying to teleport a player to an object by making a vector between the player and the object. I know I can just teleport it to the object using Get/SetEntityMatrix but I'd like to do it this way.

I'm getting the world position of the object and the player using GetWorldPosition(). Then I am making a vector with the difference of those values. Then I can pass the vector values to SetEntityMatrix(). However something is wrong and I can't seem to figure out why. It teleports me in totally different directions depending on where I am in the map, but The distance is always right. So I am assuming there is some minor detail wrong here...

Here is the relevant code

Code: Select all

                            local unit = GetCharacterUnit(player)

                            -- get start and end coordinates
                            local xStart, yStart, zStart = GetWorldPosition(unit)
                            print("player coord " .. tostring(xStart) .. " " .. tostring(yStart) .. " " .. tostring(zStart))

                            local xEnd, yEnd, zEnd = GetWorldPosition(object)
                            print("object coord " .. tostring(xEnd) .. " " .. tostring(yEnd) .. " " .. tostring(zEnd))

                            -- position deltas - create the vector
                            local Dx = xEnd - xStart
                            local Dy = yEnd - yStart
                            local Dz = zEnd - zStart

                            print("delta vector  (left/right) x: " .. tostring(Dx) .. " (up/down) y: " .. tostring(Dy) .. " (forward/back) z: " .. tostring(Dz))

                            local distance = math.sqrt( Dx*Dx + Dy*Dy + Dz*Dz )

                            print( "distance between is " .. tostring(distance))

                            local unitLocation = GetEntityMatrix(unit)
                            SetEntityMatrix(unit, CreateMatrix(0.0, 0.0, 1.0, 0.0, Dx, Dy, Dz, unitLocation))
Here is some sample output

Code: Select all

player coord -62.473567962646 -61.949283599854 168.720703125
object coord -91.142143249512 -61.818775177002 176.36627197266                 
delta vector  (left/right) x: -28.668575286865 (up/down) y: 0.13050842285156 (forward/back) z: 7.6455688476563
Thanks

EDIT: solved it.

So if you give SetEntityMatrix a nil reference matrix (the last argument), it will mean the position and rotation values are absolute, so you can pass xEnd, yEnd and zEnd to go to that position.

to get the rotation I looked at this topic
http://gametoast.com/viewtopic.php?f=27 ... le#p512435

but that wasn't quite working for this, so I had to do this:

Code: Select all


tempturret = CreateEntity("odf_name_here", CreateMatrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, objLocation), "temp_turret")
        local xTemp, yTemp, zTemp = GetWorldPosition(tempturret)
        DeleteEntity(tempturret)

--calculate angle (y-axis rotation)
        local adjacent = xTemp - xEnd
        local opposite = zTemp - zEnd
        local hypotenuse = math.sqrt(((xTemp - xEnd) * (xTemp - xEnd)) + ((zTemp - zEnd) * (zTemp - zEnd)))
        local phi = math.acos(adjacent / hypotenuse)

        --correct the angle
        -- this was a pain in the Diet Dr. Pepper
        if opposite < 0 then
            phi = phi + (math.pi) / 2
        else
            phi = -(phi - (math.pi) / 2)
        end
To see how I used it check out the grappling hook I just released
Post Reply