296 lines
9.2 KiB
Text
296 lines
9.2 KiB
Text
|
|
||
|
class PhysicsTestBase extends MainLoopScripted {
|
||
|
|
||
|
|
||
|
bodies=[]
|
||
|
type_mesh_map={}
|
||
|
type_shape_map={}
|
||
|
cameratr=Transform()
|
||
|
|
||
|
|
||
|
function body_changed_transform(p_transform, p_velocity, p_angular_velocity,p_sleeping, p_visual_instance) {
|
||
|
|
||
|
VisualServer.instance_set_transform(p_visual_instance,p_transform);
|
||
|
}
|
||
|
|
||
|
|
||
|
function create_body(p_shape, p_body_mode, p_location, p_active=true) {
|
||
|
|
||
|
local mesh_instance = VisualServer.instance_create( type_mesh_map[p_shape] )
|
||
|
local body = PhysicsServer.body_create(RID(),p_body_mode,!p_active)
|
||
|
PhysicsServer.body_add_shape(body,type_shape_map[p_shape])
|
||
|
|
||
|
local query = PhysicsServer.query_create(this,"body_changed_transform",mesh_instance)
|
||
|
PhysicsServer.query_body_state(query, body)
|
||
|
|
||
|
PhysicsServer.body_set_state( body, PhysicsServer.BODY_STATE_TRANSFORM, p_location )
|
||
|
bodies.append( body )
|
||
|
return body
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
function create_static_plane(p_plane) {
|
||
|
|
||
|
local plane_shape = PhysicsServer.shape_create(PhysicsServer.SHAPE_PLANE)
|
||
|
PhysicsServer.shape_set_data( plane_shape, p_plane );
|
||
|
|
||
|
local b = PhysicsServer.body_create(RID(), PhysicsServer.BODY_MODE_STATIC );
|
||
|
PhysicsServer.body_add_shape(b, plane_shape);
|
||
|
return b;
|
||
|
}
|
||
|
|
||
|
function configure_body( p_body, p_mass, p_friction, p_bounce) {
|
||
|
|
||
|
PhysicsServer.body_set_param( p_body, PhysicsServer.BODY_PARAM_MASS, p_mass );
|
||
|
PhysicsServer.body_set_param( p_body, PhysicsServer.BODY_PARAM_FRICTION, p_friction );
|
||
|
PhysicsServer.body_set_param( p_body, PhysicsServer.BODY_PARAM_BOUNCE, p_bounce );
|
||
|
|
||
|
}
|
||
|
|
||
|
function init_shapes() {
|
||
|
|
||
|
|
||
|
/* SPHERE SHAPE */
|
||
|
local sphere_mesh = VisualServer.make_sphere_mesh(10,20,1.0);
|
||
|
local sphere_material = VisualServer.fixed_material_create();
|
||
|
//VisualServer.material_set_flag( sphere_material, VisualServer.MATERIAL_FLAG_WIREFRAME, true );
|
||
|
VisualServer.fixed_material_set_parameter( sphere_material, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.7,0.8,3.0) );
|
||
|
VisualServer.mesh_surface_set_material( sphere_mesh, 0, sphere_material );
|
||
|
type_mesh_map[PhysicsServer.SHAPE_SPHERE] <- sphere_mesh;
|
||
|
|
||
|
local sphere_shape=PhysicsServer.shape_create(PhysicsServer.SHAPE_SPHERE);
|
||
|
PhysicsServer.shape_set_data( sphere_shape, 1.0 );
|
||
|
type_shape_map[PhysicsServer.SHAPE_SPHERE] <- sphere_shape;
|
||
|
|
||
|
/* BOX SHAPE */
|
||
|
|
||
|
local box_planes = GeometryUtils.build_box_planes(Vector3(0.5,0.5,0.5));
|
||
|
local box_material = VisualServer.fixed_material_create();
|
||
|
VisualServer.fixed_material_set_parameter( box_material, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(1.0,0.2,0.2) );
|
||
|
local box_mesh = VisualServer.mesh_create();
|
||
|
|
||
|
VisualServer.mesh_add_surface_from_planes(box_mesh,box_planes);
|
||
|
VisualServer.mesh_surface_set_material( box_mesh, 0, box_material );
|
||
|
type_mesh_map[PhysicsServer.SHAPE_BOX]<- box_mesh;
|
||
|
|
||
|
local box_shape=PhysicsServer.shape_create(PhysicsServer.SHAPE_BOX);
|
||
|
PhysicsServer.shape_set_data( box_shape, Vector3(0.5,0.5,0.5) );
|
||
|
type_shape_map[PhysicsServer.SHAPE_BOX]<- box_shape;
|
||
|
|
||
|
/* CYLINDER SHAPE */
|
||
|
|
||
|
local cylinder_planes = GeometryUtils.build_cylinder_planes(0.5,1,12
|
||
|
,Vector3.AXIS_Z);
|
||
|
local cylinder_material = VisualServer.fixed_material_create();
|
||
|
VisualServer.fixed_material_set_parameter( cylinder_material, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.3,0.4,1.0) );
|
||
|
local cylinder_mesh = VisualServer.mesh_create();
|
||
|
|
||
|
VisualServer.mesh_add_surface_from_planes(cylinder_mesh,cylinder_planes);
|
||
|
VisualServer.mesh_surface_set_material( cylinder_mesh, 0, cylinder_material );
|
||
|
type_mesh_map[PhysicsServer.SHAPE_CYLINDER]<- cylinder_mesh;
|
||
|
|
||
|
local cylinder_shape=PhysicsServer.shape_create(PhysicsServer.SHAPE_CYLINDER);
|
||
|
local cylinder_params={}
|
||
|
cylinder_params["radius"]<- 0.5;
|
||
|
cylinder_params["height"]<- 2;
|
||
|
PhysicsServer.shape_set_data( cylinder_shape, cylinder_params );
|
||
|
type_shape_map[PhysicsServer.SHAPE_CYLINDER]<- cylinder_shape;
|
||
|
|
||
|
/* CAPSULE SHAPE */
|
||
|
|
||
|
local capsule_planes = GeometryUtils.build_capsule_planes(0.5,0.7,12,Vector3.AXIS_Z);
|
||
|
local capsule_material = VisualServer.fixed_material_create();
|
||
|
VisualServer.fixed_material_set_parameter( capsule_material, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.3,0.4,1.0) );
|
||
|
|
||
|
local capsule_mesh = VisualServer.mesh_create();
|
||
|
|
||
|
VisualServer.mesh_add_surface_from_planes(capsule_mesh,capsule_planes);
|
||
|
VisualServer.mesh_surface_set_material( capsule_mesh, 0, capsule_material );
|
||
|
type_mesh_map[PhysicsServer.SHAPE_CAPSULE]<-capsule_mesh;
|
||
|
|
||
|
local capsule_shape=PhysicsServer.shape_create(PhysicsServer.SHAPE_CAPSULE);
|
||
|
local capsule_params={}
|
||
|
capsule_params["radius"]<- 0.5;
|
||
|
capsule_params["height"]<- 1.4;
|
||
|
PhysicsServer.shape_set_data( capsule_shape, capsule_params );
|
||
|
type_shape_map[PhysicsServer.SHAPE_CAPSULE]<- capsule_shape;
|
||
|
|
||
|
/* CONVEX SHAPE */
|
||
|
|
||
|
local convex_planes = GeometryUtils.build_cylinder_planes(0.5,0.7,5,Vector3.AXIS_Z);
|
||
|
local convex_material = VisualServer.fixed_material_create();
|
||
|
VisualServer.fixed_material_set_parameter( convex_material, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.8,0.2,0.9));
|
||
|
|
||
|
local convex_mesh = VisualServer.mesh_create();
|
||
|
VisualServer.mesh_add_surface_from_planes(convex_mesh,convex_planes);
|
||
|
VisualServer.mesh_surface_set_material( convex_mesh, 0, convex_material );
|
||
|
type_mesh_map[PhysicsServer.SHAPE_CONVEX_POLYGON]<- convex_mesh;
|
||
|
|
||
|
local convex_shape=PhysicsServer.shape_create(PhysicsServer.SHAPE_CONVEX_POLYGON);
|
||
|
PhysicsServer.shape_set_data( convex_shape, convex_planes );
|
||
|
type_shape_map[PhysicsServer.SHAPE_CONVEX_POLYGON]<- convex_shape;
|
||
|
|
||
|
}
|
||
|
|
||
|
function make_trimesh(p_faces,p_xform=Transform()) {
|
||
|
|
||
|
local trimesh_shape = PhysicsServer.shape_create(PhysicsServer.SHAPE_CONCAVE_POLYGON);
|
||
|
PhysicsServer.shape_set_data(trimesh_shape, p_faces);
|
||
|
p_faces=PhysicsServer.shape_get_data(trimesh_shape); // optimized one
|
||
|
normals=[]
|
||
|
|
||
|
for (i=0;i<p_faces.length()/3;i++) {
|
||
|
|
||
|
p=Plane( p_faces[i*3+0],p_faces[i*3+1], p_faces[i*3+2] );
|
||
|
normals.append(p.normal);
|
||
|
normals.append(p.normal);
|
||
|
normals.append(p.normal);
|
||
|
}
|
||
|
|
||
|
local trimesh_mesh = VisualServer.mesh_create();
|
||
|
VisualServer.mesh_add_surface(trimesh_mesh, VS::PRIMITIVE_TRIANGLES, VS::ARRAY_FORMAT_VERTEX|VS::ARRAY_FORMAT_NORMAL, p_faces.length() );
|
||
|
VisualServer.mesh_surface_set_array(trimesh_mesh,0,VS::ARRAY_VERTEX, p_faces );
|
||
|
VisualServer.mesh_surface_set_array(trimesh_mesh,0,VS::ARRAY_NORMAL, normals );
|
||
|
local trimesh_mat = VisualServer.fixed_material_create();
|
||
|
VisualServer.fixed_material_set_parameter( trimesh_mat, VisualServer.FIXED_MATERIAL_PARAM_DIFFUSE, Color(1.0,0.5,0.8));
|
||
|
//VisualServer.material_set_flag( trimesh_mat, VisualServer.MATERIAL_FLAG_UNSHADED,true);
|
||
|
VisualServer.mesh_surface_set_material( trimesh_mesh, 0, trimesh_mat );
|
||
|
|
||
|
local triins = VisualServer.instance_create(trimesh_mesh);
|
||
|
|
||
|
|
||
|
local tribody = PhysicsServer.body_create(RID(), PhysicsServer.BODY_MODE_STATIC);
|
||
|
PhysicsServer.body_add_shape(tribody, trimesh_shape);
|
||
|
tritrans = p_xform;
|
||
|
PhysicsServer.body_set_state( tribody, PhysicsServer.BODY_STATE_TRANSFORM, tritrans );
|
||
|
VisualServer.instance_set_transform( triins, tritrans );
|
||
|
//RID trimesh_material = VisualServer.fixed_material_create();
|
||
|
//VisualServer.material_generate( trimesh_material, Color(0.2,0.4,0.6) );
|
||
|
//VisualServer.mesh_surface_set_material( trimesh_mesh, 0, trimesh_material );
|
||
|
|
||
|
}
|
||
|
|
||
|
function make_grid(p_width,p_height,p_cellsize,p_cellheight,p_xform=Transform()) {
|
||
|
|
||
|
local grid = []
|
||
|
|
||
|
for (local i =0;i<p_width;i++) {
|
||
|
|
||
|
local row = []
|
||
|
|
||
|
for (local j=0;j<p_height;j++) {
|
||
|
|
||
|
local val = 1.0+Math.random(-p_cellheight, p_cellheight );
|
||
|
row.append(val)
|
||
|
}
|
||
|
grid.append(row)
|
||
|
}
|
||
|
|
||
|
local faces=[]
|
||
|
|
||
|
for (local i =1;i<p_width;i++) {
|
||
|
|
||
|
for (local j=1;j<p_height;j++) {
|
||
|
|
||
|
function MAKE_VERTEX(m_x,m_z) {
|
||
|
local v= Vector3( (m_x-p_width/2)*p_cellsize, grid[m_x][m_z], (m_z-p_height/2)*p_cellsize )
|
||
|
faces.push_back(v)
|
||
|
}
|
||
|
|
||
|
|
||
|
MAKE_VERTEX(i,j-1)
|
||
|
MAKE_VERTEX(i,j)
|
||
|
MAKE_VERTEX(i-1,j)
|
||
|
|
||
|
MAKE_VERTEX(i-1,j-1)
|
||
|
MAKE_VERTEX(i,j-1)
|
||
|
MAKE_VERTEX(i-1,j)
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
make_trimesh(faces,p_xform);
|
||
|
}
|
||
|
|
||
|
quit=false
|
||
|
|
||
|
transform = Transform()
|
||
|
camera=RID()
|
||
|
|
||
|
function init_internal() {}
|
||
|
function iteration_internal(time) {}
|
||
|
//public
|
||
|
|
||
|
|
||
|
function input_event(p_event) {
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
function request_quit() {
|
||
|
|
||
|
quit=true;
|
||
|
}
|
||
|
|
||
|
function init() {
|
||
|
|
||
|
init_shapes();
|
||
|
|
||
|
|
||
|
/* LIGHT */
|
||
|
local lightaux = VisualServer.light_create( VisualServer.LIGHT_DIRECTIONAL );
|
||
|
//VisualServer.light_set_color( lightaux, VisualServer.LIGHT_COLOR_AMBIENT, Color(0.0,0.0,0.0) );
|
||
|
VisualServer.light_set_shadow(lightaux,true);
|
||
|
local light = VisualServer.instance_create( lightaux );
|
||
|
local t=Transform()
|
||
|
t.rotate(Vector3(1.0,0,0),0.6);
|
||
|
VisualServer.instance_set_transform(light,t);
|
||
|
|
||
|
|
||
|
/* CAMERA */
|
||
|
|
||
|
camera = VisualServer.camera_create();
|
||
|
local viewport = VisualServer.viewport_create();
|
||
|
VisualServer.viewport_attach_camera( viewport, camera );
|
||
|
VisualServer.viewport_set_parent(viewport, RID() );
|
||
|
|
||
|
VisualServer.camera_set_perspective(camera,60,0.1,20.0);
|
||
|
cameratr=Transform( Matrix3(), Vector3(0,2,8))
|
||
|
VisualServer.camera_set_transform(camera,cameratr);
|
||
|
|
||
|
quit=false;
|
||
|
|
||
|
init_internal()
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
function iteration(p_time) {
|
||
|
|
||
|
// VisualServer.camera_set_transform(camera,cameratr);
|
||
|
iteration_internal(p_time)
|
||
|
|
||
|
return quit;
|
||
|
}
|
||
|
|
||
|
function idle(p_time) {
|
||
|
|
||
|
return quit;
|
||
|
}
|
||
|
|
||
|
|
||
|
function finish() {
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
constructor() {
|
||
|
MainLoopScripted.constructor();
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
return PhysicsTestBase;
|