Click here to Skip to main content
15,887,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, this is just another funny 3D application i am trying to create using WPF.

It is a 3D animation kind of an application. After an ardous search, i finally figured out a way to create a 3D tennis ball in wpf. I have created a method that takes the x,y,z positions , size, etc as parameters.

In my code, i have called that method twice, and thus have got two tennis balls on my screen.:D..I have given the initial positions for both the balls as (0,0,0) and (3,3,3). Now, I would like to animate them according to the positions I give. Basically, I want the second ball to chase the first ball, but it can never catch the first one.;)

I have written a small chunk of code(as given below) that can give me new positions for both the balls every time the first for loop is executed. I am assuming that I will get 100 new positions according to the first for loop ? and the second for loops will execute two times as there are two tennis balls and their positions are stored in an array ?

Everytime the first loop is executed i want the new position to be passed to both the balls and I want to make the balls move and make it a continuous animation. I don't have any idea of how to do this and I have never done any 3D animations on WPF ever before.

I am a beginner and so Please go easy on me when explaining technical concepts. I would greatly appreciate any illustrations, examples and tutorials.

//Position generator

for(int frame=0;frame<100;i++)
{
for(int j=0;j<2;j++)
{
xposition[j] = xposition[j]+2;
yposition[j] = yposition[j]+2;
zposition[j] = zposition[j]+2;
}
}


Thanks
Posted
Updated 23-Aug-11 5:30am
v3

Actually, for this simple of animation there is no need to pre-compute the next 100 positions.

Create a timer that fires every 30ms (that gives you about 30 frames/sec) and calculate the new position. You'll want to keep variables for current vectors. Update these every time your timer ticks.

Bind your position of these elements to the data in your class. You can find a wealth of databinding information here on CP (and certainly a tonne more on Google).

Cheers.
 
Share this answer
 
Comments
steersteer 23-Aug-11 12:56pm    
thanks man..you are awesome...will post if i get stuck again...
Sergey Alexandrovich Kryukov 23-Aug-11 23:15pm    
Agree, my 5.
--SA
I Tried using dispatchertimer class in wpf. I am afriad that this procedure does not seem to work for my purpose. The output does not work very well, and I could not animate it either. I am thinking of making my project a property based animation by using translatetransform3D or something. I am not precomputing anything at the moment. There are 3 arrays for x,y and z co-ordinates. x[0] will have x position of ball-1 and x[1] will have x position of ball-2 and similarly the y and z arrays. The values in X[0], X[1]...etc. will be changed frequently using a simple method that contains some calculations within a for loop. I want these values(positions) to be used as new positions for the tennis balls. The animation should run accordingly to the positions updated by the function. (can these new positions be used as an offset to translatetransform3d ?) The animation should end when the loop within the method terminates. I have done quite a lot of reading about timer-based animation, frame-based animation and property based animation using C#/WPF, but so far I have not found anything useful. There are a few solutions but all of them are for 2D objects. I have done most of my work in C# and have not used xaml that much. Please resolve my problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900