can anybody tell me how to use python to make a cylinder swing back and forth in blender?
If you move the pivot point of the cylinder to the center of where you want it to swing (go into edit mode and move all the geometry at once and leave the origin at the center of the swing or move the 3d cursor to the center of the swing and chose Object> Transform> Origin to 3D Cursor.
Then for the python:
import bge
import math
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
rot = own.orientation
euler_rot = rot.to_euler()
t = own['time']
speed = 10 #oscillation speed
amount = 2 # size of the swing
sine = math.sin(t*speed)*amount
euler_rot = [sine,0,0] # chose what axis
own.orientation = euler_rot
main()
Simply attach an always sensor with True Level Triggering (pulse mode) to the above script and play with the pivot point of the cylinder and the speed and amount values in the script.
Hope that is what you want.
EDIT: You will also need to add a timer property to the object and name it ‘time’.
Or just have it be a rigid body, have it linked with a rigid body to a static “Axel” and define the joint so it can swivel/(pendulate?is that even a word?)
pendulate?
o well
any way, use Always every X time apply X force or torque and it will move, time it right, and it will swing,
You can make 30 cylinders swing in a chain with this set up
here is a rigid body with torque applied
Attachments
RigidDemo.blend (948 KB)