Browse Source

fixes/cleans up

-input now correctly works when using viewport scaling
-added function to get areas/bodies in given point
-added function to get space state directly from world
Juan Linietsky 10 years ago
parent
commit
dac2017dee

+ 0 - 4
demos/2d/area_input/input.gd

@@ -1,10 +1,6 @@
 
 extends Area2D
 
-# member variables here, example:
-# var a=2
-# var b="textvar"
-
 #virtual from CollisionObject2D (also available as signal)
 func _input_event(viewport, event, shape_idx):
 	#convert event to local coordinates

BIN
demos/2d/area_input/input.scn


+ 5 - 1
scene/main/viewport.cpp

@@ -104,8 +104,10 @@ void Viewport::_update_stretch_transform() {
 		stretch_transform.scale(scale);
 		stretch_transform.elements[2]=size_override_margin*scale;
 
+
 	} else {
 
+
 		stretch_transform=Matrix32();
 	}
 
@@ -1076,8 +1078,9 @@ Matrix32 Viewport::_get_input_pre_xform() const {
 
 		ERR_FAIL_COND_V(to_screen_rect.size.x==0,pre_xf);
 		ERR_FAIL_COND_V(to_screen_rect.size.y==0,pre_xf);
-		pre_xf.scale(rect.size/to_screen_rect.size);
+
 		pre_xf.elements[2]=-to_screen_rect.pos;
+		pre_xf.scale(rect.size/to_screen_rect.size);
 	} else {
 
 		pre_xf.elements[2]=-rect.pos;
@@ -1141,6 +1144,7 @@ void Viewport::_make_input_local(InputEvent& ev) {
 		} break;
 	}
 
+
 }
 
 

+ 6 - 0
scene/resources/world.cpp

@@ -307,6 +307,11 @@ Ref<Environment> World::get_environment() const {
 }
 
 
+PhysicsDirectSpaceState *World::get_direct_space_state() {
+
+	return PhysicsServer::get_singleton()->space_get_direct_state(space);
+}
+
 void World::_bind_methods() {
 
 	ObjectTypeDB::bind_method(_MD("get_space"),&World::get_space);
@@ -314,6 +319,7 @@ void World::_bind_methods() {
 	ObjectTypeDB::bind_method(_MD("get_sound_space"),&World::get_sound_space);
 	ObjectTypeDB::bind_method(_MD("set_environment","env:Environment"),&World::set_environment);
 	ObjectTypeDB::bind_method(_MD("get_environment:Environment"),&World::get_environment);
+	ObjectTypeDB::bind_method(_MD("get_direct_space_state:PhysicsDirectSpaceState"),&World::get_direct_space_state);
 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"environment",PROPERTY_HINT_RESOURCE_TYPE,"Environment"),_SCS("set_environment"),_SCS("get_environment"));
 
 }

+ 2 - 0
scene/resources/world.h

@@ -75,6 +75,8 @@ public:
 	void set_environment(const Ref<Environment>& p_environment);
 	Ref<Environment> get_environment() const;
 
+	PhysicsDirectSpaceState *get_direct_space_state();
+
 	World();
 	~World();
 

+ 9 - 0
scene/resources/world_2d.cpp

@@ -352,8 +352,17 @@ void World2D::_bind_methods() {
 	ObjectTypeDB::bind_method(_MD("get_canvas"),&World2D::get_canvas);
 	ObjectTypeDB::bind_method(_MD("get_space"),&World2D::get_space);
 	ObjectTypeDB::bind_method(_MD("get_sound_space"),&World2D::get_sound_space);
+
+	ObjectTypeDB::bind_method(_MD("get_direct_space_state:Physics2DDirectSpaceState"),&World2D::get_direct_space_state);
+
+}
+
+Physics2DDirectSpaceState *World2D::get_direct_space_state() {
+
+	return Physics2DServer::get_singleton()->space_get_direct_state(space);
 }
 
+
 World2D::World2D() {
 
 	canvas = VisualServer::get_singleton()->canvas_create();

+ 3 - 1
scene/resources/world_2d.h

@@ -30,7 +30,7 @@
 #define WORLD_2D_H
 
 #include "resource.h"
-
+#include "servers/physics_2d_server.h"
 
 class SpatialIndexer2D;
 class VisibilityNotifier2D;
@@ -68,6 +68,8 @@ public:
 	RID get_space();
 	RID get_sound_space();
 
+	Physics2DDirectSpaceState *get_direct_space_state();
+
 	World2D();
 	~World2D();
 };

+ 31 - 0
servers/physics_2d_server.cpp

@@ -289,6 +289,36 @@ Array Physics2DDirectSpaceState::_cast_motion(const Ref<Physics2DShapeQueryParam
 	return ret;
 
 }
+
+Array Physics2DDirectSpaceState::_intersect_point(const Vector2& p_point,int p_max_results,const Vector<RID>& p_exclude,uint32_t p_layers,uint32_t p_object_type_mask) {
+
+	Set<RID> exclude;
+	for(int i=0;i<p_exclude.size();i++)
+		exclude.insert(p_exclude[i]);
+
+	Vector<ShapeResult> ret;
+	ret.resize(p_max_results);
+
+	int rc = intersect_point(p_point,ret.ptr(),ret.size(),exclude,p_layers,p_object_type_mask);
+	if (rc==0)
+		return Array();
+
+	Array r;
+	r.resize(rc);
+	for(int i=0;i<rc;i++) {
+
+		Dictionary d;
+		d["rid"]=ret[i].rid;
+		d["collider_id"]=ret[i].collider_id;
+		d["collider"]=ret[i].collider;
+		d["shape"]=ret[i].shape;
+		d["metadata"]=ret[i].metadata;
+		r[i]=d;
+	}
+	return r;
+
+}
+
 Array Physics2DDirectSpaceState::_collide_shape(const Ref<Physics2DShapeQueryParameters> &psq, int p_max_results){
 
 	Vector<Vector2> ret;
@@ -336,6 +366,7 @@ Physics2DDirectSpaceState::Physics2DDirectSpaceState() {
 void Physics2DDirectSpaceState::_bind_methods() {
 
 
+	ObjectTypeDB::bind_method(_MD("intersect_point","point","max_results","exclude","layer_mask","type_mask"),&Physics2DDirectSpaceState::_intersect_point,DEFVAL(32),DEFVAL(Array()),DEFVAL(0x7FFFFFFF),DEFVAL(TYPE_MASK_COLLISION));
 	ObjectTypeDB::bind_method(_MD("intersect_ray:Dictionary","from","to","exclude","layer_mask","type_mask"),&Physics2DDirectSpaceState::_intersect_ray,DEFVAL(Array()),DEFVAL(0x7FFFFFFF),DEFVAL(TYPE_MASK_COLLISION));
 	ObjectTypeDB::bind_method(_MD("intersect_shape","shape:Physics2DShapeQueryParameters","max_results"),&Physics2DDirectSpaceState::_intersect_shape,DEFVAL(32));
 	ObjectTypeDB::bind_method(_MD("cast_motion","shape:Physics2DShapeQueryParameters"),&Physics2DDirectSpaceState::_cast_motion);

+ 1 - 0
servers/physics_2d_server.h

@@ -137,6 +137,7 @@ class Physics2DDirectSpaceState : public Object {
 
 	Dictionary _intersect_ray(const Vector2& p_from, const Vector2& p_to,const Vector<RID>& p_exclude=Vector<RID>(),uint32_t p_layers=0,uint32_t p_object_type_mask=TYPE_MASK_COLLISION);
 
+	Array _intersect_point(const Vector2& p_point,int p_max_results=32,const Vector<RID>& p_exclude=Vector<RID>(),uint32_t p_layers=0,uint32_t p_object_type_mask=TYPE_MASK_COLLISION);
 	Array _intersect_shape(const Ref<Physics2DShapeQueryParameters> &p_shape_query,int p_max_results=32);
 	Array _cast_motion(const Ref<Physics2DShapeQueryParameters> &p_shape_query);
 	Array _collide_shape(const Ref<Physics2DShapeQueryParameters> &p_shape_query,int p_max_results=32);