pXent_setposition

Posted By: 3run

pXent_setposition - 03/22/13 19:34

I tried to make simple drag/drop example with "pXent_setposition", but I've noticed, that while I pick the object up, it's still affected by the gravity, and when I drop it down, it moves down very fast (as if gravity force was increasing while it was picked up). Is that a normal behavior, or one more bug?

Here is the dirty code example:
Code:
#include <acknex.h>
#include <default.c>
#include <ackphysX.h>

ENTITY* movedEnt;

void cubeEvent(){
	// if we have the pointer, function invalid:
	if(movedEnt != NULL){ return; }	
	// we are moving:
	movedEnt = my;
}

void cube(){
	// flags:
	set(my, SHADOW | CAST | LIGHT); 	
	// random color:
	vec_set(my.blue, vector(150 + random(100), random(100), 0));	
	// set random size
	vec_fill(my.scale_x, 0.6 + random(0.4));	
	// activate physics	
	pXent_settype(my, PH_RIGID, PH_BOX);	
	// restitution:
	pXent_setelasticity(my, 30);	
	// set random initial speed	
	pXent_addvelcentral(my, vector(1 - random(2), 1 - random(2), 0));		
	// event type:
	my.emask |= (ENABLE_SHOOT); 	
	// event:
	my.event = cubeEvent;
}

void main(){
	// shadows:
	shadow_stencil = 2;	
	// limit fps:
	fps_max = 60;	
	// open physX engine:
	physX_open();	
	// load empty level:
	level_load("");	
	// place camera:
	vec_set(camera.x, vector(-180, 0, 30));	
	// create ground plate
	ENTITY* ground = ent_create(CUBE_MDL, nullvector, NULL);
	// scale the cube model:
	vec_set(ground.scale_x, vector(20, 20, 1));
	// register it as a static object:
	pXent_settype(ground, PH_STATIC, PH_BOX); 
	// set it's friction:
	pXent_setfriction(ground, 10);	
	// create three cubes:
	ent_create(CUBE_MDL, vector(random(20), random(20), 170), cube);
	ent_create(CUBE_MDL, vector(random(20), random(20), 170), cube);
	ent_create(CUBE_MDL, vector(random(20), random(20), 170), cube);	
	// vector:
	VECTOR temp;	
	// variable:
	var traceTime = 0;	
	// free vector:
	vec_fill(temp.x, 0);	
	// move camera:
	def_move();	
	// loop:
	while(1){
		// define trace position:
		vec_set(temp.x, vector(50, 0, 0));
		vec_rotate(temp.x, camera.pan);
		vec_add(temp.x, camera.x);		
		// draw end position of the trace:
		draw_point3d(temp.x, COLOR_RED, 100, 1);		
		// if we have moving entity:
		if(movedEnt != NULL){			
			// move entity:
			pXent_setposition(movedEnt, temp.x);			
			// if we press mouse left:
			if(mouse_left && traceTime <= 0){				
				// reset pointer:
				movedEnt = NULL;				
				// set timer:
				traceTime = 0.5;				
			}			
		}
		else{			
			// if needed:
			if(traceTime > 0){				
				// count down:
				traceTime -= time_step / 16;				
			}
			else{				
				// define trace mode:
				trace_mode = IGNORE_PASSABLE | IGNORE_PASSENTS | ACTIVATE_SHOOT;				
				// trace:
				c_trace(camera.x, temp.x, trace_mode);				
			}						
		}		
		// wait one frame:
		wait(1);
	}
}

Just come close to the box, so the red square touches it (the end of the trace), to drop the box, press mouse left.
You'll notice, that as soon as you pick the box up, it slowly starts to move downwards (while it's position is changed by pXent_setposition).
Posted By: MasterQ32

Re: pXent_setposition - 03/23/13 00:59

this is just a logical behaviour. you don't change anything except position. So speed and previous acceleration will be stored.

if you want some good manual movement of physics, enable kinematic movement or change the entity type to character, then move it with pXent_setposition or pXent_move.

or as a dirty workaround: if you use pXent_setpositin, also use pXent_setvelocity to reset the speed of the entity.
Posted By: 3run

Re: pXent_setposition - 03/23/13 09:17

MasterQ32@ thanks for the hints.
Posted By: 3run

Re: pXent_setposition - 03/24/13 11:40

So, as a conclusion, I wanted to make a small tip for those, who wants to create drag/drop with physX. Instead of setting kinematic flag or converting entity to PH_CHAR which IS DIRTY (and won't really work properly I think), I would recommend just to reset gravity forces (via pXent_setbodyflag), and then use pXent_setposition to move the entity and pXent_rotate to rotate it with camera. And to make it automatically drop, if carried object hits anything too hard. Maybe later I'll add my small example here.
Posted By: rojart

Re: pXent_setposition - 03/28/13 13:22

Why you coding this, if we have a function pX_pick()?
Posted By: 3run

Re: pXent_setposition - 03/28/13 14:06

rojart@ I don't really like the pX_pick function... Especially the lag of the objects, from the mouse position. Plus, I need to lock the rotation of the physical object (so it rotates with the camera as well). So, pX_pick offers only very basic solution, which doesn't looks really nice for my needs. (I'm still looking for a way, to create drag and drop system, with better results that I have now).
© 2024 lite-C Forums