#### Archived

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

# Directional segment/segment distance [precision errors!]

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

## Recommended Posts

Hm... I discovered 'an error' or hopefully 'the error', which is that I used the completely wrong plane for determining distance. There may be other errors... I are stupid. Well, keep reading for entertainment purposes or whatever. I'm trying to calculate the distance from one segment to another along a direction, and I have, but there seem to be precision errors that make it useless for collision detection. First, a pretty picture: The red segment is what's colliding with the world (it's moving). The blue segment is where the red segment would intersect the plane (ground) along the direction of movement, indicated by the green ray. So what we see is the red segment is on or just above the ground, and either it's moving forward while gravity is on, or it's just in fly mode moving downwards and forwards. The distance from the red segment to the blue segment is 0 if the red segment is on the ground, or just above zero if the red segment is just above the ground. Side view pic, if you don't get it yet: The pink line in the lower-right is the minimum distance. Anyway, even when I use doubles (I use 32-bit floats regularly) I get precision errors that causes the minimum distance to be off. Typically this results in colliding with the ground once, thereby setting the red segment to rest on the ground as it should (or 0.001 above it), but the collision the next frame never happens and you go through the ground. I hate the source-code windows here because they're so small; you have to scroll all over the place. So I uploaded the function code and you can view it here. An example of the bug I'm getting is this output:  The distance from the segment (37.654087,4.950787,167.296753)-(37.654087,-0.049214,167.296753) to the segment (32.000000,0.000000,132.000000)-(41.968750,0.000000,194.000000), when moving in the direction (0.106688,-0.731354,0.673602) is 0.120997.  Note that the red segment is below ground (ground is at Y-position 0, yes Y is vertical, not Z) and the direction is downwards, yet the distance returned is positive. I do not know why. EDIT: Oh yeah, and in the function I linked to, Segment1 is the "red" segment, Segment2 is the "blue" one. ~CGameProgrammer( ); [edited by - CGameProgrammer on September 23, 2003 8:58:36 PM] [edited by - CGameProgrammer on September 23, 2003 12:03:22 AM]

##### Share on other sites
what about changing the velocity when a collision occurs?

##### Share on other sites
I already do that but the first step is finding the distance and adjusting the entity's position to be flush with the object it's colliding with. It has to be flush or very close, as otherwise things will hover above the ground or crash into buildings from 50' away.

The velocity changes after a collision, so if you walk at an angle into a wall, you slide alongside the wall instead of stopping completely.

~CGameProgrammer( );

[edited by - CGameProgrammer on September 24, 2003 5:32:52 PM]

1. 1
2. 2
Rutin
20
3. 3
khawk
18
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633763
• Total Posts
3013728
×