
Aproximately an avatar can be represented, for collision, like a regular box. Some other objects can also be represented by boxes. Like the avatar and our wall are in the same level we can define a function (a method: Collision) to detect the collision thinking only in the collision of two rectangles. This is valid for the left part of our wall.
But the collision occurs at some distance of the pieces, like we can see in the figure.
Really the piece is not important. Important is what we call: "shadow for collison", a misterious concept... You need to think a little to understand it.
We have created a method to detect the colision of the avatar against the "shadow for collision" of a piece. All the code of this method, you can analise in the downloadable material. Here we will teach only how to use it.
The method is:
public bool Collision( Double x, Double z,Double Width, Double Height, Double elevation)
Where:
We declare the methods (one method for each "shadow") inside the "main loop". For the demo:
...
void OnTimerEvent(object sender, System.EventArgs args){
...
if(Collision( -13,-5,5,1,TheY)){
//return to the previous point
mtrans= new MatrixTransform3D(transold);
ModelVisual3D mv3D = VP.Children[0] as ModelVisual3D;
Model3DGroup m3Dg = mv3D.Content as Model3DGroup;
m3Dg.Children[4].Transform= mtrans ;
TheX=TheXold;
TheZ=TheZold;
trans = transold;
}
...
Really the avatar doesn't stops. He goes back to his previous position after to try to go for a "forbiden point". This position is captured each time the user press the keys, like we can see for "k":
...
if (e.Key == Key.K ){
transold=trans;
TheXold=TheX;
TheZold=TheZ;
TheZ=TheZ+1;
...
These variables were defined like global.
IMPORTANT: To be responsible we need to change the interval of the timer to 1 milisecond.
If the the face of the object to be collided by the avatar is smaller than 4 units, the "dirt trick" of the "shadow for collision" is not good enought.But we have another trick!
At our sample we have the part at right of the door where we can't use it. At these cases we need to define each one of the "forbiden points" and write some aditional code, like:
...
//To avoid the avatar to go to forbiden points
if ((TheZ==-3 && TheX==-4)||
(TheZ==-3 && TheX==-3)||
(TheZ==-3 && TheX==-2)||
(TheZ==-3 && TheX==-1)||
(TheZ==-8 && TheX==-4)||
(TheZ==-8 && TheX==-3)||
(TheZ==-8 && TheX==-2)||
(TheZ==-8 && TheX==-1)
){
//return to the previous point
mtrans= new MatrixTransform3D(transold);
ModelVisual3D mv3D = VP.Children[0] as ModelVisual3D;
Model3DGroup m3Dg = mv3D.Content as Model3DGroup;
m3Dg.Children[4].Transform= mtrans ;
TheX=TheXold;
TheZ=TheZold;
trans = transold;
}
...
}//OnKeyDownHandler
We can present the points X and Z of the avatar at the "vista", to facilitate our work. We do it at the demo, like you can see.
To look the source codes and/or run the exercice, create a folder having the name of the .ZIP file and download it - right-clicking the link - and unzip inside the folder :
Try to recreate the exercice changing the code .
Liberate the firewall and play this web3d application:
![]() |
We are doing our tests at a: Notebook DELL Latitude D600 - Intel Pentium M755 2.0 GHZ - Memory: 512 MB - VideoCard ATI Radeon 9000 32MB DDR 4xAGP - Resolution 1400x1050 - Communication: ADSL 256 kbps (Download: 45 kBps, Upload: 14 kBps)
PREVIOUS LESSON NEXT LESSON T.CONTENTS HOMEPAGE
