Stingray Forum (Read Only)
Welcome to Autodesk’s Stingray Forums. Share your knowledge, ask questions, and explore popular Stingray topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Lua Move unit along angle

4 REPLIES 4
Reply
Message 1 of 5
Anonymous
705 Views, 4 Replies

Lua Move unit along angle

Hi everyone!

 

I'm attemping to make a unit move in the direction that I rotate it, however I'm very new to programming in 3D.. everything that I've done in the past has always been 2D.

 

Currently, I have a function that lets me set the yaw, pitch and roll of the object:

 

function PlayerMovement.set_yaw_pitch_roll(unit, index, yaw, pitch, roll)
    local yaw_rotation = Quaternion(Vector3.up(), math.rad(yaw))
    local pitch_rotation = Quaternion(Vector3.right(), math.rad(pitch))
    local roll_rotation = Quaternion(Vector3.forward(), math.rad(roll))
    local new_rotation = Quaternion.multiply(Quaternion.multiply(yaw_rotation, pitch_rotation), roll_rotation)
    
    Unit.set_local_rotation(unit, index, new_rotation)
end

This appears to work fine, I can set any angle and the object will be correctly positioned. Now, when I go to move him, if I were to calculate x and y velocities for a 2D game I would have done it like this:

 

// pseudo code
velX = speed*sin(angle)
velY = speed*cos(angle)

Now, my current implementation of this in the Lua for 3D is this:

 

function PlayerMovement.calculate_transform(unit, index, dt)
    local curr_velocity = PlayerMovement.velocity:unbox()
    local frame_velocity = curr_velocity
    local curr_pos = Unit.local_position(unit, index)
    
    Vector3.set_x(frame_velocity, -PlayerMovement.speed * math.sin(math.rad(PlayerMovement.yaw)) * dt * (1-PlayerMovement.pitch/90))
    Vector3.set_y(frame_velocity, PlayerMovement.speed * math.cos(math.rad(PlayerMovement.yaw)) * dt * (1-PlayerMovement.pitch/90)) 
    Vector3.set_z(frame_velocity, PlayerMovement.speed * math.sin(math.rad(PlayerMovement.pitch)) * dt)
   
    PlayerMovement.velocity:store(frame_velocity)
    
    Unit.set_local_position(unit, index, curr_pos+frame_velocity)
end

I know the naming conventions are dodgy but I'm only testing!! So this works, until the object pitches past 90 degrees, obviously, as I have my (1-PlayerMovement.pitch/90) line which I THINK eliminates the correct ratio of forward movement compared to vertical, but only works (well.. it looks right anyway haha) for a quarter of the range of motion.

 

How could I improve this? What should I do to correct for the diminishing amount of forward movement that would result from moving vertically at the same time? I believe my solution is on the right track but only works for a quarter of the motion and I feel like a more elegant solution than a bunch of if statements is waiting out there. And lastly, any suggestions at all for flight in space? (links or books are fine, in any programming language)

 

Cheers,

Kaine

 

4 REPLIES 4
Message 2 of 5
Anonymous
in reply to: Anonymous

Update: I've started experimenting with this http://mathworld.wolfram.com/SphericalCoordinates.html which I think looks promising

Message 3 of 5
Anonymous
in reply to: Anonymous

Sorry for the triple post (too late to edit), just thought I'd post my progress. This is my current transform function to calculate transform using a spherical coordinate system:

 

function PlayerMovement.transform(unit, index, dt)
    local curr_pos = Unit.local_position(unit, index)
    
local speed = PlayerMovement.speed
local x = speed*math.cos(math.rad(PlayerMovement.yaw)) * math.sin(math.rad(PlayerMovement.pitch)) local y = speed*math.sin(math.rad(PlayerMovement.yaw)) * math.sin(math.rad(PlayerMovement.pitch)) local z = speed*math.cos(math.rad(PlayerMovement.pitch)) local frame_velocity = Vector3(x,y,z)*dt Unit.set_local_position(unit,index, curr_pos+frame_velocity) end

Where:

PlayerMovement.speed = radius of the 'sphere' (imaginary sphere!)

PlayerMovement.yaw = azimuthal angle

PlayerMovement.pitch = zenith/polar angle

 

The issue I'm having is that as the zenith angle is measured from the up-axis (z), a zenith/pitch of 0 degrees is directly upwards and a pitch of 90 is flat. How can I convert my pitch number, to translate to the respective zenith angle? For example, a pitch of 30 degrees should equal a zenith of 60 degrees. As pitch is measured from the xy plane upwards, and zenith is from the z axis downwards.. 

 

Besides that I think is much better than my original function, I'm starting to get my head into the space of 3D.

 

Cheers,

Kaine

 

Edit: I think I fixed my zenith angle issue, I just simply do (90-pitch) for the angle accepted by my spherical coord system, which makes the angles for pitch behave how I think of it in my head heh.

 

Any suggestions or advice would be great though!

Message 4 of 5
dan.matlack
in reply to: Anonymous

Hi Kaine,

I have a friend who is working on a space game and has done some very good controls. I'm going to reach out to him and see if he can offer up some advice on best practices for space simulated movement.
______________________________________
Dan Matlack
Senior Content Manager || Games Solutions
Autodesk, Inc.
Message 5 of 5
Anonymous
in reply to: dan.matlack

Hey Dan!

 

That's awesome! I really appreciate it. Look forward to hearing back from you.

 

Cheers,

Kaine

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report