
Lesson 4 Source Files
Resource Files
The resources zip is just the graphics for the project. All the graphics go in the “resources” sub folder of your java project space. The plan is to make the resource zip work for as many lessons as possible. This means that the graphics that are in there are pretty much the final versions. More files will be added as we progress but none will be removed. They may be altered but they won’t be removed.
One of the first things I did for this lesson was get rid of all the player specific variables from the main Wolk5K class. The player is now an object just like everything else. Bunnies now collide with each other and change direction as well. When a collision occurs the object type number is passed to the collision handling method of the object. This allows the objects to react differently to colliding with different objects. When a bunny collides with another bunny they both change direction. Pressing the space bar will launch a bullet (currently represented by a tree) in the direction you were facing. If a bullet collides with a bunny the bunny disappears. Bunnies will also change direction if they collide with the player.
One of the things the original Wolfenstein game didn’t have was objects that moved vertically. Everything just slid across the floor. In our game the bunnies actually hop. When I first added the hopping ability I just subtracted the height of the bunny from the starting height value that the raycaster calculates. Then I noticed that the bunnies far away seemed to be jumping rediculously high. I had forgotten to multiply the height they were at by the inverse of the distance from the player.
Another new addition to the game is rotating sprites. This seems difficult until you figure out what the raycaster is already calculating.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public int get_texture(double p_angle) { double t = Math.PI*2-this.facing + Math.PI/2.0 + p_angle + this.angle; while(t<0) t+=2.0*Math.PI; while(t>2.0*Math.PI) t-=2.0*Math.PI; t*=180.0/Math.PI; sel_texture = 0; if(t>0+22.5) sel_texture = 1; if(t>45+22.5) sel_texture = 2; if(t>90+22.5) sel_texture = 3; if(t>135+22.5) sel_texture = 4; if(t>180+22.5) sel_texture = 5; if(t>225+22.5) sel_texture = 6; if(t>270+22.5) sel_texture = 7; if(t>315+22.5) sel_texture = 0; return textures[sel_texture]; } |
This code is quite inefficient but it makes it very easy to see what we’re doing. We subtract the direction that the object is facing from 360 degree because we’re looking at the object. We’re not going from the object’s perspective. We thing add 90 degrees so 0 degrees is north instead of east. p_angle is the angle that the player object is facing. We add that so 0 degrees is facing the same direction as the player. And finally we add the actual angle between the player and the object. This is calculated when rendering the objects.
Next we ensure that the angle of the texture is between 0 and 360 degrees. Adding and subtracting 2*PI is the slow way but it works. When programming make it work then make it fast. And finally we pick the texture we need based on the perspective corrected rotation of the object. Texture 0 is facing east and go counter clockwise from there in 45 degree increments.