TGC Codebase Backup



3d movement with inertia in a frictionless environment by Philip

8th Feb 2004 10:46
Summary

This is example code to show how to move a camera in 3d with inertia. For example, movement like the I-Wars space games.



Description

There are plenty of remarks in the code. Feel free to use as you wish.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Remstart
PJY's attempted example of pseudo-Newtonian style movement for Mr. Silent Phoenix
Remend

Rem PJY - standard setup
sync on
sync rate 30
autocam off
backdrop on

Rem PJY - make cube to give the camera some reference point to show the movement
make object cube 1, 50
position object 1, 0, 100, 500

Rem PJY - we'll use the default camera because I'm feeling lazy
position camera 0, 0, 0, 0

Rem PJY - create some vector3 data items to hold the camera movement data
camera_position = 1
camera_new_movement = 2
camera_old_movement = 3
camera_movement = 4

null = make vector3(camera_position)
null = make vector3(camera_new_movement)
null = make vector3(camera_old_movement)
null = make vector3(camera_movement)

Rem PJY - calculate the centre of the camera's screen for use in the pick screen command below
width = screen width() / 2
height = screen height() / 2

Rem PJY - declare a few float variables - you just never know when they'll come in handy
robk# AS float
ianm# AS float
tca# AS float

Rem PJY - main loop
do

	Rem PJY - deal with human input
	if upkey() = 1 then pitch camera up 0, 1
	if downkey() = 1 then pitch camera down 0, 1
	if leftkey() = 1 then turn camera left 0, 1
	if rightkey() = 1 then turn camera right 0, 1
	
	Rem PJY - update camera position vector
	set vector3 to camera position camera_position, 0	
	
	Rem PJY - normalise the camera_movement vector otherwise on the last loop executed the camera_movement will increase exponentially
	Rem PJY - and that will cause the camera to shoot off faster than a Robin Reliant on steroids
	normalize vector3 camera_movement, camera_movement
	Rem PJY - and transfer old camera movement into a temporary storage vector
	set vector3 camera_old_movement, x vector3(camera_movement), y vector3(camera_movement), z vector3(camera_movement)
	
	Rem PJY - now calculate the 3d vector which represents the camera's orientation if the player presses the thrust key
	if returnkey() = 1
		
		pick screen width, height, 2
		robk# = get pick vector x()
		ianm# = get pick vector y()
		tca# = get pick vector z()
		set vector3 camera_new_movement, robk#, ianm#, tca#

	endif
	
	Rem PJY - right now add the old movement (if any) to the new movement (if any) to get the sum total new movement vector
	add vector3 camera_movement, camera_old_movement, camera_new_movement	
	Rem PJY - and now add the new movement vector to the camera's current position to find out the new X, Y, Z coords of the camera
	Rem PJY - which to save space I shall rather confusingly save straight back in the camera_movement vector again (overwriting the previous data in there)
	add vector3 camera_movement, camera_position, camera_movement
		
	Rem PJY - and now re-position the camera at those new coords
	position camera 0, x vector3(camera_movement), y vector3(camera_movement), z vector3(camera_movement)

	Rem PJY - display info for user comprehension
	x = 0
	y = 0
	text x, y, "X: " + str$(x vector3(camera_position)) + " Y: " + str$(y vector3(camera_position)) + " Z: " + str$(z vector3(camera_position))
	y = y + 15
	text x, y, "Cam Move X: " + str$(x vector3(camera_movement)) + " Cam Move Y: " + str$(y vector3(camera_movement)) + " Cam Move Z: " + str$(z vector3(camera_movement))
	y = y + 15
	text x, y, "Cam NewMove X: " + str$(x vector3(camera_new_movement)) + " Cam NewMove Y: " + str$(y vector3(camera_new_movement)) + " Cam NewMove Z: " + str$(z vector3(camera_new_movement))
	y = y + 15
	Rem PJY - give user a clue what the keyboard does
	text x, y, "Arrow keys to control camera, return is the move key"
	y = y + 30
	Rem PJY - put a stupid statement on the screen
	text x, y, "Its sort of real physics, Jim, but not as we know it ... "
	
	Rem PJY - and update screen
	sync
	
loop