I've been doing some speed tests to model some collision 'physics' in sketchup and found these methods interesting. My collision model is based on 6 vertices projected out of the camera point to detect surrounding objects (to save overhead). The total time I was aiming for was below 0.04 seconds to produce a decent frame rate. I started out with this script using view.inputpoint to guess the target. Its a translation of guess_target from the Film & Stage Plugin(camera.rb);
Sketchup.active_model.start_operation("collision",true);
view = Sketchup.active_model.active_view;
t=Time.now;
eye = view.camera.eye;
10.times {
target = Geom;;Point3d.new(rand(10),rand(10),rand(10));
view.camera.set(eye, target, Geom;;Vector3d.new(0,0,1));
center_pt = view.center;
input_point = view.inputpoint center_pt.x, center_pt.y;
};
puts Time.now - t;
Sketchup.active_model.abort_operation;
This produced on average over 10 iterations of 0.4136 seconds.
I compared this with the raytest method which removed the need to update the camera:
Sketchup.active_model.start_operation("collision",true);
view = Sketchup.active_model.active_view;
t=Time.now;
eye = view.camera.eye;
up = Geom;;Vector3d.new(0,0,1);
10.times {
target = Geom;;Point3d.new(rand(10),rand(10),rand(10));
ray = [eye, (eye.vector_to target)];
result = Sketchup.active_model.raytest( ray );
};
puts Time.now - t;
Sketchup.active_model.abort_operation;
This produced an average over 10 iterations of 0.1395 seconds.
I also noted that setting the second variable of raytest to false saves some more time. setting raytest to (ray,false) produced an average of 0.1363 seconds.
Since its a collision test, we've got a maximum distance that the collision 'physics' will cut in at, so we don't need to get any points beyond this. So performing raytest beyond this set distance to get a 3d point would be pointless. So I thought setting a maximum raytest distance by writing lines along each ray at this distance would stop raytest in its tracks. So this is what the script below does:
Sketchup.active_model.start_operation("collision",true);
entities = Sketchup.active_model.active_entities;
group = entities.add_group;
entities = group.entities;
view = Sketchup.active_model.active_view;
t=Time.now;
eye = view.camera.eye;
up = Geom;;Vector3d.new(0,0,1);
10.times {
target_vector = Geom;;Vector3d.new(1+rand(10),1+rand(10),1+rand(10));
target = eye + target_vector;
vector = eye.vector_to target;
vector.reverse!;
vector.normalize!;
target2 = target + vector;
line = entities.add_line target,target2;
ray = [eye, target_vector];
result = Sketchup.active_model.raytest(ray,false);
};
puts Time.now - t;
Sketchup.active_model.abort_operation;
This produced an average of 0.0219 seconds when in clear space, and an average of 0.1382 when close to an entity.
Its still a massive difference between each, and since the first only intersects with vertices, there's probably some large overhead when getting an intersection on a face? I could use this to simulate dead collisions, but any bouncing or parallel strafing would be quite slow.
Anybody got any suggestions or experience with the matter? I'm at a bit of a dead end.
-niall