Tank Controls
Blitz3D Forums/Blitz3D Beginners Area/Tank Controls
| ||
Hey guys, playing around with some commands for a little tank application and ran into a stopping point. I've got 2 meshes, the hull (bottom part) and the turret (the top part) of the tank, and what I'm trying to do is be able to move the hull around with the turret child to the hull, but on a different pivot so I can move my mouse and move the turret around with the mouse. However, while I'm moving my mouse around and stuff, it does not effect the hull, it keeps going the same direction as it was though I'm looking a different way. I would also like the steering controls so instead of a strafing action with A and D or the left and right arrows, the hull rotates depending on what direction you want it too. So if you could take a look and tell me what I'm missing or doing incorrectly please? Thanks, Chad Include "BlitzGrass.bb" Type Timer Field start Field timeOut Field vel_start#;=90 Field vel_down#;=-2 End Type Type Tank Field mesh, turret Field Yturn#, speed# Field ammo, damage# Field AI Field firetime Field team End Type ;System Global AppFPS =45 Global screen_width% = 1024 Global screen_height% = 768 Global screen_halfwidth% = screen_width/2 Global screen_halfheight% = screen_height/2 Global camHeight#=4.5 ; default height of the camera behind player Global camDist#=-7 ; default distance of the camera behind player Global dest_xang# Global dest_yang# Global pl_gravity#=-.5 Global Move_For_Back# Global Move_Left_Right# Global Move_Slow#=.1 ;CONST Const C_PLAYER=1, C_LEVEL=2 AppTitle "TPS Camera movement" HidePointer ;Screen Graphics3D screen_width,screen_height,32,1 SetBuffer BackBuffer() CreateWorld() ;CAMERA camera =CreateCamera() ;Player Pivot plpivot=CreatePivot() EntityType plpivot, C_PLAYER EntityRadius plpivot,1.8,3 PositionEntity plpivot, 0, 3,0 ;hull pivot hullpivot=CreatePivot() EntityType hullpivot,C_PLAYER EntityRadius hullpivot,1.8,3 PositionEntity hullpivot,0,2,0 ;-- Add grass ;First init must be called. Normally you would supply a terrain to this function, but ;in this demo no terrain is used. BG_Init() ;This adjusts the viewing range of the grass and the density factor. This can be changed ;based on your system's speed BG_SetLOD(50, 1) ;Load the grass layer grassLayer = BG_LoadGrass("grass.png") BG_SetDensity(grassLayer, 300) BG_SetScale(grassLayer, 4, 2) ;And set the regions of grass density through a greyscale grassmap ;BG_SetGrassMap(grassLayer, "grassmap.jpg") ;tankhull=LoadMesh("panzer\hull.b3d",plpivot) ;MoveEntity tankhull,0,-2.257,0 ;EntityType tankhull,C_PLAYER ;EntityRadius tankhull,.8 ;ScaleEntity tankhull,.065,.065,.065 ;tankturret=LoadMesh("panzer\turret.b3d",plpivot) ;MoveEntity tankturret,0,-2.257,0 ;EntityType tankturret,C_PLAYER ;EntityRadius tankturret,.8 ;ScaleEntity tankturret,.065,.065,.065 tankhull=CreateCube(plpivot) MoveEntity tankhull,0,0,0 EntityColor tankhull,0,0,255 ;I'm using 2 pivots for camera controlling ;Pivot for the camera controlling ;One Front of the player, FrontPivot=CreatePivot(plpivot) PositionEntity Frontpivot,0,4,20 bottompivot=CreatePivot(hullpivot) PositionEntity bottompivot,0,0,20 ;One behind player ;This pivot controlls the collisions and "finds" the right position of the camera behind player Backpivot=CreatePivot(plpivot) EntityRadius Backpivot,1 EntityType BackPivot, C_PLAYER Collisions C_PLAYER, C_LEVEL ,2,2 ; ------------- ; Main loop ; ------------- FramePeriod = 1000 / AppFPS FrameTime = MilliSecs () - FramePeriod While Not KeyHit(1) BG_Update(camera) ; -------------------------- ; Frame rate limiting ; -------------------------- Repeat FrameElapsed = MilliSecs () - FrameTime Until FrameElapsed FrameTicks = FrameElapsed / FramePeriod FrameTween# = Float (FrameElapsed Mod FramePeriod) / Float (FramePeriod) ; ----------------------- ; Update tweening ; ----------------------- For FrameLimit = 1 To FrameTicks If FrameLimit = FrameTicks Then CaptureWorld EndIf FrameTime = FrameTime + FramePeriod UpdateCamera(plpivot,MouseXSpeed(),MouseYSpeed(), Frontpivot, Backpivot, Camera, hullpivot) UpdatePosition(plpivot) UpdatePosition(hullpivot) UpdateWorld Next RenderWorld FrameTween ; Crosshair :) Text screen_halfwidth%, screen_halfheight%, "+" t = MilliSecs() dt = t - lastt lastt = t Text 10, 10, (1000.0 / dt) + "FPS" Flip 0 Wend End Function updatecamera(ent,mxs#, mys#, pivot1, pivothatso, camera, hullpivot) ;ent : Player pivot ;mxs: MouseXmove ;mys: MouseYmove ;pivot1: pivot before player ;pivothatso: pivot behind player ;Camera: MoveMouse screen_halfwidth%, screen_halfheight% ; Limit of the "look up", "look down" If dest_xang + mys > 120 Or dest_xang + mys < -45 Then ; mys=0 EndIf dest_xang# = dest_xang + mys ; When the player looks up, the front pivot moves UP, when looks down the back pivot moves down TranslateEntity pivot1,0,-MYS#*.15,0 angle#= Sin(EntityPitch(camera))*6 tempkkammag#=camheight + (angle*.8) tempkkamtav#= camdist + Abs( angle) dest_yang# = dest_yang - (mxs*.5) RotateEntity ent, 0, dest_yang,0 dx#=(EntityX(pivothatso,True)-EntityX(camera,True)) *.1 dy#=(EntityY(pivothatso,True)-EntityY(camera,True)) *.9 dz#=(EntityZ(pivothatso,True)-EntityZ(camera,True)) *.1 TranslateEntity camera,dx,dy,dz PointEntity camera,pivot1 PositionEntity pivothatso,0,0,0 ResetEntity pivothatso PositionEntity pivothatso,0,tempkkammag,tempkkamtav End Function Function UpdatePosition(ent) ;MOVEMENT If pl_jump=0 If KeyDown(200) Or KeyDown(17) Then Move_For_Back=.3 End If If KeyDown (208) Or KeyDown(31) Then Move_For_Back=-.2 End If If KeyDown(203) Or KeyDown(30) Then Move_Left_Right=-.2 End If If KeyDown (205) Or KeyDown(32) Then Move_Left_Right=.2 End If EndIf MoveEntity ent, Move_Left_Right, Jump_Move, Move_For_Back TranslateEntity ent,0,pl_gravity,0 ;Smooth movement move_left_right = move_left_right * move_slow move_for_back = move_for_back * move_slow End Function Function CreateWorld() ;Light light=CreateLight(1) PositionEntity light ,0,50,0 ;Plan plan=CreatePlane() planetex=LoadTexture("grass.jpg") EntityTexture plan,planetex ;EntityColor plan,255,0,0 EntityType plan, C_LEVEL ;prop=LoadMesh("panzer\panzer.b3d") ;PositionEntity prop,25,0,20 ;ScaleEntity prop,.065,.065,.065 End Function |
| ||
Hmm....well, you could start the game by pointing the turret to the mouse. Then, you can update the turret by using MouseXSpeed() and MouseYSpeed(). For example: ---------------------------------------------- TurnEntity turret,MouseYSpeed()/5,MouseXSpeed()/5,0 ---------------------------------------------- ...or something like that. Make sure you include the MouseXSpeed()/5 and MouseYSpeed()/5. You can change the number if you want the turret to spin at a different speed, but if you leave it out, the turret will spin like crazy when you move the mouse! With the hull, you could put in a variable that keeps track of the current rotation of the hull\non-turret part and use RotateEntity. For example: --------------------------------------------- If KeyHit(30) Then rx=rx-2 If KeyHit(32) Then rx=rx+2 RotateEntity hull,0,rx,0 --------------------------------------------- |
| ||
Doesn't work correctly but I know what you're saying, and I've tried that... Any other suggestions? |
| ||
...nope. Sorry, that's all the ideas I have right now. If I think of something I'll post it. |
| ||
Maybe you could avoid parenting the turret at the hull, and use PositionEntity turret, EntityX(hull), EntityY(hull), EntityZ(hull) instead ? Or use something like this ? |
| ||
I'm not sure I'm following what you're trying to achieve and can't try out your code because of dependencies, but I'll put my tuppence in, in case it's of help as I've recently done a tanks game along these lines (http://www.bankie.com/games/tanks/tanksonline.php). What I wanted to do was make the turret turn towards the mouse pointer (which appears as a crosshair on the ground), and the tank move using cursors keys (or whatever). Left and right keys to rotate the tank, and up/down to move forwards/backwards. To achieve this, I didn't use pivots, but made the turret a child of the hull. Left/right keys caused a TurnEntity hull command. Up/down keys caused a MoveEntity hull command. The position of the mouse cursor and some simple trigonometry was used to calculate the rotational angle of the turret (RotateEntity). Hope that's of some help. |