コード例 #1
0
ファイル: ParkourController.c プロジェクト: AylwynTaras/NGen
///
//Allows the parkour controller to horizontally wallrun
//
//Parameters:
//	obj: A pointer to the object attached to the parkourController state
//	state: The parkourController state updating the object
static void State_ParkourController_HorizontalWallrun(GObject* obj, State* state)
{
    printf("Horizontal\n");

    //Get the members of this state
    struct State_ParkourController_Members* members = (struct State_ParkourController_Members*)state->members;

    //Zero downward velocity
    if(obj->body->velocity->components[1] < 0.0f)
    {
        Vector impulse;
        Vector_INIT_ON_STACK(impulse, 3);
        impulse.components[1] = -obj->body->velocity->components[1];
        RigidBody_ApplyImpulse(obj->body, &impulse, &Vector_ZERO);
    }

    //Accelerate along wall
    //Get the projection of the forward vector onto the wall's plane

    //Start by getting for forward vector of the camera
    Camera* cam = RenderingManager_GetRenderingBuffer()->camera;

    Vector forward;
    Vector_INIT_ON_STACK(forward, 3);

    Matrix_SliceRow(&forward, cam->rotationMatrix, 2, 0, 3);
    //Forward of camera is back of object...
    Vector_Scale(&forward, -1.0f);

    //Project the forward vector onto the wall plane
    Vector perp;
    Vector_INIT_ON_STACK(perp, 3);

    Vector_GetProjection(&perp, &forward, members->wallNormal);
    Vector_Decrement(&forward, &perp);

    //Set the Y component to 0 to get horizontal vector along wall!
    forward.components[1] = 0.0f;

    Vector_Normalize(&forward);

    //MAke sure the velocity in this direction is not too much
    float magVelAlongWall = Vector_DotProduct(&forward, obj->body->velocity);

    if(magVelAlongWall < members->maxVelocity)
    {
        RigidBody_ApplyImpulse(obj->body, &forward, &Vector_ZERO);
    }

    //Apply a cohesive force to the wall to make sure you do not fall off
    float mag = Vector_DotProduct(obj->body->velocity, members->wallNormal);

    Vector cohesive;
    Vector_INIT_ON_STACK(cohesive, 3);

    if(mag > FLT_EPSILON)
    {
        Vector_GetScalarProduct(&cohesive, members->wallNormal, -mag);

    }
    else if(mag < FLT_EPSILON)
    {
        Vector_GetScalarProduct(&cohesive, members->wallNormal, mag);
    }
    RigidBody_ApplyImpulse(obj->body, &cohesive, members->wallNormal);


}
コード例 #2
0
ファイル: ParkourController.c プロジェクト: AylwynTaras/NGen
void State_ParkourController_Shoot(GObject* obj, State* state)
{
    //Get the members of the state
    struct State_ParkourController_Members* members = (struct State_ParkourController_Members*)state->members;

    //Get a reference to the camera
    Camera* cam = RenderingManager_GetRenderingBuffer()->camera;


    if(InputManager_GetInputBuffer().mouseLock)
    {
        //IF we can shoot again
        if(members->shootTimer >= members->shootCooldown)
        {
            //Get the forward vector of the camera
            Vector direction;
            Vector_INIT_ON_STACK(direction, 3);

            Matrix_SliceRow(&direction, cam->rotationMatrix, 2, 0, 3);
            Vector_Scale(&direction, -1.0f);

            //Create the bullet object
            GObject* bullet = GObject_Allocate();
            GObject_Initialize(bullet);

            //Set the appearance
            bullet->mesh = AssetManager_LookupMesh("Cube");
            //bullet->texture = AssetManager_LookupTexture("White");
            bullet->material = Material_Allocate();
            Material_Initialize(bullet->material, AssetManager_LookupTexture("Jacob"));
            //*Matrix_Index(bullet->material->colorMatrix, 1, 1) = 0.0f;
            //*Matrix_Index(bullet->material->colorMatrix, 2, 2) = 0.0f;

            //Create ridgid body
            bullet->body = RigidBody_Allocate();
            RigidBody_Initialize(bullet->body, bullet->frameOfReference, 1.0f);
            bullet->body->coefficientOfRestitution = 0.2f;
            bullet->body->rollingResistance = 0.2f;
            bullet->body->staticFriction = 0.4f;
            bullet->body->dynamicFriction = 0.2f;

            //Create collider
            bullet->collider = Collider_Allocate();
            ConvexHullCollider_Initialize(bullet->collider);
            ConvexHullCollider_MakeRectangularCollider(bullet->collider->data->convexHullData, 2.0f, 2.0f, 2.0f);

            //Position bullet
            Vector transform;
            Vector_INIT_ON_STACK(transform, 3);

            Vector_GetScalarProduct(&transform, &direction, 2.8243f);
            Vector_Increment(&transform, obj->frameOfReference->position);
            GObject_Translate(bullet, &transform);

            Vector_Copy(&transform, &Vector_ZERO);
            transform.components[2] = 1.0f;
            GObject_Rotate(bullet, &transform, 3.14159f);

            //Scale bullet
            Vector_Copy(&transform, &Vector_ZERO);
            transform.components[0] = transform.components[1] = transform.components[2] = 0.5f;
            GObject_Scale(bullet, &transform);

            //Apply impulse
            Vector_Scale(&direction, 25.0f);
            RigidBody_ApplyImpulse(bullet->body, &direction, &Vector_ZERO);

            //Add the remove state
            State* state = State_Allocate();
            State_Remove_Initialize(state, 7.0f);
            GObject_AddState(bullet, state);

            //Add the bullet to the world
            ObjectManager_AddObject(bullet);

            //Set shoot timer to 0
            members->shootTimer = 0.0f;
        }
    }
}
コード例 #3
0
// Create an object in front of the character and fire
// Parameters:
//	GO: The object getting passed in, in this case the character
//	State: Needed to grab members
void State_CharacterController_ShootBullet(GObject* GO, State* state)
{
	//Get members
	struct State_CharacterController_Members* members = (struct State_CharacterController_Members*)state->members;

	// Camera local
	Camera* cam = RenderingManager_GetRenderingBuffer()->camera;
	// Gets the time per second
	float dt = TimeManager_GetDeltaSec();
	members->timer += dt;

	if(InputManager_GetInputBuffer().mouseLock)
	{
		// Create a net movement vector
		Vector direction;
		Vector_INIT_ON_STACK(direction, 3);
		if (InputManager_IsMouseButtonPressed(0) && members->timer >= members->coolDown)
		{
			//Get "forward" Vector
			Matrix_SliceRow(&direction, cam->rotationMatrix, 2, 0, 3);
			Vector_Scale(&direction,-1.0f);

			// Create the bullet object
			GObject* bullet = GObject_Allocate();
			GObject_Initialize(bullet);

			//bullet->mesh = AssetManager_LookupMesh("Sphere");
			bullet->mesh = AssetManager_LookupMesh("Arrow");
			bullet->texture = AssetManager_LookupTexture("Arrow");


			bullet->body = RigidBody_Allocate();
			RigidBody_Initialize(bullet->body, bullet->frameOfReference, 0.45f);
			bullet->body->coefficientOfRestitution = 0.2f;

			bullet->collider = Collider_Allocate();
			ConvexHullCollider_Initialize(bullet->collider);
			ConvexHullCollider_MakeRectangularCollider(bullet->collider->data->convexHullData, 0.1f, 2.0f, 0.1f);
			//AABBCollider_Initialize(bullet->collider, 2.0f, 2.0f, 2.0f, &Vector_ZERO);

			//Lay arrow flat
			GObject_Rotate(bullet, &Vector_E1, -3.14159f / 2.0f);

			//Construct a rotation matrix to orient bullet
			Matrix rot;
			Matrix_INIT_ON_STACK(rot, 3, 3);
			//Grab 4,4 minor to get 3x3 rotation matrix of camera
			Matrix_GetMinor(&rot, cam->rotationMatrix, 3, 3);
			//Transpose it to get correct direction
			Matrix_Transpose(&rot);
			//Rotate the bullet
			Matrix_TransformMatrix(&rot, bullet->frameOfReference->rotation);
			Matrix_TransformMatrix(&rot, bullet->body->frame->rotation);


			Vector vector;
			Vector_INIT_ON_STACK(vector,3);
			vector.components[0] = 0.9f;
			vector.components[1] = 1.0f;
			vector.components[2] = 0.9f;
			GObject_Scale(bullet, &vector);

			Vector translation;
			Vector_INIT_ON_STACK(translation, 3);
			Vector_GetScalarProduct(&translation, &direction, 2.82843);
			GObject_Translate(bullet, GO->frameOfReference->position);
			GObject_Translate(bullet, &translation);

			Vector_Scale(&direction, 25.0f);

			//Vector_Increment(bullet->body->velocity,&direction);
			RigidBody_ApplyImpulse(bullet->body,&direction,&Vector_ZERO);

			//Add remove state
			State* state = State_Allocate();
			State_Remove_Initialize(state, 5.0f);
			GObject_AddState(bullet, state);

			ObjectManager_AddObject(bullet);

			members->timer = 0;
		}
	}
}