示例#1
0
def ProcessClicks(evt):
    global Trail
    b = scene.mouse.pick
    if b is not None:  # If we've clicked on an actual object, and not the background
        try:
            if Trail is not None:
                Trail.clear()
                Trail.stop()
            print('Trailing: %s' % b)
            Trail = vpython.attach_trail(b,
                                         type='points',
                                         radius=0.02,
                                         pps=10,
                                         retain=600)
        except:
            pass  # Can't leave a trail for Saturn, or a breadcrumb point
    else:
        Trail.clear()
        Trail.stop()
        Trail = None
        else: vel.y = -abs(vel.y)
    if abs(loc.z) > d:
        if loc.z < 0: vel.z = abs(vel.z)
        else: vel.z = -abs(vel.z)


N = 2000  # numero di particelle
particelle = []  # lista che conterrà tutte le particelle (sphere)

polline = sphere()
polline.mass = 2
polline.radius = 0.15
polline.pos = vector(0, 0, 0)
polline.velocity = vector(10, 0, 0)
polline.color = color.green
attach_trail(polline, radius=0.006, retain=70, pps=4, type='points')

for index in range(N):
    particella = sphere()
    particella.mass = 0.01
    particella.radius = 0.015
    particella.pos = vector.random()
    particella.velocity = vector.random()
    particella.color = color.orange
    particelle.append(particella)

# Disegna un cubo di lato 2d
d = 1  # semilato del cubo
r = 0.005
gray = color.gray(0.7)
boxbottom = curve(color=gray, radius=r)
示例#3
0
Created on Fri Mar 26 17:47:54 2021

@author: Andrea Bassi
"""

from vpython import scene, vector, arrow, color, sphere, rate, attach_trail, pi, mag

body = sphere(radius=0.05)
body.color = color.orange

R = 1

body.velocity = vector(0, 0.7, 0)  # velocità iniziale diretta verso l'alto
body.pos = vector(R, 0, 0)

attach_trail(body)

dt = 0.01

# mostra gli assi x e y
axis_x = arrow(pos=vector(0, 0, 0), axis=vector(1, 0, 0), shaftwidth=0.01)
axis_y = arrow(pos=vector(0, 0, 0), axis=vector(0, 1, 0), shaftwidth=0.01)

time = 0

while True:

    rate(100)

    time = time + dt
示例#4
0
def launch(sample_rate, position, orientation, COM, COP, thrust, gravity, lift, drag):
    for step in range(len(position)):
        position[step][2] = -position[step][2]

    force_scale = 0.01
    l = 2
    rad = 0.09
    steps = len(position)

    # Initialization of enviorment
    render = vp.canvas(height=600, width=1200, background=vp.vector(0.8, 0.8, 0.8), forward=vp.vector(1, 0, 0), up=vp.vector(0, 0, 1))
    render.select()
    render.caption = "Loading..."
    render.visible = False

    # Initialization of body and cone
    body = vp.cylinder(pos=vp.vector(-l, 0, 0), axis=vp.vector(l*0.8, 0, 0), radius=rad)
    cone = vp.cone(pos=vp.vector(-l*0.2, 0, 0), axis=vp.vector(l*0.2, 0, 0), radius=rad)

    # Initialization of fins
    a_fins = vp.box(pos=vp.vector(-l + (l*0.05), 0, 0), size=vp.vector(l*0.1, rad*4, rad*0.25))
    b_fins = vp.box(pos=vp.vector(-l + (l*0.05), 0, 0), size=vp.vector(l*0.1, rad*0.25, rad*4))

    inv_box = vp.box(pos=vp.vector(l/2, 0, 0), size=vp.vector(l, 10e-10, 10e-10), visible=False)

    # Initialization of rocket
    rocket = vp.compound([body, cone, a_fins, b_fins, inv_box], pos=vp.vector(0, 0, 1), axis=vp.vector(1, 0, 0), up=vp.vector(0, 0, 1), color=vp.vector(1,1,1), opacity=0.5)

    COM_sphere = vp.sphere(radius = 0.04, color=vp.vector(0, 0, 0))
    COP_sphere = vp.sphere(radius = 0.04, color=vp.vector(0, 0, 0))
    thrust_pointer = vp.arrow(shaftwidth = 0.1, color=vp.vector(1, 1, 0))
    gravity_pointer = vp.arrow(shaftwidth = 0.1, color=vp.vector(0, 1, 1))
    lift_pointer = vp.arrow(shaftwidth = 0.1, color=vp.vector(1, 0, 1))
    drag_pointer = vp.arrow(shaftwidth = 0.1, color=vp.vector(0, 0, 1))

    a = 4
    c = 0.3
    b = a - c
    sqr_out = [[-a, a],[a, a],[a, -a],[-a, -a], [-a, a]]
    sqr_in1 = [[-b, b - 3*c],[b, b - 3*c],[b, -b],[-b, -b], [-b, b - 3*c]]
    sqr_in2 = [[-b, b],[b, b], [b, b - 2*c],[-b, b - 2*c], [-b, b]]
    ref_box = vp.extrusion(path=[vp.vector(-c/2, 0, 0), vp.vector(c/2, 0, 0)], shape=[sqr_out, sqr_in1, sqr_in2], color=vp.vector(0.5, 0.5, 0.5))

    render.camera.follow(rocket)
    render.autoscale = False
    launch_pad = vp.box(pos=vp.vector(position[0][0], position[0][1], -1), size=vp.vector(16, 16, 2), color=vp.vector(0.2, 0.2, 0.2))
    launch_pad = vp.box(pos=vp.vector(position[-1][0], position[-1][1], -1), size=vp.vector(16, 16, 2), color=vp.vector(0.2, 0.2, 0.2))

    rocket.normal = vp.cross(rocket.up, rocket.axis)


    #vp.attach_arrow(rocket, 'up', color=vp.color.green)
    #vp.attach_arrow(rocket, 'axis', color=vp.color.blue)
    #vp.attach_arrow(rocket, 'normal', color=vp.color.red)

    roll = 0
    vp.attach_trail(rocket, radius=rad/3, color=vp.color.red)

    sqr_out = [[-a, a],[a, a],[a, -a],[-a, -a], [-a, a]]
    sqr_in1 = [[-b, b - 3*c],[b, b - 3*c],[b, -b],[-b, -b], [-b, b - 3*c]]
    sqr_in2 = [[-b, b],[b, b], [b, b - 2*c],[-b, b - 2*c], [-b, b]]

    for step in range(2, steps):
        if not step % 5:
            a = 4
            c = 0.3
            b = a - c
            ref = vp.extrusion(path=[vp.vector(0, 0, 0), vp.vector(c, 0, 0)], shape=[sqr_out, sqr_in1, sqr_in2], axis=vp.vector(1, 0, 0), up=vp.vector(0, 1, 0), pos=vp.vector(position[step][0], position[step][1], position[step][2]))
            ref.up = vp.vector(0, 0, 1)
            ref.rotate(angle=orientation[step][0], axis=vp.cross(ref.axis, ref.up))
            ref.rotate(angle=orientation[step][1], axis=ref.up)
            ref.rotate(angle=orientation[step][2], axis=ref.axis)

    render.caption = "Running"
    render.visible = True
    for step in range(steps):
        x = position[step][0]
        y = position[step][1]
        z = position[step][2]

        rocket.normal = vp.cross(rocket.axis, rocket.up)

        pitch, yaw, roll = orientation[step][0], orientation[step][1], orientation[step][2]

        COM_x = x + COM[step] * np.cos(yaw) * np.cos(pitch)
        COM_y = y + COM[step] * np.sin(yaw) * np.cos(pitch)
        COM_z = z + COM[step] * np.sin(pitch)

        COP_x = x + COP[step] * np.cos(yaw) * np.cos(pitch)
        COP_y = y + COP[step] * np.sin(yaw) * np.cos(pitch)
        COP_z = z + COP[step] * np.sin(pitch)

        thrust_x = x + (-l) * np.cos(yaw) * np.cos(pitch)
        thrust_y = y + (-l) * np.sin(yaw) * np.cos(pitch)
        thrust_z = z + (-l) * np.sin(pitch)

        thrust_mag = np.linalg.norm(thrust[step]) * force_scale
        thrust_ax_x = thrust_mag * np.cos(yaw) * np.cos(pitch)
        thrust_ax_y = thrust_mag * np.sin(yaw) * np.cos(pitch)
        thrust_ax_z = thrust_mag * np.sin(pitch)

        lift_mag = lift[step][0] * force_scale
        lift_ax_x = 0
        lift_ax_y = 0
        lift_ax_z = lift_mag

        drag_mag = np.linalg.norm(drag[step]) * force_scale
        print(lift_mag)
        drag_ax_x = drag_mag
        drag_ax_y = 0
        drag_ax_z = 0

        gravity_mag = np.linalg.norm(gravity[step]) * force_scale

        thrust_pointer.pos = vp.vector(thrust_x, thrust_y, thrust_z)
        thrust_pointer.axis = vp.vector(thrust_ax_x, thrust_ax_y, thrust_ax_z)

        gravity_pointer.pos = vp.vector(COM_x, COM_y, COM_z)
        gravity_pointer.axis = vp.vector(0, 0, -gravity_mag)

        lift_pointer.pos = vp.vector(COP_x, COP_y, COP_z)
        lift_pointer.axis = vp.vector(lift_ax_x, lift_ax_y, lift_ax_z)

        drag_pointer.pos = vp.vector(COP_x, COP_y, COP_z)
        drag_pointer.axis = vp.vector(drag_ax_x,drag_ax_y, drag_ax_z)

        rocket.rotate(angle=pitch, axis=rocket.normal)
        rocket.rotate(angle=yaw, axis=rocket.up)
        rocket.rotate(angle=roll, axis=rocket.axis)

        COM_sphere.pos = vp.vector(COM_x, COM_y, COM_z)
        COP_sphere.pos = vp.vector(COP_x, COP_y, COP_z)

        rocket.pos = vp.vector(x, y, z)
        vp.sleep(1/sample_rate)
        if step + 1 != steps:
            rocket.rotate(angle=-roll, axis=rocket.axis)
            rocket.rotate(angle=-yaw, axis=rocket.up)
            rocket.rotate(angle=-pitch, axis=rocket.normal)

    render.caption = "Done"