Main Content

createViewpoint

Create custom view of 3D environment

Since R2025a

    Description

    createViewpoint(world) creates a view of the 3D environment specified by world, with default values. This function creates a Viewpoint object to define the view and saves the view as a field in the viewpoint structure. Access the fields in the viewpoint structure using the Viewpoints property of the sim3d.World object.

    If you do not create a viewpoint, then the Simulation 3D Viewer window displays the default view of the 3D environment from the field Default in the viewpoint structure. To set the created view as the view for the 3D environment in the Simulation 3D Viewer window, use the setView function. You can use the arrow keys and pointer to navigate in the environment.

    createViewpoint(world,Name=Value) specifies additional options using one or more name-value arguments to create a custom view of the sim3d.World object specified by world. You can also use the name-value arguments to set the properties of the Viewpoint object. For example, to create a viewpoint named View1 at (5,0,2), set Name to View1 and Translation to [5 0 2].

    example

    Examples

    collapse all

    Create a custom view of the 3D environment using the createViewpoint function. Use the setView function to set the view in the Simulation 3D Viewer window.

    Create a world object using the sim3d.World object.

    world = sim3d.World();

    Create a box actor in the 3D environment using the sim3d.Actor object. Add the box actor to the world.

    box = sim3d.Actor(ActorName='Box');
    createShape(box,'box',[0.5 0.25 0.25]);
    box.Color = [1 0 0];
    add(world,box);

    Create a custom view using the createViewpoint function.

    view = createViewpoint(world);
    view.Name = 'View';
    view.Translation = [-4 0 0];

    Set the view of the 3D environment to display in the Simulation 3D Viewer window. If you do not set the view, the software sets the default view.

    setView(world,view);

    Run the simulation.

    sampletime = 0.01;
    stoptime = 10;
    run(world,sampletime,stoptime);

    Box actor in 3D environment.

    Create multiple views of the 3D environment using the createViewpoint function. Use the setView function to set the view in the Simulation 3D Viewer window.

    Create a world object using the sim3d.World object.

    world = sim3d.World(Output=@outputImpl);

    Create a box actor in the 3D environment using the sim3d.Actor object. Add the box actor to the world.

    box = sim3d.Actor(ActorName='Box');
    createShape(box,'box',[0.5 0.25 0.25]);
    box.Color = [1 0 0];
    add(world,box);

    Create multiple custom views using the createViewpoint function.

    view1 = createViewpoint( ...
        world,Name='Front',Translation=[-4 0 0]);
    view2 = createViewpoint( ...
        world,Name='Right',Translation=[0 4 0],Rotation=[0 0 -pi/2]);
    view3 = createViewpoint( ...
        world,Name='Back',Translation=[4 0 0],Rotation=[0 0 pi]);
    view4 = createViewpoint( ...
        world,Name='Left',Translation=[0 -4 0],Rotation=[0 0 pi/2]);

    Using the UserData property of the sim3d.World object, create a user data structure with a field named Step to store the simulation step during simulation.

    world.UserData.Step = 1;

    Run the simulation. The viewer displays the default view.

    sampletime = 0.01;
    stoptime = 10;
    run(world,sampletime,stoptime);

    View2 of the box actor in 3D environment.

    Output Function

    Use the output function to set the view in the Simulation 3D Viewer window at a specific simulation step. The Front and Back views display a square. The Right and Left views display a rectangle.

    function outputImpl(world)
        if world.UserData.Step == 200
            setView(world,world.Viewpoints.Front);
        elseif world.UserData.Step == 400
            setView(world,world.Viewpoints.Right);
        elseif world.UserData.Step == 600
            setView(world,world.Viewpoints.Back);
        elseif world.UserData.Step == 800
           setView(world,world.Viewpoints.Left);       
        end
        world.UserData.Step = world.UserData.Step + 1;
    end

    Input Arguments

    collapse all

    World object that defines the 3D environment, specified as a sim3d.World object.

    Example: world = sim3d.World()

    Name-Value Arguments

    collapse all

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: createViewpoint(world,Name='View1',Translation=[1 2 3])

    Name of viewpoint, specified as a character array or string. If you do not specify the name, then the software assigns the viewpoint an autogenerated name, such as Viewpoint1 and so on. Use this argument to set the field name of the viewpoint in the viewpoint structure.

    Note

    If you specify the same name as a viewpoint that already exists, then the software appends the viewpoint name you specify with a unique identifier.

    Example: Name="View1"

    Relative translation (x,y,z) of the viewpoint to the Scene Origin at (0,0,0), specified as a real 1-by-3 vector, in m. Use Translation to change the position of the viewpoint in the 3D environment along the X, Y, and Z axes of the coordinate system.

    Example: Translation=[3 4 3]

    Data Types: double

    Relative rotation (roll, pitch, yaw) of the viewpoint to the Scene Origin at (0,0,0), specified as a real 1-by-3 vector, in rad. Use Rotation to rotate the viewpoint in the 3D environment.

    Example: Rotation=[0 pi/2 pi/4]

    Data Types: double

    Version History

    Introduced in R2025a