PDA

View Full Version : Camera Question -Coding in Blitz3d-


Chad
03-07-07, 11:31 PM
I'm trying to create a rocking feeling while playing so keep that in mind.

With help off of IRC I have came up with the following code. Basically, it is WolRon's FPS example code with my

RotateEntity camera,Sin(MilliSecs()*.05)*.5,0,Sin(MilliSecs()*. 05)*.5However, with this I cannot use my mouse to look up and down, it stutters. And keeps it still. How can I use my Sin code but still have the full capabilities of my mouse look?

Graphics3D 800, 600, 0, 1 ;3D graphics at a resolution of 800x600
SetBuffer BackBuffer() ;do all drawing to the back drawing buffer
Const PLAYER_COL= 1
Const LEVEL_COL = 2
Const ENEMY_COL = 3
Const BULLET_COL= 4
Type bullettype ;set up the bullet type
Field entityhandle ;create a field to contain the handle of the bullets mesh
End Type
Type badguytype ;set up the badguy type
Field entityhandle ;will contain the handle of the badguys mesh
Field state ;will contain the state that the badguy is currently in
End Type
light = CreateLight() ;Create a light to see
RotateEntity light, 30, 30, 0 ;angle the light
player = CreatePivot() ;A simple pivot is all we need to represent the player
camera = CreateCamera(player) ;create the camera and attach it to the player
CameraRange camera, .01, 250 ;set the camera range to something reasonable
;level = LoadMesh("level.b3d") ;load in the level mesh
level = CreateCube():ScaleEntity level,50,50,50:FlipMesh level:MoveEntity level,0,50,0:EntityColor level,20,20,200
levelfloor=CreatePlane():MoveEntity levelfloor,0,.002,0:EntityColor levelfloor,0,100,0 ;plane not needed, added it just for color
;weapon1 = LoadMesh("pistol.b3d", camera) ;load in the weapon mesh and attach it to the camera
weapon1 = CreateCylinder(32, 1, camera):RotateMesh weapon1, 90, 0, 0:ScaleEntity weapon1, .05, .05, .2:EntityColor weapon1, 50, 50, 50
;bulletmesh = LoadMesh("bullet.b3d") ;load in a bullet mesh (this will be a template mesh)
bulletmesh = CreateCone():RotateMesh bulletmesh,90,0,0:ScaleEntity bulletmesh,.1,.1,.1
EntityType bulletmesh, BULLET_COL ;set up collision type for the bullet
EntityRadius bulletmesh, .01 ;set up collision radius for the bullet
;badguymesh = LoadMesh("badguy.b3d") ;load in badguy mesh (this will also be a template mesh)
badguymesh = CreateCylinder():ScaleEntity badguymesh,.3,.95,.125;.6 wide, 1.9 tall, .25 thick
EntityType badguymesh, ENEMY_COL ;set up collision type for the badguy
EntityRadius badguymesh, .3, .95 ;set up collision radius for the badguy (1.9 meters tall, .6 meters wide)
HideEntity bulletmesh ;hide the template meshes since they are not actual objects
HideEntity badguymesh ;this will also exclude them from collisions
For iter = 1 To 10 ;create some badguys from the template badguy mesh
badguy.badguytype = New badguytype ;create a new badguy
badguy\entityhandle = CopyEntity(badguymesh) ;give him a mesh
badguy\state = Rnd(1, 2) ;1=Guard 2=Search 3=Evade 4=Attack 5=Dead
Next
MoveEntity camera, 0, .9, 0 ;move camera up to height of players head (also moves weapon)
MoveEntity weapon1, .1, -.15, .1 ;move weapon to bottom right of camera
MoveEntity player, 20, 1, -20 ;move the player to the starting position
For badguy = Each badguytype ;iterate through all of the badguys
PositionEntity badguy\entityhandle, Rnd(100)-50, .95, Rnd(100)-50 ;move the badguy to a random starting position
Next
EntityType player, PLAYER_COL ;set up collision type for the player
EntityRadius player, .3, .95 ;set up the players collision radius (1.9 meters tall, .6 meters wide)
EntityType level, LEVEL_COL ;set up collision type for the level
Collisions PLAYER_COL, LEVEL_COL, 2, 2 ;player to level
Collisions PLAYER_COL, ENEMY_COL, 1, 2 ;player to badguy
Collisions ENEMY_COL, LEVEL_COL, 2, 2 ;badguy to level
Collisions BULLET_COL, LEVEL_COL, 2, 1 ;bullet to level
Collisions BULLET_COL, ENEMY_COL, 2, 1 ;bullet to badguy

While Not KeyHit(1) ;ESC key
RotateEntity camera,Sin(MilliSecs()*.05)*.5,0,Sin(MilliSecs()*. 05)*.5
wkey = KeyDown(17) ;collect user input
skey = KeyDown(31) ;It's a good practice to collect these inputs only once
akey = KeyDown(30) ;per loop. This will prevent odd behaviors from happening,
dkey = KeyDown(32) ;for instance if the state of a key changes between multiple
mouse1 = MouseHit(1) ;checks of that key while still in the same loop.
If wkey Then MoveEntity player, 0, 0, .1 ;Forward - w key
If skey Then MoveEntity player, 0, 0, -.1 ;Back - s key
If akey Then MoveEntity player, -.1, 0, 0 ;Left - a key
If dkey Then MoveEntity player, .1, 0, 0 ;Right - d key
TurnEntity player, 0, -MouseXSpeed()/5.0, 0 ;rotate player Pivot according to mouse X movement
TurnEntity camera, MouseYSpeed()/5.0, 0, 0 ;rotate camera up/down according to mouse Y movement
If EntityPitch(camera) < -45 ;don't allow camera to look below -45 degrees
RotateEntity camera, -45, EntityYaw(camera), EntityRoll(camera)
EndIf
If EntityPitch(camera) > 45 ;don't allow camera to look above 45 degrees
RotateEntity camera, 45, EntityYaw(camera), EntityRoll(camera)
EndIf
MoveMouse GraphicsWidth()/2, GraphicsHeight()/2 ;reset mouse position to middle of screen
TranslateEntity player, 0, -.1, 0 ;simple gravity
If mouse1 ;check if left mouse button was pressed
bullet.bullettype = New bullettype ;create a bullet
bullet\entityhandle = CopyEntity(bulletmesh) ;create the bullet mesh
PositionEntity bullet\entityhandle, EntityX(weapon1, 1), EntityY(weapon1, 1), EntityZ(weapon1, 1) ;place the bullet at the guns position
RotateEntity bullet\entityhandle, EntityPitch(weapon1, 1), EntityYaw(weapon1, 1), EntityRoll(weapon1, 1);orientate the bullet with the gun
ResetEntity bullet\entityhandle ;otherwise bullet could hit enemy while moving from 0,0,0 to current position
EndIf
For thisbullet.bullettype = Each bullettype ;iterate through all of the bullets
MoveEntity thisbullet\entityhandle, 0, 0, 2 ;move the bullet forward along the bullets Z axis
If Abs(EntityX(thisbullet\entityhandle, 1)) > 10000 ;check if the bullet is way out of bounds
FreeEntity thisbullet\entityhandle ;delete the bullet mesh
Delete thisbullet ;delete the bullet
ElseIf Abs(EntityY(thisbullet\entityhandle, 1)) > 10000 ;check if the bullet is way out of bounds
FreeEntity thisbullet\entityhandle ;delete the bullet mesh
Delete thisbullet ;delete the bullet
ElseIf Abs(EntityZ(thisbullet\entityhandle, 1)) > 10000 ;check if the bullet is way out of bounds
FreeEntity thisbullet\entityhandle ;delete the bullet mesh
Delete thisbullet ;delete the bullet
EndIf
Next
UpdateWorld ;figures out collisions
For thisbullet = Each bullettype ;iterate through all of the bullets
If CountCollisions(thisbullet\entityhandle) > 0 ;check if bullet collided with something
enemyhit = EntityCollided(thisbullet\entityhandle, 3) ;note which enemy bullet collided with (if any)
If enemyhit > 0 Then KillBadGuy(enemyhit) ;enemyhit contains entity handle of enemy that was hit
FreeEntity thisbullet\entityhandle ;delete the bullet mesh
Delete thisbullet ;delete the bullet
EndIf
Next
RenderWorld ;draws the 3d scene
Flip ;displays the scene to the screen
Wend ;loop until the ESC key is pressed
End
Function KillBadGuy(enemyhit)
For thisbadguy.badguytype = Each badguytype ;iterate through all of the badguys
If enemyhit = thisbadguy\entityhandle ;check if the enemy hit = this badguy
If thisbadguy\state <> 5 ;check if badguy is alive
thisbadguy\state = 5 ;dead ;make him dead
EntityType thisbadguy\entityhandle, 0 ;turn off collisions for this badguy
RotateEntity thisbadguy\entityhandle, 90, 0, 0 ;make him horizontal
TranslateEntity thisbadguy\entityhandle, 0, -.9, 0 ;set him on the ground
Exit ;exits the badguy For-Next loop (no need to check rest of badguys)
EndIf
EndIf
Next
End Function

Deamon
03-10-07, 01:07 AM
RotateEntity camera,Sin(MilliSecs()*.05)*.5,0,Sin(MilliSecs()*. 05)*.5However, with this I cannot use my mouse to look up and down, it stutters. And keeps it still. How can I use my Sin code but still have the full capabilities of my mouse look?
LOL

Chad use TurnEntity instead of RotateEntity becose RotateEntity set the view to an absolute angle while you want relative motion.

You basicaly had a conflict between this two lines of code:

RotateEntity camera,Sin(MilliSecs()*.05)*.5,0,Sin(MilliSecs()*. 05)*.5

TurnEntity camera, MouseYSpeed()/5.0, 0, 0
Coding can be a bitch somtimes eh ?

Chad...

...your code is horrobly to read! lol

You should always leave an empty line betwin code segments and also put comments what a code segment is doing so that no one need to read all of the code to understand what it's supposed to do.

Enjoy

Chad
03-11-07, 10:58 PM
Ah your right, it is a messy piece of :|\\

But, yeah, TurnEntity works good. I've got one more piece of eq I want to finish before Wednesday and if I get that done then I'll post a new dev vid of Iron Coffins, first public preview of the new and improved version :yep:

Thanks,
Chad

Deamon
03-12-07, 01:26 PM
But, yeah, TurnEntity works good. I've got one more piece of eq I want to finish before Wednesday and if I get that done then I'll post a new dev vid of Iron Coffins, first public preview of the new and improved version :yep:
Looking forward. What are you using for making the videos ?

Chad
03-12-07, 11:31 PM
I will be using Fraps, www.fraps.com (http://www.fraps.com)

Posted this in your forum too, just making sure you get it, I want to see yours! :ping: And by yours I'm talking about game :|\\

Chad
03-13-07, 01:34 AM
Couple of more things to do on my list before I show the vid, and one being getting my rudder in full working order!

Here is the code for my rudder, could you help me by showing me some example code to increase my rudder's turn by 5 degrees each time with a max of 40 degrees? You might have to rework some of this, but I just can't get it to work right with the way I'm doing it.


;RUDDER INFORMATION!!!!!
If KeyHit(26) Then TurnLeftFlag=1-TurnLeftFlag:TurnRightFlag=0
If KeyHit(27) Then TurnRightFlag=1-TurnRightFlag:TurnLeftFlag=0
If KeyHit(40) Then TurnLeftFlag=0:TurnRightFlag=0
If TurnLeftFlag<>0 Then
TurnEntity conn,0,.03,0
EndIf
If TurnRightFlag<>0 Then
TurnEntity conn,0,-.03,0
EndIf



If I get this out of the way then there's about 5-10 small things left before Video.

Thanks,
Chad

Deamon
03-13-07, 06:56 AM
Couple of more things to do on my list before I show the vid, and one being getting my rudder in full working order!

Here is the code for my rudder, could you help me by showing me some example code to increase my rudder's turn by 5 degrees each time with a max of 40 degrees? Well, i could but i don't want to steal your lessons. But we could do this together if you like. If you find it to hard to make it than start by express it in pseudo code first.

So show me your pseudo code.

Chad don't use bride font for text, since i'm usign the bride style. I can't read the test unless i mark it.

Chad
03-15-07, 12:15 PM
Ok, finally! I have found time to come back to my rudder question :yep:
Here's something I think it should look like, but doesn't work, so corrections are needed. Just one thing to remember, I'm using Blitz3d.

;Rudder Information
For Each KeyHit(26) TurnEntity conn,0,0.03,0 Until conn 0,0.24,0
;I have it stop at .24 to simulate 40 degrees, because, I want to go in increments of 5 degrees each key press, 5, 10, 15 etc, there would be 8 increments, ;and .03 * 8 = .24
For Each KeyHit(27) TurnEntity conn,0,-0.03,0 Until conn 0,0.24,0
EndIf
If KeyHit(40) Then TurnEntity conn,0,0,0 ; rudder amidships

Deamon
03-15-07, 01:08 PM
;Rudder Information
For Each KeyHit(26) TurnEntity conn,0,0.03,0 Until conn 0,0.24,0
;I have it stop at .24 to simulate 40 degrees, because, I want to go in increments of 5 degrees each key press, 5, 10, 15 etc, there would be 8 increments, ;and .03 * 8 = .24
For Each KeyHit(27) TurnEntity conn,0,-0.03,0 Until conn 0,0.24,0
EndIf
If KeyHit(40) Then TurnEntity conn,0,0,0 ; rudder amidships No Chad no, i mean express it in Pseudo code not code! You know what pseudocode is ?

Also explain me what the problem is or give me the full code and i will look it up here.

Chad
03-15-07, 04:05 PM
I don't know what psuedo code is except that it's in plain language, no coding.

Ready and open subscriber file
Get a record
Do while more records
If current subscriber subscription count is > 3
then Output the record
Get another record
end
Release the subscriber file.

is an example of psuedo code, but I really don't know how to put this in for my rudder adjustments

Deamon
03-16-07, 10:17 AM
I don't know what psuedo code is except that it's in plain language, no coding. Exactly. You formulate each step of the code by plain language.

is an example of psuedo code, but I really don't know how to put this in for my rudder adjustments Uhh, you can't exaplain what your rudder is supposed to do in plain language, although you can even do this kinda in code ?

You kidden me, ain't you ? :yep:

Anyway i will give you an example:

If key(A) is pressed and rudder is > -40, turn rudder to prt by n°/s

else if key(D) is pressed and rudder is < 40, turn rudder to prt by n°/s Simple eh ?

But why turn it for n° per second ? becose when you not turn it vs time it will be turned by n° each frame. This will have the side effect that the turnrate will always change according to the framerate which always fluctuates and is very different from system to system.

If you want to make sure that the rudder never turn more than 40°, cose in the example above it will stop to turn only when it's already above the 40. It will be just a tiny little bit above it maybe not noticeable at all but if you want it to be 100% correct then you can express it like this:

If key(A) is pressed and rudder is > (-40 - n° * Millisec()), turn rudder to prt by n°/s

else if key(D) is pressed and rudder is < (40 - n° * Milisec()), turn rudder to prt by n°/s

More efficient would be to express it like this:


; The ammount the rudder is supposed to spin in this frame
rudderSpin = n°/s * Millisec() ; n°/s is the ammount the rudder should spin in a second. Milisec simple reduce this value depending on the frame rate so that the rudder always spin by the same ammount per second regardless of the framerate. For n°/s use a constant. Don't use numerical values in your code.

If key(A) is pressed and rudder is > (-40 - -rudderSpin), turn rudder to prt by -rudderSpin

else if key(D) is pressed and rudder is < (40 - rudderSpin), turn rudder to prt by rudderSpin

With such a simple code you don't need pseudo code but if you are still a beginner it might help alot.

Ok Chad now show me if you can transform this into code.

Chad
03-16-07, 01:11 PM
Ok.. let me try this then..

Const n/s = 0.03(1000 Millisec()

rudderspin = n/s

If keyhit() And Rudder < (-40 : rudderspin) Turn entity 0,-0.03,0 : rudderspin
If Keyhit() And Rudder > (40 : rudderspin) Turn Entity 0,0.03,0 :rudderspin

Deamon
03-16-07, 02:18 PM
Ok.. let me try this then..

Const n/s = 0.03(1000 Millisec())

rudderspin = n/s LOL,

It's pointless to define a constant and apply Millisec() on it cose a constant is being defined only one time but we need to update the turn rate each frame so:

Const n/s = 0.03

rudderspin = n/s * (1000 Millisec())

rudderspin = n/s * (1000 Millisec()) Have of course to be placed within the game loop!!!

And n/s was just an example. Give it an explicite name that is self exaplaining, like:

Const rudderTurnCoefficient = 0.03

or

Const rudderTurnCoef = 0.03

The turn coefficient appears to me way to low though. Your value meant the rudder will turn 0.03 ° per second. So it will take 22.222221 minutes before the rudder will turn to 40° lol

Take maybe 2° or 3° else experiment till you get the desired effect.

BTW: The whole point of constants is that they are defined only one time and cannot be changed again.

If keyhit() And Rudder < (-40 : rudderspin) Turn entity 0,-0.03,0 : rudderspin
If Keyhit() And Rudder > (40 : rudderspin) Turn Entity 0,0.03,0 :rudderspin
Again make it a habit to not use numerical values in functions, so:

If keyhit() And Rudder < (-40 : rudderspin) Turn entity 0, -rudderspin, 0
If Keyhit() And Rudder > (40 : rudderspin) Turn Entity 0, rudderspin, 0

Generaly when you code gets longer and more complex and you start to use the same falue in numerical form on many different places of your code you will have to finde and change each entry when you suddenly want to alter this value. This is not only combersome but you might overlook to change a value somewhere and have suddenly a bug and don't know why. So always use variables or constants instead. Becose when you want to change the general value you just need to change it in this variable or constant.

When you have made a software that makes many calculations that use that current taxrate and the taxrate gets changed by the government then you would have to change each taxrate entry throughtout the whole code. But when you have used a constant for the taxrate instead a numerical value then all you have to do is to change the declaration of that constant and recompile.

And when you have 2 If's from which only one can be true in this pass then use If and ElseIf and not If If. Cose when the first If is true then the second one don't even need to be checked, otherwise you waste speed. So:

If keyhit() And Rudder < (-40 : rudderspin) Turn entity 0, -rudderspin, 0
ElseIf Keyhit() And Rudder > (40 : rudderspin) Turn Entity 0, rudderspin, 0