Jump to content

Collision Detection on Editor Camera


photo

Recommended Posts

Hello I have found it really easy to control the editor camera in a way that allows full analog control of the movement so smallest possible movement ranges smoothly from min velocity to max velocity.

 

I can use the editor code in game but I would need to add some sort of collision detection Or I need to figure out how to get analog movement to work with the usual Player Set up...

 

My modified version of the control function looks like this.

void control() {
// 		log.message("editor control is enabled %i\n",enabled);
	if(enabled == 0) return;

	float ifps = engine.app.getIFps();

	// camera basis
	mat4 rimodelview = rotation(imodelview);
	vec3 x = rimodelview * vec3(1.0f,0.0f,0.0f);
	vec3 y = rimodelview * vec3(0.0f,1.0f,0.0f);
	vec3 z = rimodelview * vec3(0.0f,0.0f,1.0f);

	// mouse controls
	if(engine.controls.isMouseEnabled() && getGuiActivity() == 0) {
		int button = gui.getMouseButton();
		float dx = engine.controls.getMouseDX();
		float dy = engine.controls.getMouseDY();
// 			log.message("dx %f, dy %f\n",dx,dy);
		if(button & APP_BUTTON_LEFT) {
			phi += dx;
			theta += dy;
		}
		if(button & APP_BUTTON_MIDDLE) {
			position -= (x * dx - y * dy) * toolsGetCameraMaxVelocity() / 20.0f;
		}
		if(button & APP_BUTTON_RIGHT) {
			distance -= toolsGetCameraMaxVelocity() * dy / 20.0f;
			distance = max(distance,toolsGetCameraZNear() * 2.0f);
		}
	}

	// impulse
	vec3 impulse = vec3_zero;

// 		// keyboard controls
// 		log.message("zip\n");
	if(engine.controls.getState(CONTROLS_STATE_FORWARD) || forward) impulse -= z;
	if(engine.controls.getState(CONTROLS_STATE_BACKWARD) || back) impulse += z;
	if(engine.controls.getState(CONTROLS_STATE_MOVE_LEFT) || left) impulse -= x;
	if(engine.controls.getState(CONTROLS_STATE_MOVE_RIGHT) || right) impulse += x;
	if(engine.controls.getState(CONTROLS_STATE_CROUCH) || down) impulse -= y;
	if(engine.controls.getState(CONTROLS_STATE_JUMP)|| up) impulse += y;
	up=0;down=0;left=0;right=0;back=0;forward=0;
	impulse = normalize(impulse);

	if(speed < -0.1f) {
		if(engine.controls.getState(CONTROLS_STATE_RUN)) impulse *= toolsGetCameraMaxVelocity();
		else impulse *= toolsGetCameraMinVelocity();
	} else if (speed < 0.2) {
		impulse *= toolsGetCameraMinVelocity();
	} else {
		speed = (speed-0.2)*1.25;
		float min = toolsGetCameraMinVelocity();
		float max = toolsGetCameraMaxVelocity();
		impulse *= min + ((max-min)*speed);
	}

	speed = -1.0f;
	// target velocity
	float target_velocity = length(impulse);

	// save old velocity
	float old_velocity = length(velocity);

	// integrate velocity
	velocity += impulse * toolsGetCameraAcceleration() * ifps;

	// damping velocity
	float current_velocity = length(velocity);
	if(target_velocity < EPSILON || current_velocity > target_velocity) {
		velocity *= exp(-toolsGetCameraDamping() * ifps);
	}

	// clamp maximum velocity
	current_velocity = length(velocity);
	if(current_velocity > old_velocity && current_velocity > target_velocity) {
		velocity *= target_velocity / current_velocity;
	}

	// sleep velocity
	if(current_velocity < EPSILON) {
		velocity = vec3_zero;
	}

	// integrate position

			// world intersection
	if(collide){
		int ret[3];
		vec3 ret_normal;
// 			log.message("delta %s\n",typeinfo(position - p0));
		vec3 p0 = imodelview.m03m13m23;
		vec3 p1 = position + velocity * (ifps + 0.25f);
		while(engine.world.getIntersection(p0,p1,~0,ret) != NULL) {
			if(current_velocity < EPSILON) {
				velocity = vec3_zero;
				break;
			}
			p0 = ret[0];
			ret_normal = ret[1];
			vec3 vtmp = velocity * -1.0f;
			float projection = dot(vec3(vtmp),vec3(ret_normal));
			vec3 ln = ret_normal * projection;
			vec3 reflect = ln * 2.0f;
			vec3 vel_final = reflect + velocity; 
			float current_velocity = length(velocity);

			p1 = p0 + vel_final;
			velocity = vel_final;


		} 
		position = p1;
	}  else position += velocity * ifps;
	// matrices
	imodelview = translate(position) * inverse(translate(0.0f,0.0f,-distance) * mat4(quat(1.0f,0.0f,0.0f,theta) * quat(0.0f,0.0f,1.0f,phi)));
}

 

In which the collision Detection sort of works but is not robust enough.

Link to comment
  • 2 weeks later...

Ok I got the analog movement working on the a PlayerSpectator object.

But even with this code disabled I am noticing that Collision detection is broken once you get the editor camera up to a certain speed.

Link to comment
×
×
  • Create New...