Project 6: Advanced Computer Graphics: C-Script Editing with SED
Reference for this project:
Chapter 7 (Link to page of draft
book chapter. )
- Save the pieces of the project so you can later demonstrate
each one
- On university computers, the SED editor may freeze
temporarily. To
avoid this problem, disable automatic backup with
MenuBar->Options->Preferences and uncheck the backup item, click
OK. If SED crashes, open SED a second time and check that the
preference was
set.
You may also need to do this each time that you log in.
- Make a directory ~/video/project6 in your home directory
- Comlex levels have complex script files and would be difficult to
navigate. To make it easier to learn the basics of C-script and
SED, we will start with an extremely simple level. Do not use
your previous projects with complicated levels.
- Using WED, create a simple level consisting of a single
textured large plate floor as shown below, and save it as
game6.wmd. It is best if the plate is
centered at the origin.
Figure
1. An extremely simple level.
Basic Output: Error messages
- Error messages can be helpful in debugging code and interrupting
code execution.
- Open the game script "game6.wdl"
- Add a pop-up error message using the error()
function.as described in "Adding an Error Message" section of
the book.
- Have the error message print out "Test 1."
- Place the error ahead of the “main game
loop” .
- Save the script and rebuild and run the game.
- The message box should appear as below, except with the message
"Test 1."
- Q1 Does the game stop
running until OK is pressed?.
Figure
2. Error message.
Adding Actions
- Actions are pretty much what the name implies, they are actions
that an entity performs. And entities are models that have
associated actions. For example, if you
have
a player in a level and it has no associated action, it will not move
or do anything in the game.
- To experiment with actions, add an action to rotate a model to
your "game6.wdl" script, as described in the "Adding Actions to
Entites" section of the book.
- Call
the action "tw_rotate"
- Use
the statement "my.pan = my.pan +
angleIncrement;" to create the rotation
- After adding the code for the
action, save the script.
- Open the level in WED, and add a
player with PlBiped01 behavior, and add a camera3rd01 camera.
- Add a second model such as a fish
(seen below), and add to it the new behavior "tw_rotate" by
rightclicking the model and adding the behavior. (The behavior
appears in the menu of available behaviors after it is added to the
script file.)
Figure
3. Player and fish added.
- Rebuild and run the game.
- The fish should spin! Try
the other axes,“.pan” with
“.tilt,” or “.roll”
- Q2 How long does it
take the fish to turn 360 degrees?
Adding Keyboard Controls and If Statements
- Next, add keyboard control over the action, as described in the
"If Statements and Movement of Entites" section of the book.
- Use the built-in "key_f" macro/keyboard-function to test is the
"f" key is pressed on the keyboard.
- When the "f" key is pressed, make the fish float upward using the
statement "my.z = my.z +
zIncrement;"
- When the key is not pressed, the fish should float downward, but
stop above the floor.
- Q3 If you do not
prevent it, will the fish pass through the floor, or will it collide
and stop?
Adding Functions and Triggers
- A trigger is an action that happens when a player-entity comes
close to a trigger-entity.
- Next, add another fish to the level.
- Add a function tw_float_away_func() that uses the statement "my.z = my.z +
zIncrement;" to make the fish float away forever. Use a
while(1) loop, an dont forget to add a wait(1) statement inside the
loop so it
doesnt crash.
- Create the action tw_float_away including the statement "my.event
= tw_float_away_func;"
- Save the new script.
- In WED add the action tw_float_away to the new fish.
- Rebuild and run the game.
- The fish should float away when the player touches the new fish!
- Q4 If you comment out
the while(1) statement in tw_float_away_func(), what happens when the
player keeps bumping into the fish?
- The new two-fish game should look something like:
Figure
3. Player and second fish added.
Creating and Animating New Entities
- Up to this point models and player entities have been added to
the game from within WED. It is also possible to create entities
from within a script. This may be useful in making characters
appear and disappear from a game, or for making other objects
appear
or disappear.
- The following method creates an entity and animates it with the
walk cycle.
- This would be one way to test your model animation cycles in the
game engine.
- Use an adaptation of the following code to make your model from
the previous project appear in the game:
var tw_walk_counter;
action tw_walk
{
while(1) //start loop
{
tw_walk_counter = tw_walk_counter + 1;
//add to walk counter
//ent_frame("walk",tw_walk_counter);
//old style syntax, pre 6.31
ent_animate(me,"walk",tw_walk_counter,ANM_CYCLE+ANM_ADD); //new
syntax
if(tw_walk_counter >= 100)
{
tw_walk_counter = 0;
// reset counter at 100 percent
}
wait(1);
}
}
var tw_mod_vector[3]=30,40,50;
// where entity is located
string mod_name = <animatedmodel1.mdl>;
//file name of fish entity
function tw_create_mod1() //called by pressing "p"
{
ent_create (mod_name, tw_mod_vector, tw_walk);
//create the fish entity
}
- Add a call to the function right before the main game loop (i.e.
add the statement "tw_create_mod1();" right before the main loop.)
- Add a function prototype after the include statements and before
main()
- Rebuild and run the game.
- You should see the model walking on the floor!
Figure 4. Another model added
from within script at corner.
- Q5 What vector
setting for tw_mod_vector places the model at the corner of the
floor plate, as illustrated above?
- Q6 What are the
coordinates of the corner of the floorplate in WED?
Gravity and Collision Detection
- A simple way to simulate gravity is do decrement the z coordinate.
- In the previous code for action tw_walk,
- change the initial vector to "tw_mod_vector[3]=0,100,100;"
- and add the following two lines immediately after the
ent_animate(...) line:
me.z=me.z-1;
me.x=me.x+0.5;
- Rebuild and run the game.
- The added character should start above the ground and disappear
into the floor.
- This is because there is no collision detection in the way the
movement was implemented using the statement "me.z=me.z-1"
- Next, add a collision-based move and name the new action
tw_walk_collision and add a new function to create a second model
entity and name the new entity-creation-function tw_create_mod2() ...
following along the lines of the template of tw_create_mod1().
- See the "Simple Gravity and Collision Detection" section of the
book
- Add a call to the function right before the main game loop
- Add a function prototype after the include statements and before
main()
- Add a new "var tw_mod_vector2[3]=0,50,100;" so this new
character walks along side the first one
- To implement collision detection use statements along the lines
of:
- var tw_errresult=0;
- var tw_walk_incr[3]=0.25,0,0;
- var tw_walk_gravity[3]=0,0,-1;
- move_mode =IGNORE_PASSABLE+GLIDE
- tw_errresult=ent_move(tw_walk_incr,
tw_walk_gravity); //collision detect move
- After editing the script, save it.
- Rebuild and rerun the game.
- Demonstrate that the model no longer goes through the floor!
- Hopefully, you will see one entity heading through the floor and
one colliding as below:
Figure 5. Two entities in front
of player with one going through floor.
- Q7 What happens if
the player walks off the edge?
- Q8 How far into the
floor does the collision-based-move entity go?
- Q9 After "bottoming
out" does the collision-based-move entity glide?
- Change your script to use c_move() instead of ent_move() for
polygon presise movement.
- Q10 Does the
collision-based-move entity now hit the floor and glide on its feet?
Report and demonstration:
- Prepare a 12-page report exactly in the following format;
- Cover sheet with names
- 1 Page summary of project
- 1 Page Printout from the running game showing the error message
as in Figure 2.
- 1 Page Printout from WED showing model and 2 fish,
as
in Figure 3 above.
- 1 Page Printout from the running game showing the entity in the
corner as in Figure 4.
- 1 Page Printout from the running game showing the 2 entities as
in Figure 5.
- 5 (or more) Page Printout from SED showing game6.wdl.
- 1 Page Short answers to the following questions:
- Q1 above
- Q2 above
- Q3 above
- Q4 above
- Q5 above
- Q6 above
- Q7 above
- Q8 above
- Q9 above
- Q10 above
- <>Demonstration: Show your
- error message
- "key-f" floating fish,
- trigger-fish,
- move-through-floor entity,
- ent_move() entity,
- c_move()-entity.
Copyright © 2005- 2007 by Thomas Paul
Weldon
ACKNEX,
WED, MED, and 3D GameStudio are trademarks of Conitec Corporation.
Microsoft, DOS, MS-Dos, Windows, DirectX,
Halo, and
Direct3D are trademarks of Microsoft, Inc.
PAC-MAN trademarks or registered trademarks of Namco Limited
Nintendo, Mario, are trademarks of Nintendo
Atari, Pong, are trademarks of Atari
Intel, 8086, are trademarks of Intel Corporation
id, DOOM, and Quake are trademarks or registered trademarks of Id
Software, Inc.
Autodesk, 3D Studio, and 3ds Max are trademarks or registered
trademarks of Autodesk, Inc.
Alias and Maya are trademarks or registered trademarks of Alias Systems
Corp.