开发者

Ball Rolling in DirectX with C#

Im trying to create a seesaw with ball on its shape, that based on the shapes angle, the ball rolls.

Here is the screenshot of it.

Ball Rolling in DirectX with C#

So, the shape of the seesaw moves based on the angle generatated by a trackbar value.

Here are the variables declared:

private const float ONE_DEGREE = 0.0174532924f;
        private ID3DMesh tab;        
        private ID3DMesh ball;

The 'tab' variable is the shape.

This method sets the angle of the shape:

public void setShapeAngle(float degree)
{            
    tabTargetAngle = Util.DegreeToRadian(degree);
}

And here is the method that updates it:

public void Update(int elapsedTime)
        {

            if (tab.Pitch != tabTargetAngle)
            {
                if (tabTargetAngle > tab.Pitch)
                {
                    if (tab.Pitch >= (tabTargetAngle - ONE_DEGREE))
                    {
                        tab.Pitch = tabTargetAngle;

                    }
                    else
                    {
                        tab.Pitch += tabuaSpeed * elapsedTime;                        
                    }
                }
                else if (tabTargetAngle < tab.Pitch)
                {
                    if (tab.Pitch <= (tabTargetAngle + ONE_DEGREE))
                    {
                        tab.Pitch = tabTargetAngle;

                    }
                    else
                    {
                        tab.Pitch -= tabuaSpeed * elapsedTime;                        

                    }

                }

            }                                                 
        }

All of the objects, are ID3DMesh objects. Here is the code of the ID3DMesh class.

 public interface ID3DMesh : IDisposable
    {
        Color Ambient { get; set; }
        CollisionTestMethod CollisionDetectionMethod { get; set; }
        Mesh D3DXMesh { get; }
        Color Diffuse { get; set; }
        Color Emissive { get; set; }
        Material[] Materials { get; set; }
        ID3DMesh Parent { get; set; }
        float Pitch { get; set; }
        Vector3 PivotOffset { get; set; }
        float PivotOffsetX { get; set; }
        float PivotOffsetY { get; set; }
        float PivotOffsetZ { get; set; }
        Vector3 Position { get; set; }
        RenderOptions RenderSettings { get; set; }
        float Roll { get; set; }
        Vector3 Scale { get; set; }
        float ScaleX { get; set; }
        float ScaleY { get; set; }
        float ScaleZ { get; set; }
        Color Specular { get; set; }
        float SpecularSharpness { get; set; }
        Texture[] Textures { get; set; }
        Color WireColor { get; set; }
        float X { get; set; }
        float Y { get; set; }
        float Yaw { get; set; }
        float Z { get; set; }

        MeshBoundingBox GetBoundingBox();
        MeshBoundingSphere GetBoundingSphere();
        float GetDepth();
        float GetHeight();
        float GetWidth();
        Matrix GetWorldMatrix();
        bool Intersects(ID3DMesh mesh);
        void Link(ID3DMesh parentMesh, Vector3 linkPosition);
        void Move(float xAmount, float yAmount, float zAmount);
        void Render();
        void RenderPlanarShadow(Plane groundPlane, Light light, bool allowDoubleBlending);
        void SetDepth(float depth);
        void SetDepth(float depth, bool uniformScale);
        void SetHeight(float height);
        void SetHeight(float height, bool uniformScale);
        void S开发者_如何学运维etPlanarShadowOpacity(float shadowOpacity);
        void SetScale(float amount);
        void SetScale(float xAmount, float yAmount, float zAmount);
        void SetSize(float width, float height, float depth);
        void SetWidth(float width);
        void SetWidth(float width, bool uniformScale);
    }

I tried to use the Move(float, float, float) method. But it didnt moved as it should. If you could help me with that.

Thank you.


(Note: Below I'll be ignoring the third dimension, because the ball will always move along the same plane)

If we take the seesaw as a reference frame, I think the movement of the ball will be similar to that of an harmonic oscillator. That is to say, the position of the ball along the seesaw at a given instant of time, s(t), will be given by the following formula:

s(t) = L cos(2π t / T + ϕ)

where L is the length of the seesaw (the amplitude of the harmonic) and T is the time it takes the ball to move from one end of the seesaw to the other and back to the start (the period of the harmonic). ϕ, the initial phase of the harmonic, is there to adjust the formula so s(0) gives you the starting position. If you want it to start at the center, you need to make s(0) = 0, which means you need the cosine to be 0. So you have to make ϕ be π/2 (90 degrees), because cos(π/2) = 0.

With this you can put the ball in place by changing the world transform. If you rotate it to the current angle of the seesaw (let's call it θ(t)), you can just translate the ball by the value of s(t) along the xx axis.

This is equivalent to treating (s(t),θ(t)) as the position of the ball in polar coordinates. You can then get the cartesian coordinates at a given time (x(t),y(t)) with these formulae:

x(t) = s(t) cos(θ)

y(t) = s(t) sin(θ)


(Let's assume the up-vector is (0, 1, 0) and the tab is aligned with the X-axis)

You can imagine the ball would have to "roll" down the tab along the X-axis, and you would have to calculate the Y-position to let it stick to the tab.

You could use the Move() method for the X position, as the ball's speed has an impact on it's X-position in a relative way.

The Y-position though (as long as the ball remains on the tab) could more easily be calculated for each X-position, by setting the Y property.

If I were you I'd start by creating a method that calculates the Y position to make the ball "stick to the tab" for any X position.

If this doesn't point you in the right direction, please elaborate a bit more on "it didnt moved as it should".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜