Placing Products into 3d Truck - Using excel input file with dimensions

Perhaps the hardest part of this has nothing really to do with PlayCanvas - namely, what is the optimal packing arrangement in a container for an arbitrary set of packages. There are various threads online discussing the solution to this problem. For example:

You also need to figure out the best way to get the relevant data out of Excel and into PlayCanvas. You might just want to export to CSV and convert that to JSON before upload, say?

But assuming you know how to solve that, I’ll turn my attention to the parts that are relevant to PlayCanvas. You need to:

  1. Find/create a model of a flatbed truck and import it into your scene.
  2. Procedurally fill the trick with boxes of the size specified in the spreadsheet.

So you algorithm to generate the packages might create an array:

var packages = [];

A package might be defined as:

var package = {
    position: [ x, y, z ],
    rotation: [ x, y, z],
    scale: [ x, y, z ]
};

In other words, it’s position, rotation and scale (size) in 3D space. You can of course store other information about each package - perhaps its weight and color.

To create the visuals for your packages, you might do:

for (var i = 0; i < packages.length; i++) {
    var p = packages[i];
    var e = new pc.Entity();
    e.addComponent('model', { type: 'box' };
    e.setPosition(p.position[0], p.position[1], p.position[2]);
    e.setEulerAngles(p.rotation[0], p.rotation[1], p.rotation[2]);
    e.setLocalScale(p.scale[0], p.scale[1], p.scale[2]);
    this.app.root.addChild(e);
}
1 Like