Exemple #1
0
Matrix32 Node2D::get_relative_transform(const Node *p_parent) const {

	if (p_parent==this)
		return Matrix32();

	Node2D *parent_2d = get_parent()->cast_to<Node2D>();
	ERR_FAIL_COND_V(!parent_2d,Matrix32());
	if (p_parent==parent_2d)
		return get_transform();
	else
		return parent_2d->get_relative_transform(p_parent) * get_transform();
}
void ControlEditor::_update_scroll(float) {

	if (updating_scroll)
		return;

	if (!current_window)
		return;

	Point2 ofs;
	ofs.x=h_scroll->get_val();
	ofs.y=v_scroll->get_val();

//	current_window->set_scroll(-ofs);

	transform=Matrix32();

	transform.scale_basis(Size2(zoom,zoom));
	transform.elements[2]=-ofs*zoom;


	RID viewport = editor->get_scene_root()->get_viewport();

	VisualServer::get_singleton()->viewport_set_global_canvas_transform(viewport,transform);

	update();

}
Matrix32 Physics2DServerSW::area_get_transform(RID p_area) const {

	Area2DSW *area = area_owner.get(p_area);
	ERR_FAIL_COND_V(!area,Matrix32());

	return area->get_transform();
};
Matrix32 Physics2DServerSW::body_get_shape_transform(RID p_body, int p_shape_idx) const {

	Body2DSW *body = body_owner.get(p_body);
	ERR_FAIL_COND_V(!body,Matrix32());

	return body->get_shape_transform(p_shape_idx);
}
Exemple #5
0
void Body2DSW::integrate_velocities(real_t p_step) {

	if (mode==Physics2DServer::BODY_MODE_STATIC)
		return;

	if (fi_callback)
		get_space()->body_add_to_state_query_list(&direct_state_query_list);

	if (mode==Physics2DServer::BODY_MODE_KINEMATIC) {

		_set_transform(new_transform,false);
		_set_inv_transform(new_transform.affine_inverse());
		if (contacts.size()==0 && linear_velocity==Vector2() && angular_velocity==0)
			set_active(false); //stopped moving, deactivate
		return;
	}

	real_t total_angular_velocity = angular_velocity+biased_angular_velocity;
	Vector2 total_linear_velocity=linear_velocity+biased_linear_velocity;

	real_t angle = get_transform().get_rotation() - total_angular_velocity * p_step;
	Vector2 pos = get_transform().get_origin() + total_linear_velocity * p_step;

	_set_transform(Matrix32(angle,pos),continuous_cd_mode==Physics2DServer::CCD_MODE_DISABLED);
	_set_inv_transform(get_transform().inverse());

	if (continuous_cd_mode!=Physics2DServer::CCD_MODE_DISABLED)
		new_transform=get_transform();

	//_update_inertia_tensor();
}
Exemple #6
0
Matrix32 TileMap::get_cell_transform() const {

	switch(mode) {

		case MODE_SQUARE: {

			Matrix32 m;
			m[0]*=cell_size.x;
			m[1]*=cell_size.y;
			return m;
		} break;
		case MODE_ISOMETRIC: {

			//isometric only makes sense when y is positive in both x and y vectors, otherwise
			//the drawing of tiles will overlap
			Matrix32 m;
			m[0]=Vector2(cell_size.x*0.5,cell_size.y*0.5);
			m[1]=Vector2(-cell_size.x*0.5,cell_size.y*0.5);
			return m;

		} break;
		case MODE_CUSTOM: {

			return custom_transform;
		} break;
	}

	return Matrix32();
}
Exemple #7
0
void Viewport::_update_stretch_transform() {

	if (size_override_stretch && size_override) {

		stretch_transform=Matrix32();
		stretch_transform.scale(rect.size/(size_override_size+size_override_margin*2));
		stretch_transform.elements[2]=size_override_margin;

	} else {

		stretch_transform=Matrix32();
	}

	_update_global_transform();

}
Exemple #8
0
void Camera2D::_notification(int p_what) {

	switch(p_what) {

		case NOTIFICATION_FIXED_PROCESS: {

			_update_scroll();

		} break;
		case NOTIFICATION_TRANSFORM_CHANGED: {


			if (!is_fixed_processing())
				_update_scroll();

		} break;
		case NOTIFICATION_ENTER_SCENE: {

			viewport = NULL;
			Node *n=this;
			while(n){

				viewport = n->cast_to<Viewport>();
				if (viewport)
					break;
				n=n->get_parent();
			}

			canvas = get_canvas();

			RID vp = viewport->get_viewport();

			group_name = "__cameras_"+itos(vp.get_id());
			canvas_group_name ="__cameras_c"+itos(canvas.get_id());
			add_to_group(group_name);
			add_to_group(canvas_group_name);

			_update_scroll();
			first=true;


		} break;
		case NOTIFICATION_EXIT_SCENE: {

			if (is_current()) {
				if (viewport) {
					viewport->set_canvas_transform( Matrix32() );
				}
			}
			remove_from_group(group_name);
			remove_from_group(canvas_group_name);
			viewport=NULL;

		} break;
	}
}
Exemple #9
0
Matrix32 CanvasItem::get_viewport_transform() const {

	ERR_FAIL_COND_V(!is_inside_tree(),Matrix32());

	if (canvas_layer) {

		if (get_viewport()) {
			return get_viewport()->get_final_transform() * canvas_layer->get_transform();
		} else {
			return canvas_layer->get_transform();
		}

	} else if (get_viewport()) {
		return get_viewport()->get_final_transform() * get_viewport()->get_canvas_transform();
	}

	return Matrix32();

}
Exemple #10
0
Matrix32 CanvasItem::get_canvas_transform() const {

	ERR_FAIL_COND_V(!is_inside_tree(),Matrix32());

	if (canvas_layer)
		return canvas_layer->get_transform();
	else
		return get_viewport()->get_canvas_transform();

}
Exemple #11
0
void Viewport::set_canvas_transform(const Matrix32& p_transform) {

	canvas_transform=p_transform;
	VisualServer::get_singleton()->viewport_set_canvas_transform(viewport,find_world_2d()->get_canvas(),canvas_transform);

	Matrix32 xform = (global_canvas_transform * canvas_transform).affine_inverse();
	Size2 ss = get_visible_rect().size;
	SpatialSound2DServer::get_singleton()->listener_set_transform(listener_2d,Matrix32(0,xform.xform(ss*0.5)));
	Vector2 ss2 = ss*xform.get_scale();
	float panrange = MAX(ss2.x,ss2.y);

	SpatialSound2DServer::get_singleton()->listener_set_param(listener_2d,SpatialSound2DServer::LISTENER_PARAM_PAN_RANGE,panrange);


}
Exemple #12
0
void Viewport::_update_global_transform() {


	Matrix32 sxform = stretch_transform * global_canvas_transform;

	VisualServer::get_singleton()->viewport_set_global_canvas_transform(viewport,sxform);

	Matrix32 xform = (sxform * canvas_transform).affine_inverse();
	Size2 ss = get_visible_rect().size;
	SpatialSound2DServer::get_singleton()->listener_set_transform(listener_2d,Matrix32(0,xform.xform(ss*0.5)));
	Vector2 ss2 = ss*xform.get_scale();
	float panrange = MAX(ss2.x,ss2.y);

	SpatialSound2DServer::get_singleton()->listener_set_param(listener_2d,SpatialSound2DServer::LISTENER_PARAM_PAN_RANGE,panrange);

}
Exemple #13
0
 Variant::operator Matrix32() const {

	 if (type==MATRIX32) {
		 return *_data._matrix32;
	 } else if (type==TRANSFORM) {
		 const Transform& t = *_data._transform;;
		 Matrix32 m;
		 m.elements[0][0]=t.basis.elements[0][0];
		 m.elements[0][1]=t.basis.elements[1][0];
		 m.elements[1][0]=t.basis.elements[0][1];
		 m.elements[1][1]=t.basis.elements[1][1];
		 m.elements[2][0]=t.origin[0];
		 m.elements[2][1]=t.origin[1];
		 return m;
	 } else
		 return Matrix32();
}
void Polygon2DEditor::_uv_draw() {

	Ref<Texture> base_tex = node->get_texture();
	if (base_tex.is_null())
		return;

	Matrix32 mtx;
	mtx.elements[2]=-uv_draw_ofs;
	mtx.scale_basis(Vector2(uv_draw_zoom,uv_draw_zoom));

	VS::get_singleton()->canvas_item_set_clip(uv_edit_draw->get_canvas_item(),true);
	VS::get_singleton()->canvas_item_add_set_transform(uv_edit_draw->get_canvas_item(),mtx);
	uv_edit_draw->draw_texture(base_tex,Point2());
	VS::get_singleton()->canvas_item_add_set_transform(uv_edit_draw->get_canvas_item(),Matrix32());

	DVector<Vector2> uvs = node->get_uv();
	Ref<Texture> handle = get_icon("EditorHandle","EditorIcons");

	Rect2 rect(Point2(),mtx.basis_xform(base_tex->get_size()));
	rect.expand_to(mtx.basis_xform(uv_edit_draw->get_size()));

	for(int i=0;i<uvs.size();i++) {

		int next = (i+1)%uvs.size();
		uv_edit_draw->draw_line(mtx.xform(uvs[i]),mtx.xform(uvs[next]),Color(0.9,0.5,0.5),2);
		uv_edit_draw->draw_texture(handle,mtx.xform(uvs[i])-handle->get_size()*0.5);
		rect.expand_to(mtx.basis_xform(uvs[i]));
	}

	rect=rect.grow(200);
	updating_uv_scroll=true;
	uv_hscroll->set_min(rect.pos.x);
	uv_hscroll->set_max(rect.pos.x+rect.size.x);
	uv_hscroll->set_page(uv_edit_draw->get_size().x);
	uv_hscroll->set_val(uv_draw_ofs.x);
	uv_hscroll->set_step(0.001);

	uv_vscroll->set_min(rect.pos.y);
	uv_vscroll->set_max(rect.pos.y+rect.size.y);
	uv_vscroll->set_page(uv_edit_draw->get_size().y);
	uv_vscroll->set_val(uv_draw_ofs.y);
	uv_vscroll->set_step(0.001);
	updating_uv_scroll=false;

}
Exemple #15
0
	void _add_concave(const Vector<Vector2> &p_points, const Matrix32 &p_xform = Matrix32()) {

		Physics2DServer *ps = Physics2DServer::get_singleton();
		VisualServer *vs = VisualServer::get_singleton();

		RID concave = ps->shape_create(Physics2DServer::SHAPE_CONCAVE_POLYGON);
		ps->shape_set_data(concave, p_points);
		RID body = ps->body_create(Physics2DServer::BODY_MODE_STATIC);
		ps->body_set_space(body, space);
		ps->body_add_shape(body, concave);
		ps->body_set_state(body, Physics2DServer::BODY_STATE_TRANSFORM, p_xform);

		RID sprite = vs->canvas_item_create();
		vs->canvas_item_set_parent(sprite, canvas);
		vs->canvas_item_set_transform(sprite, p_xform);
		for (int i = 0; i < p_points.size(); i += 2) {
			vs->canvas_item_add_line(sprite, p_points[i], p_points[i + 1], Color(0, 0, 0), 2);
		}
	}
Exemple #16
0
Variant::Variant(const Matrix32& p_transform) {

	type=MATRIX32;
	_data._matrix32 = memnew( Matrix32( p_transform ) );

}
Exemple #17
0
void Physics2DServer::_bind_methods() {


	ObjectTypeDB::bind_method(_MD("shape_create","type"),&Physics2DServer::shape_create);
	ObjectTypeDB::bind_method(_MD("shape_set_data","shape","data"),&Physics2DServer::shape_set_data);

	ObjectTypeDB::bind_method(_MD("shape_get_type","shape"),&Physics2DServer::shape_get_type);
	ObjectTypeDB::bind_method(_MD("shape_get_data","shape"),&Physics2DServer::shape_get_data);


	ObjectTypeDB::bind_method(_MD("space_create"),&Physics2DServer::space_create);
	ObjectTypeDB::bind_method(_MD("space_set_active","space","active"),&Physics2DServer::space_set_active);
	ObjectTypeDB::bind_method(_MD("space_is_active","space"),&Physics2DServer::space_is_active);
	ObjectTypeDB::bind_method(_MD("space_set_param","space","param","value"),&Physics2DServer::space_set_param);
	ObjectTypeDB::bind_method(_MD("space_get_param","space","param"),&Physics2DServer::space_get_param);
	ObjectTypeDB::bind_method(_MD("space_get_direct_state:Physics2DDirectSpaceState","space"),&Physics2DServer::space_get_direct_state);

	ObjectTypeDB::bind_method(_MD("area_create"),&Physics2DServer::area_create);
	ObjectTypeDB::bind_method(_MD("area_set_space","area","space"),&Physics2DServer::area_set_space);
	ObjectTypeDB::bind_method(_MD("area_get_space","area"),&Physics2DServer::area_get_space);

	ObjectTypeDB::bind_method(_MD("area_set_space_override_mode","area","mode"),&Physics2DServer::area_set_space_override_mode);
	ObjectTypeDB::bind_method(_MD("area_get_space_override_mode","area"),&Physics2DServer::area_get_space_override_mode);

	ObjectTypeDB::bind_method(_MD("area_add_shape","area","shape","transform"),&Physics2DServer::area_add_shape,DEFVAL(Matrix32()));
	ObjectTypeDB::bind_method(_MD("area_set_shape","area","shape_idx","shape"),&Physics2DServer::area_set_shape);
	ObjectTypeDB::bind_method(_MD("area_set_shape_transform","area","shape_idx","transform"),&Physics2DServer::area_set_shape_transform);

	ObjectTypeDB::bind_method(_MD("area_get_shape_count","area"),&Physics2DServer::area_get_shape_count);
	ObjectTypeDB::bind_method(_MD("area_get_shape","area","shape_idx"),&Physics2DServer::area_get_shape);
	ObjectTypeDB::bind_method(_MD("area_get_shape_transform","area","shape_idx"),&Physics2DServer::area_get_shape_transform);

	ObjectTypeDB::bind_method(_MD("area_remove_shape","area","shape_idx"),&Physics2DServer::area_remove_shape);
	ObjectTypeDB::bind_method(_MD("area_clear_shapes","area"),&Physics2DServer::area_clear_shapes);


	ObjectTypeDB::bind_method(_MD("area_set_param","area","param","value"),&Physics2DServer::area_set_param);
	ObjectTypeDB::bind_method(_MD("area_set_transform","area","transform"),&Physics2DServer::area_set_transform);

	ObjectTypeDB::bind_method(_MD("area_get_param","area","param"),&Physics2DServer::area_get_param);
	ObjectTypeDB::bind_method(_MD("area_get_transform","area"),&Physics2DServer::area_get_transform);

	ObjectTypeDB::bind_method(_MD("area_attach_object_instance_ID","area","id"),&Physics2DServer::area_attach_object_instance_ID);
	ObjectTypeDB::bind_method(_MD("area_get_object_instance_ID","area"),&Physics2DServer::area_get_object_instance_ID);

	ObjectTypeDB::bind_method(_MD("area_set_monitor_callback","receiver","method"),&Physics2DServer::area_set_monitor_callback);

	ObjectTypeDB::bind_method(_MD("body_create","mode","init_sleeping"),&Physics2DServer::body_create,DEFVAL(BODY_MODE_RIGID),DEFVAL(false));

	ObjectTypeDB::bind_method(_MD("body_set_space","body","space"),&Physics2DServer::body_set_space);
	ObjectTypeDB::bind_method(_MD("body_get_space","body"),&Physics2DServer::body_get_space);

	ObjectTypeDB::bind_method(_MD("body_set_mode","body","mode"),&Physics2DServer::body_set_mode);
	ObjectTypeDB::bind_method(_MD("body_get_mode","body"),&Physics2DServer::body_get_mode);

	ObjectTypeDB::bind_method(_MD("body_add_shape","body","shape","transform"),&Physics2DServer::body_add_shape,DEFVAL(Matrix32()));
	ObjectTypeDB::bind_method(_MD("body_set_shape","body","shape_idx","shape"),&Physics2DServer::body_set_shape);
	ObjectTypeDB::bind_method(_MD("body_set_shape_transform","body","shape_idx","transform"),&Physics2DServer::body_set_shape_transform);
	ObjectTypeDB::bind_method(_MD("body_set_shape_metadata","body","shape_idx","metadata"),&Physics2DServer::body_set_shape_metadata);

	ObjectTypeDB::bind_method(_MD("body_get_shape_count","body"),&Physics2DServer::body_get_shape_count);
	ObjectTypeDB::bind_method(_MD("body_get_shape","body","shape_idx"),&Physics2DServer::body_get_shape);
	ObjectTypeDB::bind_method(_MD("body_get_shape_transform","body","shape_idx"),&Physics2DServer::body_get_shape_transform);
	ObjectTypeDB::bind_method(_MD("body_get_shape_metadata","body","shape_idx"),&Physics2DServer::body_get_shape_metadata);

	ObjectTypeDB::bind_method(_MD("body_remove_shape","body","shape_idx"),&Physics2DServer::body_remove_shape);
	ObjectTypeDB::bind_method(_MD("body_clear_shapes","body"),&Physics2DServer::body_clear_shapes);


	ObjectTypeDB::bind_method(_MD("body_set_shape_as_trigger","body","shape_idx","enable"),&Physics2DServer::body_set_shape_as_trigger);
	ObjectTypeDB::bind_method(_MD("body_is_shape_set_as_trigger","body","shape_idx"),&Physics2DServer::body_is_shape_set_as_trigger);

	ObjectTypeDB::bind_method(_MD("body_attach_object_instance_ID","body","id"),&Physics2DServer::body_attach_object_instance_ID);
	ObjectTypeDB::bind_method(_MD("body_get_object_instance_ID","body"),&Physics2DServer::body_get_object_instance_ID);


	ObjectTypeDB::bind_method(_MD("body_set_continuous_collision_detection_mode","body","mode"),&Physics2DServer::body_set_continuous_collision_detection_mode);
	ObjectTypeDB::bind_method(_MD("body_get_continuous_collision_detection_mode","body"),&Physics2DServer::body_get_continuous_collision_detection_mode);


	ObjectTypeDB::bind_method(_MD("body_set_layer_mask","body","mask"),&Physics2DServer::body_set_layer_mask);
	ObjectTypeDB::bind_method(_MD("body_get_layer_mask","body"),&Physics2DServer::body_get_layer_mask);

	ObjectTypeDB::bind_method(_MD("body_set_user_mask","body","mask"),&Physics2DServer::body_set_user_mask);
	ObjectTypeDB::bind_method(_MD("body_get_user_mask","body"),&Physics2DServer::body_get_user_mask);


	ObjectTypeDB::bind_method(_MD("body_set_param","body","param","value"),&Physics2DServer::body_set_param);
	ObjectTypeDB::bind_method(_MD("body_get_param","body","param"),&Physics2DServer::body_get_param);

	ObjectTypeDB::bind_method(_MD("body_set_state","body","state","value"),&Physics2DServer::body_set_state);
	ObjectTypeDB::bind_method(_MD("body_get_state","body","state"),&Physics2DServer::body_get_state);

	ObjectTypeDB::bind_method(_MD("body_apply_impulse","body","pos","impulse"),&Physics2DServer::body_apply_impulse);
	ObjectTypeDB::bind_method(_MD("body_set_axis_velocity","body","axis_velocity"),&Physics2DServer::body_set_axis_velocity);

	ObjectTypeDB::bind_method(_MD("body_add_collision_exception","body","excepted_body"),&Physics2DServer::body_add_collision_exception);
	ObjectTypeDB::bind_method(_MD("body_remove_collision_exception","body","excepted_body"),&Physics2DServer::body_remove_collision_exception);
//	virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions)=0;

	ObjectTypeDB::bind_method(_MD("body_set_max_contacts_reported","body","amount"),&Physics2DServer::body_set_max_contacts_reported);
	ObjectTypeDB::bind_method(_MD("body_get_max_contacts_reported","body"),&Physics2DServer::body_get_max_contacts_reported);

	ObjectTypeDB::bind_method(_MD("body_set_one_way_collision_direction","normal"),&Physics2DServer::body_set_one_way_collision_direction);
	ObjectTypeDB::bind_method(_MD("body_get_one_way_collision_direction"),&Physics2DServer::body_get_one_way_collision_direction);

	ObjectTypeDB::bind_method(_MD("body_set_one_way_collision_max_depth","normal"),&Physics2DServer::body_set_one_way_collision_max_depth);
	ObjectTypeDB::bind_method(_MD("body_get_one_way_collision_max_depth"),&Physics2DServer::body_get_one_way_collision_max_depth);


	ObjectTypeDB::bind_method(_MD("body_set_omit_force_integration","body","enable"),&Physics2DServer::body_set_omit_force_integration);
	ObjectTypeDB::bind_method(_MD("body_is_omitting_force_integration","body"),&Physics2DServer::body_is_omitting_force_integration);

	ObjectTypeDB::bind_method(_MD("body_set_force_integration_callback","body","receiver","method"),&Physics2DServer::body_set_force_integration_callback);

	/* JOINT API */

	ObjectTypeDB::bind_method(_MD("joint_set_param","joint","param","value"),&Physics2DServer::joint_set_param);
	ObjectTypeDB::bind_method(_MD("joint_get_param","joint","param"),&Physics2DServer::joint_get_param);

	ObjectTypeDB::bind_method(_MD("pin_joint_create","anchor","body_a","body_b"),&Physics2DServer::pin_joint_create,DEFVAL(RID()));
	ObjectTypeDB::bind_method(_MD("groove_joint_create","groove1_a","groove2_a","anchor_b","body_a","body_b"),&Physics2DServer::groove_joint_create,DEFVAL(RID()),DEFVAL(RID()));
	ObjectTypeDB::bind_method(_MD("damped_spring_joint_create","anchor_a","anchor_b","body_a","body_b"),&Physics2DServer::damped_spring_joint_create,DEFVAL(RID()));

	ObjectTypeDB::bind_method(_MD("damped_string_joint_set_param","joint","param","value"),&Physics2DServer::damped_string_joint_set_param,DEFVAL(RID()));
	ObjectTypeDB::bind_method(_MD("damped_string_joint_get_param","joint","param"),&Physics2DServer::damped_string_joint_get_param);

	ObjectTypeDB::bind_method(_MD("joint_get_type","joint"),&Physics2DServer::joint_get_type);

	ObjectTypeDB::bind_method(_MD("free_rid","rid"),&Physics2DServer::free);

	ObjectTypeDB::bind_method(_MD("set_active","active"),&Physics2DServer::set_active);

	ObjectTypeDB::bind_method(_MD("get_process_info"),&Physics2DServer::get_process_info);

//	ObjectTypeDB::bind_method(_MD("init"),&Physics2DServer::init);
//	ObjectTypeDB::bind_method(_MD("step"),&Physics2DServer::step);
//	ObjectTypeDB::bind_method(_MD("sync"),&Physics2DServer::sync);
	//ObjectTypeDB::bind_method(_MD("flush_queries"),&Physics2DServer::flush_queries);

	BIND_CONSTANT( SHAPE_LINE );
	BIND_CONSTANT( SHAPE_SEGMENT );
	BIND_CONSTANT( SHAPE_CIRCLE );
	BIND_CONSTANT( SHAPE_RECTANGLE );
	BIND_CONSTANT( SHAPE_CAPSULE );
	BIND_CONSTANT( SHAPE_CONVEX_POLYGON );
	BIND_CONSTANT( SHAPE_CONCAVE_POLYGON );
	BIND_CONSTANT( SHAPE_CUSTOM );

	BIND_CONSTANT( AREA_PARAM_GRAVITY );
	BIND_CONSTANT( AREA_PARAM_GRAVITY_VECTOR );
	BIND_CONSTANT( AREA_PARAM_GRAVITY_IS_POINT );
	BIND_CONSTANT( AREA_PARAM_GRAVITY_POINT_ATTENUATION );
	BIND_CONSTANT( AREA_PARAM_LINEAR_DAMP);
	BIND_CONSTANT( AREA_PARAM_ANGULAR_DAMP);
	BIND_CONSTANT( AREA_PARAM_PRIORITY );

	BIND_CONSTANT( AREA_SPACE_OVERRIDE_COMBINE );
	BIND_CONSTANT( AREA_SPACE_OVERRIDE_DISABLED );
	BIND_CONSTANT( AREA_SPACE_OVERRIDE_REPLACE );

	BIND_CONSTANT( BODY_MODE_STATIC );
	BIND_CONSTANT( BODY_MODE_KINEMATIC );
	BIND_CONSTANT( BODY_MODE_RIGID );
	BIND_CONSTANT( BODY_MODE_CHARACTER );

	BIND_CONSTANT( BODY_PARAM_BOUNCE );
	BIND_CONSTANT( BODY_PARAM_FRICTION );
	BIND_CONSTANT( BODY_PARAM_MASS );
	BIND_CONSTANT( BODY_PARAM_GRAVITY_SCALE );
	BIND_CONSTANT( BODY_PARAM_LINEAR_DAMP);
	BIND_CONSTANT( BODY_PARAM_ANGULAR_DAMP);
	BIND_CONSTANT( BODY_PARAM_MAX );

	BIND_CONSTANT( BODY_STATE_TRANSFORM );
	BIND_CONSTANT( BODY_STATE_LINEAR_VELOCITY );
	BIND_CONSTANT( BODY_STATE_ANGULAR_VELOCITY );
	BIND_CONSTANT( BODY_STATE_SLEEPING );
	BIND_CONSTANT( BODY_STATE_CAN_SLEEP );

	BIND_CONSTANT( JOINT_PIN );
	BIND_CONSTANT( JOINT_GROOVE );
	BIND_CONSTANT( JOINT_DAMPED_SPRING );

	BIND_CONSTANT( DAMPED_STRING_REST_LENGTH );
	BIND_CONSTANT( DAMPED_STRING_STIFFNESS );
	BIND_CONSTANT( DAMPED_STRING_DAMPING );

	BIND_CONSTANT( CCD_MODE_DISABLED );
	BIND_CONSTANT( CCD_MODE_CAST_RAY );
	BIND_CONSTANT( CCD_MODE_CAST_SHAPE );

//	BIND_CONSTANT( TYPE_BODY );
//	BIND_CONSTANT( TYPE_AREA );

	BIND_CONSTANT( AREA_BODY_ADDED );
	BIND_CONSTANT( AREA_BODY_REMOVED );

	BIND_CONSTANT( INFO_ACTIVE_OBJECTS );
	BIND_CONSTANT( INFO_COLLISION_PAIRS );
	BIND_CONSTANT( INFO_ISLAND_COUNT );


}
Exemple #18
0
bool Variant::is_zero() const {

	switch( type ) {
		case NIL: {

			return true;
		} break;

		// atomic types
		case BOOL: {

			return _data._bool==false;
		} break;
		case INT: {

			return _data._int==0;

		} break;
		case REAL: {

			return _data._real==0;

		} break;
		case STRING: {

			return *reinterpret_cast<const String*>(_data._mem)==String();

		} break;

		// math types

		case VECTOR2: {

			return *reinterpret_cast<const Vector2*>(_data._mem)==Vector2();

		} break;
		case RECT2: {

			return *reinterpret_cast<const Rect2*>(_data._mem)==Rect2();

		} break;
		case MATRIX32: {

			return *_data._matrix32==Matrix32();

		} break;
		case VECTOR3: {

			return *reinterpret_cast<const Vector3*>(_data._mem)==Vector3();

		} break;
		case PLANE: {

			return *reinterpret_cast<const Plane*>(_data._mem)==Plane();

		} break;
/*
		case QUAT: {


		} break;*/
		case _AABB: {

			return *_data._aabb==AABB();
		} break;
		case QUAT: {

			*reinterpret_cast<const Quat*>(_data._mem)==Quat();

		} break;
		case MATRIX3: {

			return *_data._matrix3==Matrix3();

		} break;
		case TRANSFORM: {

			return *_data._transform == Transform();

		} break;

		// misc types
		case COLOR: {

			return *reinterpret_cast<const Color*>(_data._mem)==Color();

		} break;
		case IMAGE: {

			return _data._image->empty();

		} break;
		case _RID: {

			return *reinterpret_cast<const RID*>(_data._mem)==RID();
		} break;
		case OBJECT: {

			return _get_obj().obj==NULL;
		} break;
		case NODE_PATH: {

			return reinterpret_cast<const NodePath*>(_data._mem)->is_empty();

		} break;
		case INPUT_EVENT: {

			return _data._input_event->type==InputEvent::NONE;

		} break;
		case DICTIONARY: {

			return reinterpret_cast<const Dictionary*>(_data._mem)->empty();

		} break;
		case ARRAY: {

			return reinterpret_cast<const Array*>(_data._mem)->empty();

		} break;

		// arrays
		case RAW_ARRAY: {

			return reinterpret_cast<const DVector<uint8_t>*>(_data._mem)->size()==0;

		} break;
		case INT_ARRAY: {

			return reinterpret_cast<const DVector<int>*>(_data._mem)->size()==0;

		} break;
		case REAL_ARRAY: {

			return reinterpret_cast<const DVector<real_t>*>(_data._mem)->size()==0;

		} break;
		case STRING_ARRAY: {

			return reinterpret_cast<const DVector<String>*>(_data._mem)->size()==0;

		} break;
		case VECTOR2_ARRAY: {

			return reinterpret_cast<const DVector<Vector2>*>(_data._mem)->size()==0;

		} break;
		case VECTOR3_ARRAY: {

			return reinterpret_cast<const DVector<Vector3>*>(_data._mem)->size()==0;

		} break;
		case COLOR_ARRAY: {

			return reinterpret_cast<const DVector<Color>*>(_data._mem)->size()==0;

		} break;
		default: {}
	}

	return false;
}
Exemple #19
0
void Variant::reference(const Variant& p_variant) {

	
	if (this == &p_variant)
		return;
		
	clear();
		
	type=p_variant.type;
		
	switch( p_variant.type ) {
		case NIL: {
		
			// none
		} break;
		
		// atomic types 		
		case BOOL: {
		
			_data._bool=p_variant._data._bool;
		} break;
		case INT: {
		
			_data._int=p_variant._data._int;
		
		} break;
		case REAL: {
		
			_data._real=p_variant._data._real;
		
		} break;
		case STRING: {
		
			memnew_placement( _data._mem, String( *reinterpret_cast<const String*>(p_variant._data._mem) ) );
			
		} break;
		
		// math types
		
		case VECTOR2: {
		
			memnew_placement( _data._mem, Vector2( *reinterpret_cast<const Vector2*>(p_variant._data._mem) ) );
		
		} break;
		case RECT2: {
		
			memnew_placement( _data._mem, Rect2( *reinterpret_cast<const Rect2*>(p_variant._data._mem) ) );
		
		} break;
		case MATRIX32: {

			_data._matrix32 = memnew( Matrix32( *p_variant._data._matrix32 ) );

		} break;
		case VECTOR3: {
		
			memnew_placement( _data._mem, Vector3( *reinterpret_cast<const Vector3*>(p_variant._data._mem) ) );
		
		} break;
		case PLANE: {
		
			memnew_placement( _data._mem, Plane( *reinterpret_cast<const Plane*>(p_variant._data._mem) ) );
		
		} break;
/*		
		case QUAT: {
		
		
		} break;*/
		case _AABB: {
		
			_data._aabb = memnew( AABB( *p_variant._data._aabb ) );
		} break;
		case QUAT: {
		
			memnew_placement( _data._mem, Quat( *reinterpret_cast<const Quat*>(p_variant._data._mem) ) );
		
		} break;
		case MATRIX3: {
		
			_data._matrix3 = memnew( Matrix3( *p_variant._data._matrix3 ) );
		
		} break;
		case TRANSFORM: {
		
			_data._transform = memnew( Transform( *p_variant._data._transform ) );
		
		} break;
		
		// misc types		
		case COLOR: {
		
			memnew_placement( _data._mem, Color( *reinterpret_cast<const Color*>(p_variant._data._mem) ) );
		
		} break;
		case IMAGE: {
		
			_data._image = memnew( Image( *p_variant._data._image ) );
		
		} break;
		case _RID: {
		
			memnew_placement( _data._mem, RID( *reinterpret_cast<const RID*>(p_variant._data._mem) ) );
		} break;
		case OBJECT: {
		
			memnew_placement( _data._mem, ObjData( p_variant._get_obj() ) );
		} break;
		case NODE_PATH: {
		
			memnew_placement( _data._mem, NodePath( *reinterpret_cast<const NodePath*>(p_variant._data._mem) ) );
		
		} break;
		case INPUT_EVENT: {
			
			_data._input_event= memnew( InputEvent( *p_variant._data._input_event ) );
			
		} break;
		case DICTIONARY: {
			
			memnew_placement( _data._mem, Dictionary( *reinterpret_cast<const Dictionary*>(p_variant._data._mem) ) );
			
		} break;
		case ARRAY: {
			
			memnew_placement( _data._mem, Array ( *reinterpret_cast<const Array*>(p_variant._data._mem) ) );
			
		} break;
		
		// arrays
		case RAW_ARRAY: {
		
			memnew_placement( _data._mem, DVector<uint8_t> ( *reinterpret_cast<const DVector<uint8_t>*>(p_variant._data._mem) ) );
		
		} break;
		case INT_ARRAY: {
		
			memnew_placement( _data._mem, DVector<int> ( *reinterpret_cast<const DVector<int>*>(p_variant._data._mem) ) );
		
		} break;
		case REAL_ARRAY: {
		
			memnew_placement( _data._mem, DVector<real_t> ( *reinterpret_cast<const DVector<real_t>*>(p_variant._data._mem) ) );
		
		} break;
		case STRING_ARRAY: {
		
			memnew_placement( _data._mem, DVector<String> ( *reinterpret_cast<const DVector<String>*>(p_variant._data._mem) ) );
		
		} break;
		case VECTOR2_ARRAY: {

			memnew_placement( _data._mem, DVector<Vector2> ( *reinterpret_cast<const DVector<Vector2>*>(p_variant._data._mem) ) );

		} break;
		case VECTOR3_ARRAY: {
		
			memnew_placement( _data._mem, DVector<Vector3> ( *reinterpret_cast<const DVector<Vector3>*>(p_variant._data._mem) ) );
		
		} break;
		case COLOR_ARRAY: {
		
			memnew_placement( _data._mem, DVector<Color> ( *reinterpret_cast<const DVector<Color>*>(p_variant._data._mem) ) );
		
		} break;
		default: {}
	}		


}
Exemple #20
0
bool Tween::_calc_delta_val(const Variant& p_initial_val, const Variant& p_final_val, Variant& p_delta_val) {

	const Variant& initial_val = p_initial_val;
	const Variant& final_val = p_final_val;
	Variant& delta_val = p_delta_val;

	switch(initial_val.get_type()) {

		case Variant::BOOL:
			//delta_val = p_final_val;
			delta_val = (int) p_final_val - (int) p_initial_val;
			break;

		case Variant::INT:
			delta_val = (int) final_val - (int) initial_val;
			break;

		case Variant::REAL:
			delta_val = (real_t) final_val - (real_t) initial_val;
			break;

		case Variant::VECTOR2:
			delta_val = final_val.operator Vector2() - initial_val.operator Vector2();
			break;

		case Variant::VECTOR3:
			delta_val = final_val.operator Vector3() - initial_val.operator Vector3();
			break;

		case Variant::MATRIX3:
			{
				Matrix3 i = initial_val;
				Matrix3 f = final_val;
				delta_val = Matrix3(f.elements[0][0] - i.elements[0][0],
					f.elements[0][1] - i.elements[0][1],
					f.elements[0][2] - i.elements[0][2],
					f.elements[1][0] - i.elements[1][0],
					f.elements[1][1] - i.elements[1][1],
					f.elements[1][2] - i.elements[1][2],
					f.elements[2][0] - i.elements[2][0],
					f.elements[2][1] - i.elements[2][1],
					f.elements[2][2] - i.elements[2][2]
				);
			}
			break;

		case Variant::MATRIX32:
			{
				Matrix32 i = initial_val;
				Matrix32 f = final_val;
				Matrix32 d = Matrix32();
				d[0][0] = f.elements[0][0] - i.elements[0][0];
				d[0][1] = f.elements[0][1] - i.elements[0][1];
				d[1][0] = f.elements[1][0] - i.elements[1][0];
				d[1][1] = f.elements[1][1] - i.elements[1][1];
				d[2][0] = f.elements[2][0] - i.elements[2][0];
				d[2][1] = f.elements[2][1] - i.elements[2][1];
				delta_val = d;
			}
			break;
		case Variant::QUAT:
			delta_val = final_val.operator Quat() - initial_val.operator Quat();
			break;
		case Variant::_AABB:
			{
				AABB i = initial_val;
				AABB f = final_val;
				delta_val = AABB(f.pos - i.pos, f.size - i.size);
			}
			break;
		case Variant::TRANSFORM:
			{
				Transform i = initial_val;
				Transform f = final_val;
				Transform d;
				d.set(f.basis.elements[0][0] - i.basis.elements[0][0],
					f.basis.elements[0][1] - i.basis.elements[0][1],
					f.basis.elements[0][2] - i.basis.elements[0][2],
					f.basis.elements[1][0] - i.basis.elements[1][0],
					f.basis.elements[1][1] - i.basis.elements[1][1],
					f.basis.elements[1][2] - i.basis.elements[1][2],
					f.basis.elements[2][0] - i.basis.elements[2][0],
					f.basis.elements[2][1] - i.basis.elements[2][1],
					f.basis.elements[2][2] - i.basis.elements[2][2],
					f.origin.x - i.origin.x,
					f.origin.y - i.origin.y,
					f.origin.z - i.origin.z
				);

				delta_val = d;
			}
			break;
		case Variant::COLOR:
			{
				Color i = initial_val;
				Color f = final_val;
				delta_val = Color(f.r - i.r, f.g - i.g, f.b - i.b, f.a - i.a);
			}
			break;

		default:
			ERR_PRINT("Invalid param type, except(int/real/vector2/vector/matrix/matrix32/quat/aabb/transform/color)");
			return false;
	};
	return true;
}
Exemple #21
0
void TileMap::_update_dirty_quadrants() {

	if (!pending_update)
		return;
	if (!is_inside_tree())
		return;
	if (!tile_set.is_valid())
		return;

	VisualServer *vs = VisualServer::get_singleton();
	Physics2DServer *ps = Physics2DServer::get_singleton();
	Vector2 tofs = get_cell_draw_offset();
	Vector2 tcenter = cell_size/2;
	Matrix32 nav_rel;
	if (navigation)
		nav_rel = get_relative_transform_to_parent(navigation);

	Vector2 qofs;

	SceneTree *st=SceneTree::get_singleton();
	Color debug_collision_color;

	bool debug_shapes = st && st->is_debugging_collisions_hint();
	if (debug_shapes) {
		debug_collision_color=st->get_debug_collisions_color();
	}

	while (dirty_quadrant_list.first()) {

		Quadrant &q = *dirty_quadrant_list.first()->self();

		for (List<RID>::Element *E=q.canvas_items.front();E;E=E->next()) {

			vs->free(E->get());
		}

		q.canvas_items.clear();

		ps->body_clear_shapes(q.body);
		int shape_idx=0;

		if (navigation) {
			for(Map<PosKey,Quadrant::NavPoly>::Element *E=q.navpoly_ids.front();E;E=E->next()) {

				navigation->navpoly_remove(E->get().id);
			}
			q.navpoly_ids.clear();
		}

		for(Map<PosKey,Quadrant::Occluder>::Element *E=q.occluder_instances.front();E;E=E->next()) {
			VS::get_singleton()->free(E->get().id);
		}
		q.occluder_instances.clear();
		Ref<CanvasItemMaterial> prev_material;
		RID prev_canvas_item;
		RID prev_debug_canvas_item;

		for(int i=0;i<q.cells.size();i++) {

			Map<PosKey,Cell>::Element *E=tile_map.find( q.cells[i] );
			Cell &c=E->get();
			//moment of truth
			if (!tile_set->has_tile(c.id))
				continue;
			Ref<Texture> tex = tile_set->tile_get_texture(c.id);
			Vector2 tile_ofs = tile_set->tile_get_texture_offset(c.id);

			Vector2 wofs = _map_to_world(E->key().x, E->key().y);
			Vector2 offset = wofs - q.pos + tofs;

			if (!tex.is_valid())
				continue;

			Ref<CanvasItemMaterial> mat = tile_set->tile_get_material(c.id);

			RID canvas_item;
			RID debug_canvas_item;

			if (prev_canvas_item==RID() || prev_material!=mat) {

				canvas_item=vs->canvas_item_create();
				if (mat.is_valid())
					vs->canvas_item_set_material(canvas_item,mat->get_rid());
				vs->canvas_item_set_parent( canvas_item, get_canvas_item() );
				Matrix32 xform;
				xform.set_origin( q.pos );
				vs->canvas_item_set_transform( canvas_item, xform );
				vs->canvas_item_set_light_mask(canvas_item,get_light_mask());
				vs->canvas_item_set_blend_mode(canvas_item,VS::MaterialBlendMode(get_blend_mode()));

				q.canvas_items.push_back(canvas_item);

				if (debug_shapes) {

					debug_canvas_item=vs->canvas_item_create();
					vs->canvas_item_set_parent( debug_canvas_item, canvas_item );
					vs->canvas_item_set_z_as_relative_to_parent(debug_canvas_item,false);
					vs->canvas_item_set_z(debug_canvas_item,VS::CANVAS_ITEM_Z_MAX-1);
					q.canvas_items.push_back(debug_canvas_item);
					prev_debug_canvas_item=debug_canvas_item;
				}

				prev_canvas_item=canvas_item;
				prev_material=mat;

			} else {
				canvas_item=prev_canvas_item;
				if (debug_shapes) {
					debug_canvas_item=prev_debug_canvas_item;
				}
			}



			Rect2 r = tile_set->tile_get_region(c.id);
			Size2 s = tex->get_size();

			if (r==Rect2())
				s = tex->get_size();
			else {
				s = r.size;
				r.pos.x+=fp_adjust;
				r.pos.y+=fp_adjust;
				r.size.x-=fp_adjust*2.0;
				r.size.y-=fp_adjust*2.0;
			}

			Rect2 rect;
			rect.pos=offset.floor();
			rect.size=s;

			if (rect.size.y > rect.size.x) {
				if ((c.flip_h && (c.flip_v || c.transpose)) || (c.flip_v && !c.transpose))
					tile_ofs.y += rect.size.y - rect.size.x;
			} else if (rect.size.y < rect.size.x) {
				if ((c.flip_v && (c.flip_h || c.transpose)) || (c.flip_h && !c.transpose))
					tile_ofs.x += rect.size.x - rect.size.y;
			}

		/*	rect.size.x+=fp_adjust;
			rect.size.y+=fp_adjust;*/

			if (c.transpose)
				SWAP(tile_ofs.x, tile_ofs.y);

			if (c.flip_h) {
				rect.size.x=-rect.size.x;
				tile_ofs.x=-tile_ofs.x;
			}
			if (c.flip_v) {
				rect.size.y=-rect.size.y;
				tile_ofs.y=-tile_ofs.y;
			}

			Vector2 center_ofs;

			if (tile_origin==TILE_ORIGIN_TOP_LEFT) {
				rect.pos+=tile_ofs;
				
			} else if (tile_origin==TILE_ORIGIN_BOTTOM_LEFT) {
								
				rect.pos+=tile_ofs;
				
				if(c.transpose)
				{
					if(c.flip_h)
						rect.pos.x-=cell_size.x;
					else
						rect.pos.x+=cell_size.x;
				} else {
					if(c.flip_v)
						rect.pos.y-=cell_size.y;
					else
						rect.pos.y+=cell_size.y;
				}
				
			} else if (tile_origin==TILE_ORIGIN_CENTER) {
				rect.pos+=tcenter;

				Vector2 center = (s/2) - tile_ofs;
				center_ofs=tcenter-(s/2);

				if (c.flip_h)
					rect.pos.x-=s.x-center.x;
				else
					rect.pos.x-=center.x;

				if (c.flip_v)
					rect.pos.y-=s.y-center.y;
				else
					rect.pos.y-=center.y;
			}


			if (r==Rect2()) {
				tex->draw_rect(canvas_item,rect,false,Color(1,1,1),c.transpose);
			} else {
				tex->draw_rect_region(canvas_item,rect,r,Color(1,1,1),c.transpose);
			}

			Vector< Ref<Shape2D> > shapes = tile_set->tile_get_shapes(c.id);


			for(int i=0;i<shapes.size();i++) {

				Ref<Shape2D> shape = shapes[i];
				if (shape.is_valid()) {

					Vector2 shape_ofs = tile_set->tile_get_shape_offset(c.id);
					Matrix32 xform;
					xform.set_origin(offset.floor());

					_fix_cell_transform(xform,c,shape_ofs+center_ofs,s);

					if (debug_canvas_item) {
						vs->canvas_item_add_set_transform(debug_canvas_item,xform);
						shape->draw(debug_canvas_item,debug_collision_color);

					}
					ps->body_add_shape(q.body,shape->get_rid(),xform);
					ps->body_set_shape_metadata(q.body,shape_idx++,Vector2(E->key().x,E->key().y));

				}
			}

			if (debug_canvas_item) {
				vs->canvas_item_add_set_transform(debug_canvas_item,Matrix32());
			}

			if (navigation) {
				Ref<NavigationPolygon> navpoly = tile_set->tile_get_navigation_polygon(c.id);
				if (navpoly.is_valid()) {
					Vector2 npoly_ofs = tile_set->tile_get_navigation_polygon_offset(c.id);
					Matrix32 xform;
					xform.set_origin(offset.floor()+q.pos);
					_fix_cell_transform(xform,c,npoly_ofs+center_ofs,s);


					int pid = navigation->navpoly_create(navpoly,nav_rel * xform);

					Quadrant::NavPoly np;
					np.id=pid;
					np.xform=xform;
					q.navpoly_ids[E->key()]=np;
				}
			}


			Ref<OccluderPolygon2D> occluder=tile_set->tile_get_light_occluder(c.id);
			if (occluder.is_valid()) {

				Vector2 occluder_ofs = tile_set->tile_get_occluder_offset(c.id);
				Matrix32 xform;
				xform.set_origin(offset.floor()+q.pos);
				_fix_cell_transform(xform,c,occluder_ofs+center_ofs,s);

				RID orid = VS::get_singleton()->canvas_light_occluder_create();
				VS::get_singleton()->canvas_light_occluder_set_transform(orid,get_global_transform() * xform);
				VS::get_singleton()->canvas_light_occluder_set_polygon(orid,occluder->get_rid());
				VS::get_singleton()->canvas_light_occluder_attach_to_canvas(orid,get_canvas());
				VS::get_singleton()->canvas_light_occluder_set_light_mask(orid,occluder_light_mask);
				Quadrant::Occluder oc;
				oc.xform=xform;
				oc.id=orid;
				q.occluder_instances[E->key()]=oc;
			}
		}

		dirty_quadrant_list.remove( dirty_quadrant_list.first() );
		quadrant_order_dirty=true;
	}



	pending_update=false;

	if (quadrant_order_dirty) {

		for (Map<PosKey,Quadrant>::Element *E=quadrant_map.front();E;E=E->next()) {

			Quadrant &q=E->get();
			for (List<RID>::Element *E=q.canvas_items.front();E;E=E->next()) {

				VS::get_singleton()->canvas_item_raise(E->get());
			}
		}

		quadrant_order_dirty=false;
	}

	for(int i=0;i<get_child_count();i++) {

		CanvasItem *c=get_child(i)->cast_to<CanvasItem>();

		if (c)
			VS::get_singleton()->canvas_item_raise(c->get_canvas_item());
	}

	_recompute_rect_cache();

}
Exemple #22
0
/* ************************************************************************* */
const Matrix32& Unit3::basis(OptionalJacobian<6, 2> H) const {
#ifdef GTSAM_USE_TBB
    // NOTE(hayk): At some point it seemed like this reproducably resulted in deadlock. However, I
    // can't see the reason why and I can no longer reproduce it. It may have been a red herring, or
    // there is still a latent bug to watch out for.
    tbb::mutex::scoped_lock lock(B_mutex_);
#endif

    // Return cached basis if available and the Jacobian isn't needed.
    if (B_ && !H) {
        return *B_;
    }

    // Return cached basis and derivatives if available.
    if (B_ && H && H_B_) {
        *H = *H_B_;
        return *B_;
    }

    // Get the unit vector and derivative wrt this.
    // NOTE(hayk): We can't call point3(), because it would recursively call basis().
    const Point3 n(p_);

    // Get the axis of rotation with the minimum projected length of the point
    Point3 axis;
    double mx = fabs(n.x()), my = fabs(n.y()), mz = fabs(n.z());
    if ((mx <= my) && (mx <= mz)) {
        axis = Point3(1.0, 0.0, 0.0);
    } else if ((my <= mx) && (my <= mz)) {
        axis = Point3(0.0, 1.0, 0.0);
    } else if ((mz <= mx) && (mz <= my)) {
        axis = Point3(0.0, 0.0, 1.0);
    } else {
        assert(false);
    }

    // Choose the direction of the first basis vector b1 in the tangent plane by crossing n with
    // the chosen axis.
    Matrix33 H_B1_n;
    Point3 B1 = n.cross(axis, H ? &H_B1_n : 0);

    // Normalize result to get a unit vector: b1 = B1 / |B1|.
    Matrix33 H_b1_B1;
    Point3 b1 = B1.normalize(H ? &H_b1_B1 : 0);

    // Get the second basis vector b2, which is orthogonal to n and b1, by crossing them.
    // No need to normalize this, p and b1 are orthogonal unit vectors.
    Matrix33 H_b2_n, H_b2_b1;
    Point3 b2 = n.cross(b1, H ? &H_b2_n : 0, H ? &H_b2_b1 : 0);

    // Create the basis by stacking b1 and b2.
    B_.reset(Matrix32());
    (*B_) << b1.x(), b2.x(), b1.y(), b2.y(), b1.z(), b2.z();

    if (H) {
        // Chain rule tomfoolery to compute the derivative.
        const Matrix32& H_n_p = *B_;
        Matrix32 H_b1_p = H_b1_B1 * H_B1_n * H_n_p;
        Matrix32 H_b2_p = H_b2_n * H_n_p + H_b2_b1 * H_b1_p;

        // Cache the derivative and fill the result.
        H_B_.reset(Matrix62());
        (*H_B_) << H_b1_p, H_b2_p;
        *H = *H_B_;
    }

    return *B_;
}
Exemple #23
0
Matrix32 Camera2D::get_camera_transform()  {

	if (!get_tree())
		return Matrix32();

	Size2 screen_size = get_viewport_rect().size;
	screen_size=get_viewport_rect().size;


	Point2 new_camera_pos = get_global_transform().get_origin();
	Point2 ret_camera_pos;

	if (!first) {


		if (anchor_mode==ANCHOR_MODE_DRAG_CENTER) {

			if (h_drag_enabled && !get_tree()->is_editor_hint()) {
				camera_pos.x = MIN( camera_pos.x, (new_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_RIGHT]));
				camera_pos.x = MAX( camera_pos.x, (new_camera_pos.x - screen_size.x * 0.5 * drag_margin[MARGIN_LEFT]));
			} else {

				if (h_ofs<0) {
					camera_pos.x = new_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_RIGHT] * h_ofs;
				} else {
					camera_pos.x = new_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_LEFT] * h_ofs;
				}
			}

			if (v_drag_enabled && !get_tree()->is_editor_hint()) {

				camera_pos.y = MIN( camera_pos.y, (new_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_BOTTOM]));
				camera_pos.y = MAX( camera_pos.y, (new_camera_pos.y - screen_size.y * 0.5 * drag_margin[MARGIN_TOP]));

			} else {

				if (v_ofs<0) {
					camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_TOP] * v_ofs;
				} else {
					camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_BOTTOM] * v_ofs;
				}
			}

		} else if (anchor_mode==ANCHOR_MODE_FIXED_TOP_LEFT){

			camera_pos=new_camera_pos;
		}



		if (smoothing_enabled && !get_tree()->is_editor_hint()) {

			float c = smoothing*get_fixed_process_delta_time();
			smoothed_camera_pos = ((camera_pos-smoothed_camera_pos)*c)+smoothed_camera_pos;
			ret_camera_pos=smoothed_camera_pos;
			//			camera_pos=camera_pos*(1.0-smoothing)+new_camera_pos*smoothing;
		} else {

			ret_camera_pos=smoothed_camera_pos=camera_pos;

		}



	} else {
		ret_camera_pos=smoothed_camera_pos=camera_pos=new_camera_pos;
		first=false;
	}


	Point2 screen_offset = (anchor_mode==ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom) : Point2());

	float angle = get_global_transform().get_rotation();
	if(rotating){
		screen_offset = screen_offset.rotated(angle);
	}

	Rect2 screen_rect(-screen_offset+ret_camera_pos,screen_size*zoom);
	if (screen_rect.pos.x + screen_rect.size.x > limit[MARGIN_RIGHT])
		screen_rect.pos.x = limit[MARGIN_RIGHT] - screen_rect.size.x;

	if (screen_rect.pos.y + screen_rect.size.y > limit[MARGIN_BOTTOM])
		screen_rect.pos.y = limit[MARGIN_BOTTOM] - screen_rect.size.y;


	if (screen_rect.pos.x < limit[MARGIN_LEFT])
		screen_rect.pos.x=limit[MARGIN_LEFT];

	if (screen_rect.pos.y < limit[MARGIN_TOP])
		screen_rect.pos.y =limit[MARGIN_TOP];

	if (offset!=Vector2()) {

		screen_rect.pos+=offset;
		if (screen_rect.pos.x + screen_rect.size.x > limit[MARGIN_RIGHT])
			screen_rect.pos.x = limit[MARGIN_RIGHT] - screen_rect.size.x;

		if (screen_rect.pos.y + screen_rect.size.y > limit[MARGIN_BOTTOM])
			screen_rect.pos.y = limit[MARGIN_BOTTOM] - screen_rect.size.y;


		if (screen_rect.pos.x < limit[MARGIN_LEFT])
			screen_rect.pos.x=limit[MARGIN_LEFT];

		if (screen_rect.pos.y < limit[MARGIN_TOP])
			screen_rect.pos.y =limit[MARGIN_TOP];

	}

	camera_screen_center=screen_rect.pos+screen_rect.size*0.5;

	Matrix32 xform;
	if(rotating){
		xform.set_rotation(angle);
	}
	xform.scale_basis(zoom);
	xform.set_origin(screen_rect.pos/*.floor()*/);


/*
	if (0) {

		xform = get_global_transform() * xform;
	} else {

		xform.elements[2]+=get_global_transform().get_origin();
	}
*/


	return (xform).affine_inverse();
}
Exemple #24
0
void Camera2D::_notification(int p_what) {

	switch(p_what) {

		case NOTIFICATION_FIXED_PROCESS: {

			_update_scroll();

		} break;
		case NOTIFICATION_TRANSFORM_CHANGED: {


			if (!is_fixed_processing())
				_update_scroll();

		} break;
		case NOTIFICATION_ENTER_TREE: {


			viewport = NULL;
			Node *n=this;
			while(n){

				viewport = n->cast_to<Viewport>();
				if (viewport)
					break;
				n=n->get_parent();
			}

			canvas = get_canvas();

			RID vp = viewport->get_viewport();

			group_name = "__cameras_"+itos(vp.get_id());
			canvas_group_name ="__cameras_c"+itos(canvas.get_id());
			add_to_group(group_name);
			add_to_group(canvas_group_name);

			_update_scroll();
			first=true;


		} break;
		case NOTIFICATION_EXIT_TREE: {

			if (is_current()) {
				if (viewport) {
					viewport->set_canvas_transform( Matrix32() );
				}
			}
			remove_from_group(group_name);
			remove_from_group(canvas_group_name);
			viewport=NULL;

		} break;
		case NOTIFICATION_DRAW: {

			if (!is_inside_tree() || !get_tree()->is_editor_hint())
				break;

			Color area_axis_color(0.5, 0.42, 0.87, 0.63);
			float area_axis_width = 1;
			if(current)
				area_axis_width = 3;

			Matrix32 inv_camera_transform = get_camera_transform().affine_inverse();
			Size2 screen_size = get_viewport_rect().size;

			Vector2 screen_endpoints[4]= {
				inv_camera_transform.xform(Vector2(0, 0)),
				inv_camera_transform.xform(Vector2(screen_size.width,0)),
				inv_camera_transform.xform(Vector2(screen_size.width, screen_size.height)),
				inv_camera_transform.xform(Vector2(0, screen_size.height))
			};

			Matrix32 inv_transform = get_transform().affine_inverse(); // undo global space
			draw_set_transform(inv_transform.get_origin(), inv_transform.get_rotation(), inv_transform.get_scale());

			for(int i=0;i<4;i++) {
				draw_line(screen_endpoints[i], screen_endpoints[(i+1)%4], area_axis_color, area_axis_width);
			}

		} break;
	}
}
Exemple #25
0
Variant::operator String() const {

	switch( type ) {
	
		case NIL: return ""; 
		case BOOL: return _data._bool ? "True" : "False";
		case INT: return String::num(_data._int);
		case REAL: return String::num(_data._real);
		case STRING: return *reinterpret_cast<const String*>(_data._mem);
		case VECTOR2: return operator Vector2();
		case RECT2: return operator Rect2();
		case MATRIX32: return operator Matrix32();
		case VECTOR3: return operator Vector3();
		case PLANE: return operator Plane();
		//case QUAT: 
		case _AABB: return operator AABB();
		case QUAT: return operator Quat();
		case MATRIX3: return operator Matrix3();
		case TRANSFORM: return operator Transform();
		case NODE_PATH: return operator NodePath();
		case INPUT_EVENT: return operator InputEvent();
		case COLOR: return String::num( operator Color().r)+","+String::num( operator Color().g)+","+String::num( operator Color().b)+","+String::num( operator Color().a) ;
		case DICTIONARY: {
			
			const Dictionary &d =*reinterpret_cast<const Dictionary*>(_data._mem);
			//const String *K=NULL;
			String str;
			List<Variant> keys;
			d.get_key_list(&keys);

			Vector<_VariantStrPair> pairs;

			for(List<Variant>::Element *E=keys.front();E;E=E->next()) {

				_VariantStrPair sp;
				sp.key=String(E->get());
				sp.value=d[E->get()];
				pairs.push_back(sp);
			}

			pairs.sort();

			for(int i=0;i<pairs.size();i++) {
				if (i>0)
					str+=", ";
				str+="("+pairs[i].key+":"+pairs[i].value+")";
			}
			
			return str;
		} break;
		case VECTOR3_ARRAY: { 
		
			DVector<Vector3> vec = operator DVector<Vector3>();
			String str;
			for(int i=0;i<vec.size();i++) {
			
				if (i>0)
					str+=", ";
				str=str+Variant( vec[i] );
			}
			return str;
		} break;
		case STRING_ARRAY: {

			DVector<String> vec = operator DVector<String>();
			String str;
			for(int i=0;i<vec.size();i++) {

				if (i>0)
					str+=", ";
				str=str+vec[i];
			}
			return str;
		} break;
		case INT_ARRAY: {

			DVector<int> vec = operator DVector<int>();
			String str;
			for(int i=0;i<vec.size();i++) {

				if (i>0)
					str+=", ";
				str=str+itos(vec[i]);
			}
			return str;
		} break;
		case REAL_ARRAY: {

			DVector<real_t> vec = operator DVector<real_t>();
			String str;
			for(int i=0;i<vec.size();i++) {

				if (i>0)
					str+=", ";
				str=str+rtos(vec[i]);
			}
			return str;
		} break;
		case ARRAY: {

			Array arr = operator Array();
			String str;
			for (int i=0; i<arr.size(); i++) {
				if (i)
					str+=", ";
				str += String(arr[i]);
			};
			return str;

		} break;
		case OBJECT: {

			if (_get_obj().obj)
				return "["+_get_obj().obj->get_type()+":"+itos(_get_obj().obj->get_instance_ID())+"]";
			else
				return "[Object:null]";

		} break;
		default: {
			return "["+get_type_name(type)+"]";
		}		
	}
	
	return "";
}
void Polygon2DEditor::_uv_draw() {

	Ref<Texture> base_tex = node->get_texture();
	if (base_tex.is_null())
		return;

	Matrix32 mtx;
	mtx.elements[2]=-uv_draw_ofs;
	mtx.scale_basis(Vector2(uv_draw_zoom,uv_draw_zoom));

	VS::get_singleton()->canvas_item_set_clip(uv_edit_draw->get_canvas_item(),true);
	VS::get_singleton()->canvas_item_add_set_transform(uv_edit_draw->get_canvas_item(),mtx);
	uv_edit_draw->draw_texture(base_tex,Point2());
	VS::get_singleton()->canvas_item_add_set_transform(uv_edit_draw->get_canvas_item(),Matrix32());

	if (snap_show_grid) {
		Size2 s = uv_edit_draw->get_size();
		int last_cell;

		if (snap_step.x!=0) {
			for(int i=0;i<s.width;i++) {
				int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i,0)).x-snap_offset.x)/snap_step.x));
				if (i==0)
					last_cell=cell;
				if (last_cell!=cell)
					uv_edit_draw->draw_line(Point2(i,0),Point2(i,s.height),Color(0.3,0.7,1,0.3));
				last_cell=cell;
			}
		}

		if (snap_step.y!=0) {
			for(int i=0;i<s.height;i++) {
				int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0,i)).y-snap_offset.y)/snap_step.y));
				if (i==0)
					last_cell=cell;
				if (last_cell!=cell)
					uv_edit_draw->draw_line(Point2(0,i),Point2(s.width,i),Color(0.3,0.7,1,0.3));
				last_cell=cell;
			}
		}
	}

	DVector<Vector2> uvs = node->get_uv();
	Ref<Texture> handle = get_icon("EditorHandle","EditorIcons");

	Rect2 rect(Point2(),mtx.basis_xform(base_tex->get_size()));
	rect.expand_to(mtx.basis_xform(uv_edit_draw->get_size()));

	for(int i=0;i<uvs.size();i++) {

		int next = (i+1)%uvs.size();
		uv_edit_draw->draw_line(mtx.xform(uvs[i]),mtx.xform(uvs[next]),Color(0.9,0.5,0.5),2);
		uv_edit_draw->draw_texture(handle,mtx.xform(uvs[i])-handle->get_size()*0.5);
		rect.expand_to(mtx.basis_xform(uvs[i]));
	}

	rect=rect.grow(200);
	updating_uv_scroll=true;
	uv_hscroll->set_min(rect.pos.x);
	uv_hscroll->set_max(rect.pos.x+rect.size.x);
	uv_hscroll->set_page(uv_edit_draw->get_size().x);
	uv_hscroll->set_val(uv_draw_ofs.x);
	uv_hscroll->set_step(0.001);

	uv_vscroll->set_min(rect.pos.y);
	uv_vscroll->set_max(rect.pos.y+rect.size.y);
	uv_vscroll->set_page(uv_edit_draw->get_size().y);
	uv_vscroll->set_val(uv_draw_ofs.y);
	uv_vscroll->set_step(0.001);
	updating_uv_scroll=false;

}
Exemple #27
0
void CollisionObject2D::_bind_methods() {

	ObjectTypeDB::bind_method(_MD("add_shape","shape:Shape2D","transform"),&CollisionObject2D::add_shape,DEFVAL(Matrix32()));
	ObjectTypeDB::bind_method(_MD("get_shape_count"),&CollisionObject2D::get_shape_count);
	ObjectTypeDB::bind_method(_MD("set_shape","shape_idx","shape:Shape"),&CollisionObject2D::set_shape);
	ObjectTypeDB::bind_method(_MD("set_shape_transform","shape_idx","transform"),&CollisionObject2D::set_shape_transform);
	ObjectTypeDB::bind_method(_MD("set_shape_as_trigger","shape_idx","enable"),&CollisionObject2D::set_shape_as_trigger);
	ObjectTypeDB::bind_method(_MD("get_shape:Shape2D","shape_idx"),&CollisionObject2D::get_shape);
	ObjectTypeDB::bind_method(_MD("get_shape_transform","shape_idx"),&CollisionObject2D::get_shape_transform);
	ObjectTypeDB::bind_method(_MD("is_shape_set_as_trigger","shape_idx"),&CollisionObject2D::is_shape_set_as_trigger);
	ObjectTypeDB::bind_method(_MD("remove_shape","shape_idx"),&CollisionObject2D::remove_shape);
	ObjectTypeDB::bind_method(_MD("clear_shapes"),&CollisionObject2D::clear_shapes);
	ObjectTypeDB::bind_method(_MD("get_rid"),&CollisionObject2D::get_rid);

}
void SpriteRegionEditor::_region_draw()
{
	Ref<Texture> base_tex = node->get_texture();
	if (base_tex.is_null())
		return;

	Matrix32 mtx;
	mtx.elements[2]=-draw_ofs;
	mtx.scale_basis(Vector2(draw_zoom,draw_zoom));

	VS::get_singleton()->canvas_item_set_clip(edit_draw->get_canvas_item(),true);
	VS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(),mtx);
	edit_draw->draw_texture(base_tex,Point2());
	VS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(),Matrix32());

	if (snap_show_grid) {
		Size2 s = edit_draw->get_size();
		int last_cell;

		if (snap_step.x!=0) {
			for(int i=0;i<s.width;i++) {
				int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i,0)).x-snap_offset.x)/snap_step.x));
				if (i==0)
					last_cell=cell;
				if (last_cell!=cell)
					edit_draw->draw_line(Point2(i,0),Point2(i,s.height),Color(0.3,0.7,1,0.3));
				last_cell=cell;
			}
		}

		if (snap_step.y!=0) {
			for(int i=0;i<s.height;i++) {
				int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0,i)).y-snap_offset.y)/snap_step.y));
				if (i==0)
					last_cell=cell;
				if (last_cell!=cell)
					edit_draw->draw_line(Point2(0,i),Point2(s.width,i),Color(0.3,0.7,1,0.3));
				last_cell=cell;
			}
		}
	}

	Ref<Texture> select_handle = get_icon("EditorHandle","EditorIcons");

	Rect2 scroll_rect(Point2(),mtx.basis_xform(base_tex->get_size()));
	scroll_rect.expand_to(mtx.basis_xform(edit_draw->get_size()));

	Vector2 endpoints[4]={
		mtx.basis_xform(rect.pos),
		mtx.basis_xform(rect.pos+Vector2(rect.size.x,0)),
		mtx.basis_xform(rect.pos+rect.size),
		mtx.basis_xform(rect.pos+Vector2(0,rect.size.y))
	};

	for(int i=0;i<4;i++) {

		int prev = (i+3)%4;
		int next = (i+1)%4;

		Vector2 ofs = ((endpoints[i] - endpoints[prev]).normalized() + ((endpoints[i] - endpoints[next]).normalized())).normalized();
		ofs*=1.4144*(select_handle->get_size().width/2);

		edit_draw->draw_line(endpoints[i]-draw_ofs, endpoints[next]-draw_ofs, Color(0.9,0.5,0.5), 2);

		edit_draw->draw_texture(select_handle,(endpoints[i]+ofs-(select_handle->get_size()/2)).floor()-draw_ofs);

		ofs = (endpoints[next]-endpoints[i])/2;
		ofs += (endpoints[next]-endpoints[i]).tangent().normalized()*(select_handle->get_size().width/2);

		edit_draw->draw_texture(select_handle,(endpoints[i]+ofs-(select_handle->get_size()/2)).floor()-draw_ofs);

		scroll_rect.expand_to(endpoints[i]);
	}

	scroll_rect=scroll_rect.grow(200);
	updating_scroll=true;
	hscroll->set_min(scroll_rect.pos.x);
	hscroll->set_max(scroll_rect.pos.x+scroll_rect.size.x);
	hscroll->set_page(edit_draw->get_size().x);
	hscroll->set_val(draw_ofs.x);
	hscroll->set_step(0.001);

	vscroll->set_min(scroll_rect.pos.y);
	vscroll->set_max(scroll_rect.pos.y+scroll_rect.size.y);
	vscroll->set_page(edit_draw->get_size().y);
	vscroll->set_val(draw_ofs.y);
	vscroll->set_step(0.001);
	updating_scroll=false;
}
	virtual void init() {

		VisualServer *vs = VisualServer::get_singleton();
		Physics2DServer *ps = Physics2DServer::get_singleton();



		space=ps->space_create();
		ps->space_set_active(space,true);
		ps->set_active(true);
		ps->area_set_param(space,Physics2DServer::AREA_PARAM_GRAVITY_VECTOR,Vector2(0,1));
		ps->area_set_param(space,Physics2DServer::AREA_PARAM_GRAVITY,98);

		{

			RID vp = vs->viewport_create();
			canvas = vs->canvas_create();
			vs->viewport_attach_canvas(vp,canvas);
			vs->viewport_attach_to_screen(vp,Rect2(Vector2(),OS::get_singleton()->get_window_size()));
			Matrix32 smaller;
			//smaller.scale(Vector2(0.6,0.6));
			//smaller.elements[2]=Vector2(100,0);

			//view_xform = smaller;
			vs->viewport_set_canvas_transform(vp,canvas,view_xform);

		}

		ray = vs->canvas_item_create();
		vs->canvas_item_set_parent(ray,canvas);
		//ray_query = ps->query_create(this,"_ray_query_callback",Variant());
		//ps->query_intersection(ray_query,space);

		_create_body_shape_data();

		for(int i=0;i<32;i++) {

			Physics2DServer::ShapeType types[4]={
				Physics2DServer::SHAPE_CIRCLE,
				Physics2DServer::SHAPE_CAPSULE,
				Physics2DServer::SHAPE_RECTANGLE,
				Physics2DServer::SHAPE_CONVEX_POLYGON,

			};

			Physics2DServer::ShapeType type = types[i%4];
//			type=Physics2DServer::SHAPE_SEGMENT;
			_add_body(type,Matrix32(i*0.8,Point2(152+i*40,100-40*i)));
			//if (i==0)
			//	ps->body_set_mode(b,Physics2DServer::BODY_MODE_STATIC);
		}

		//RID b= _add_body(Physics2DServer::SHAPE_CIRCLE,Matrix32(0,Point2(101,140)));
		//ps->body_set_mode(b,Physics2DServer::BODY_MODE_STATIC);

		Point2 prev;

		Vector<Point2> parr;
		for(int i=0;i<30;i++) {

			Point2 p(i*60,Math::randf() * 70+340);
			if (i>0) {
				parr.push_back(prev);
				parr.push_back(p);
			}
			prev=p;
		}

		_add_concave(parr);
		//_add_plane(Vector2(0.0,-1).normalized(),-300);
		//_add_plane(Vector2(1,0).normalized(),50);
		//_add_plane(Vector2(-1,0).normalized(),-600);

	}
Exemple #30
0
Matrix32 CollisionObject2D::get_shape_transform(int p_shape_idx) const {

	ERR_FAIL_INDEX_V(p_shape_idx,shapes.size(),Matrix32());
	return shapes[p_shape_idx].xform;

}