• Create Account

Banner advertising on our site currently available from just \$5!

Member Since 01 Jul 2003
Online Last Active Today, 03:45 PM

### Align Vertices to Average Plane ?

Today, 03:45 PM

Hi guys,

I have a collection of vertices that are roughly on the same plane, I want them to actually be on the same plane.

I thought I would loop through the vertices and compute an average normal and an average position and use them to define the plane. The plane normal looks correct but when I project the vertices they are a good way off from their original positions.

Here is the (pseudo) code:

```
float3 avg_pos = (0, 0, 0);
float3 avg_nrm = (0, 0, 0);

for (i = 0; i < num_vertices; i++){

// avg position
avg_pos += vertex_array[i];

// create normal (no array bounds check/wrap-around in this example)
// I know I am skipping triangles this way but it's a good enough sample
v1 = vertex_array[i];
v2 = vertex_array[i + 1];
v3 = vertex_array[i + 2];

v12 = v2 - v1;
v13 = v3 - v1;

nrm = cross(v12, v13);
avg_nrm += normalize(nrm);

}

// divide avg_pos by count
avg_pos /= num_vertices

// normalize avg_normal (it becomes the plane normal)
plane_nrm = normalize(avg_nrm);

// now project

for (i = 0; i < num_vertices; i++){

d = dot(vertex_array[i], plane_nrm);
pos = vertex_array[i] - (d * plane_n);

// add in average position ?
pos += avg_pos;

}
```

The vertices do end up on the average plane which is nice but, like i wrote, the positions are noticably off. I'd like as little displacement as possible. Maybe its correct and it's just not a good technique to do what I want ?

Thanks a lot.

### Animation Keyframes, Sampling, and Interpolation Questions

17 April 2015 - 06:19 PM

Hi guys,

I'm having what seems to be a problem to me but may be normal behavior. Please take a look at this diagram:

It's a two keyframe clip (of a leg.) At time t = 0 and at t = 1 the foot is exactly where it should be, meaning the same position since it should not move at all. However, during the interpolation it goes down a little then back up (see yellow arrow), it's largest deviation is at exactly t = 0.5. It's not out of control but it is noticable.

Here is what the numbers look like for the y-axis (up) value of the foot bone:

t = 0 : 0.5 (totally accurate)

t = 0.25 : 0.462 (sinking in to floor)

t = 0.5: 0.449 (at it's lowest)

t = 0.75 : 0.464 (coming back up)

t = 1.0 : 0.5 (totally accurate)

I tried both N-Lerp and Slerp, and made sure all my quaternions are normaized. I'm almost positive my math is correct.

The only way I can minimize the error is with more keyframes. That makes me wonder about something, if i specify those two keyframes in 3ds Max the other keyframes I would use would  be interpolated by Max, so I'd really be using interpolated data for more data. What is Max doing that I'm not ?

Is this normal behavior ? It seems to creep in with IK; for example, if I make the foot go up I don't see any errors (I have to run the numbers to be sure though.)

Any information would be helpful. Thanks.

### Skeletal Animation Root Translation ?

11 April 2015 - 09:10 PM

Hi guys,

I'm having a problem and I can't understand what's happening. My skeleton's root bone/joint is the pelvis. I set up two (looping) keyframes for a simple squat, the feet should stay planted on the ground while the pelvis goes up and down with the knees bending accordingly; the rig works fine in 3ds Max. It kind of works in my engine but there is a problem: when the pelvis is going down, the knees bend nicely but the feet go down a little then back up, and when the pelvis is going back up the feet go down and up again, so the feet go down, up, down, up during one animation cycle.

When I create the pelvis transform, which I do first since it's the root, I interpolate it's translation between the two keyframes. Please note that this is the only bone/joint whose translation is interpolated, all the other bones only need their rotation interpolated (slerp).

If I don't interpolate the pelvis translation the pelvis stays stationary and the feet come up off the ground with the knees bending very nicely, without that weird down/up cycle of the feet.

I did a dump of the pelvis translation values and they are fine, with just one inflection point per cycle.

I'm not sure what I'm doing wrong or how to handle the root node.

Thanks a lot.

### WM_INPUT Keys Not Registering

16 February 2015 - 03:31 PM

Hi guys,

I'm not sure what's happening here. If I hold down *both* the 'W' and 'S' keys the other keys won't register, but if I hold down *both* the 'A' and 'D' keys the other keys will register. I don't get it:

```#define keyStateDown(flags) ((flags & RI_KEY_BREAK) ? 0 : 1)

// WM_INPUT handler

...
RAWINPUT* raw = (RAWINPUT*)lpb;
USHORT keyCode = raw->data.keyboard.VKey;

if (keyCode == key_w) // 0x57
move_forward = keyStateDown(raw->data.keyboard.Flags);
else if (keyCode == key_s) // 0x53
move_back = keyStateDown(raw->data.keyboard.Flags);
else if (keyCode == key_a) // 0x41
move_left = keyStateDown(raw->data.keyboard.Flags); // breakpoint here
else if (keyCode == key_d) // 0x44
move_right = keyStateDown(raw->data.keyboard.Flags); // breakpoint here
```

I set breakpoints and they don't get triggered.

Edit: the same thing happens if I use WM_KEYDOWN.

### Staging Vertex Buffer Failing

02 February 2015 - 06:14 PM

Hey guys, I've been trying to change the contents of my vertex buffer like this:

1) Create a 2nd buffer with D3D11_USAGE_STAGING; fill the second buffer using ID3D11DeviceContext::Map, ID3D11DeviceContext::Unmap; use ID3D11DeviceContext::CopyResource to copy from the staging buffer to the default buffer.

When I try to create the staging buffer it fails with E_INVALIDARG:

```D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));

bd.Usage = D3D11_USAGE_STAGING;
bd.ByteWidth = sizeof(SimpleVertex) * 12; // 12 verts for this example
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; // I tried different flags

ID3D11Buffer* staging_buffer = nullptr;

HRESULT hResult = d3d_device->CreateBuffer(&bd, nullptr, &staging_buffer);
// I also tried using init data for the second param
```

The other method would be to do this:

2) Use ID3D11DeviceContext::UpdateSubresource to copy data from memory.

Should I just forget the first method and try to get the second method working ?

Thanks.

PARTNERS