General Question Propeller animation [solved]

johnnymanly

Donator
Donator
Joined
Mar 17, 2008
Messages
55
Reaction score
23
Points
8
Location
Southwest Pennsylginia
I'd like to animate a prop in a Lua vessel.
I can set the animation like this:
Code:
pwr = vi:get_animation(anim_Prop)
  da = simdt * 0.1
  pwr_proc = pwr + da
  gas = vi:get_thrusterlevel(thmain)

   if
    gas > 0
   then
    vi:set_animation(anim_Prop,pwr_proc)
   end
but I don't know how to repeat the animation. (loop?)
Any hint would be appreciated.

Thanks.
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,362
Reaction score
3,297
Points
138
Location
Massachusetts
I'd like to animate a prop in a Lua vessel.
I can set the animation like this:
Code:
pwr = vi:get_animation(anim_Prop)
  da = simdt * 0.1
  pwr_proc = pwr + da
  gas = vi:get_thrusterlevel(thmain)

   if
    gas > 0
   then
    vi:set_animation(anim_Prop,pwr_proc)
   end
but I don't know how to repeat the animation. (loop?)
Any hint would be appreciated.

Thanks.
I'm assuming your animation state defines a full totation. The animation state can't exceed 1. So if it does, you have to subtract 1.
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,362
Reaction score
3,297
Points
138
Location
Massachusetts
Thanks for the answer.
Do you mean I have to set the animation state back to 0 before I can make another revolution?

Not quite. Remember that the animation state variable really describes what angle the propeller should be oriented at a given time on a scale from 0 to 360 degrees (0 to 1 animation state). As an example, let's say that the propeller angle increases by 62 degrees every time step. After six steps, the total angle of rotation would be 6*62 = 372 degrees. This would be equivalent to an animation state of 1.03333. You don't want to reset the state to 0 because that wouldn't give the correct angle. What you need to know is what is the angle on a 0 to 360 scale (0 to 1 animation state). So you subtract off 360 degrees (1 from your animation state) to get the proper angle of 12 degrees, at a corresponding animation state of 0.03333.

Mathematically what you want is the remainder of the sum of your animation state increments divided by 1.
 

johnnymanly

Donator
Donator
Joined
Mar 17, 2008
Messages
55
Reaction score
23
Points
8
Location
Southwest Pennsylginia
Thanks for answering but your talking past me a bit. I assumed the angle would increase the correct amount so the animation would finish at 1. (or where I set it )
 

johnnymanly

Donator
Donator
Joined
Mar 17, 2008
Messages
55
Reaction score
23
Points
8
Location
Southwest Pennsylginia
This seems to do it and ties rpm to thrust.
Code:
  --configuration part
  anim_Prop = vi:create_animation(0)

  Prop1 = {
    type = 'rotation',
    mesh = 0,
    grp = 13,
    ref = {x=0,y=0.467,z=8.928},
    axis = {x=0,y=0,z=1},
    angle = -360*RAD
    }

  animcomp_Prop = oapi.create_animationcomponent(Prop1)

  vi:add_animationcomponent(anim_Prop,0,1,animcomp_Prop)
 
  --prop spinning part
function clbk_prestep(simt,simdt,mjd)

  pwr = vi:get_thrusterlevel(thmain)
  prp = vi:get_animation(anim_Prop)
  da = simdt * 0.1 + (pwr * 0.1)
  prp_proc = prp + da

  if
    prp < 1
  then
    vi:set_animation(anim_Prop,prp_proc)
  else
    vi:set_animation(anim_Prop)
  end

end
 

Matias Saibene

Development hell
Joined
Jul 7, 2012
Messages
1,055
Reaction score
642
Points
128
Location
Monte Hermoso - Argentina
Website
de-todo-un-poco-computacion-e-ideas.blogspot.com.ar
This seems to do it and ties rpm to thrust.
Code:
  --configuration part
  anim_Prop = vi:create_animation(0)

  Prop1 = {
    type = 'rotation',
    mesh = 0,
    grp = 13,
    ref = {x=0,y=0.467,z=8.928},
    axis = {x=0,y=0,z=1},
    angle = -360*RAD
    }

  animcomp_Prop = oapi.create_animationcomponent(Prop1)

  vi:add_animationcomponent(anim_Prop,0,1,animcomp_Prop)
 
  --prop spinning part
function clbk_prestep(simt,simdt,mjd)

  pwr = vi:get_thrusterlevel(thmain)
  prp = vi:get_animation(anim_Prop)
  da = simdt * 0.1 + (pwr * 0.1)
  prp_proc = prp + da

  if
    prp < 1
  then
    vi:set_animation(anim_Prop,prp_proc)
  else
    vi:set_animation(anim_Prop)
  end

end
I was racking my brain all afternoon until I found this solution. Can you allow me to use it in my C++ project?
I think I know how to convert it from Lua to C++.
 

johnnymanly

Donator
Donator
Joined
Mar 17, 2008
Messages
55
Reaction score
23
Points
8
Location
Southwest Pennsylginia
I was racking my brain all afternoon until I found this solution. Can you allow me to use it in my C++ project?
I think I know how to convert it from Lua to C++.
Of course. Feel free to use it.
If you get any weird behavior pay attention to the other comments in this thread. I'm not a very good coder.
 
Top