Archived

This topic is now archived and is closed to further replies.

Gravity Problem

This topic is 5648 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is the problem I have to apply gravity before I test for collision because if I don''t I get a bounce back effect but I dont want to apply gravity if I collide with an object because if I do that then my view get pulled up (I look at the sky) If you could help me that would be great, thanks. Here is the relevent code (Its from a game engine I am working on I placed the functions in the order they are clled from the object. PS CPlayer has a CCamera object contained inside it (I think this is called a container (memory lapse) void CCamera::Animate(scalar_t deltaTime) { CVector cross; CVector axis; // speed is velocity z-component float speed = velocity.z * deltaTime; gravity = -4; // strafe speed is velocity x-component float strafeSpeed = velocity.x * deltaTime; //friction if (Magnitude(velocity) > 0.0) acceleration = -velocity * 5.0f; velocity += acceleration*deltaTime; //get view view.x = lookAt.x - position.x; view.y = lookAt.y - position.y; view.z = lookAt.z - position.z; //rotate view axis = Cross(view, up); axis = Normalize(axis); RotateView(pitch, axis.x, axis.y, axis.z); RotateView(yaw, 0, 1, 0); //move camera position.x += view.x * speed; position.z += view.z * speed; position.y += gravity; lookAt.x += view.x * speed; lookAt.z += view.z * speed; //strafe cross = Cross(up, view); position.x += cross.x * strafeSpeed; position.z += cross.z * strafeSpeed; lookAt.x += cross.x * strafeSpeed; lookAt.z += cross.z * strafeSpeed; } void CPlayer::CheckCollision() { CVector offset; CVector verts[4]; verts[0].x = -100; verts[0].y = 0.0; verts[0].z = -100; verts[1].x = -100; verts[1].y = 0.0f; verts[1].z = 100; verts[2].x = 100; verts[2].y = 0; verts[2].z = 100; verts[3].x = 100; verts[3].y = 0; verts[3].z = -100; float radius = 1.0; CVector quad[4] = { verts[0], verts[1], verts[2], verts[3]}; CVector normal = Normal(quad); float distance = 0.0f; position -= CVector(0, 2, 0); int classification = ClassifySphere(position , normal, quad[0], radius, distance); if(classification == INTERSECTS) { offset = normal * distance; CVector intersection = position - offset; if(InsidePolygon(intersection, quad, 4) || EdgeSphereCollision(position, quad, 4, radius / 2)) { offset = GetCollisionOffset(normal, radius, distance); camera->position = camera->position + offset; camera->lookAt = camera->lookAt + offset; } } } void CCamera:ositionCamera() { gluLookAt(position.x, position.y, position.z, lookAt.x, lookAt.y, lookAt.z, 0.0, 1.0, 0.0); }

Share this post


Link to post
Share on other sites
quote:

position.y += gravity;



Gravity is an acceleration (force), not a velocity.

You should apply the gravity to the velocity and not the position. That should fix the problems since you only then need deal with a single velocity changing the position.

Share this post


Link to post
Share on other sites