AnniXa Posted August 30, 2011 Share Posted August 30, 2011 Hi =) I have the following scenario: A simple invisible tbody that is flying like bullet throught the world, and now i wanted to attach a bullet or missile mesh to this body, and i wanted to calculate the rotation for the missile depending on the velocity vector of the body thingy, and then apply this rotation to the missile mesh and set it on the position of the body, so that it looks like the missile is flying like the body. But for any reason i cant find out how to calculate the rotation that i need for the missile, since the school is long time ago maybe someone knows a simple way to do oit? maybe there is some prebuild function with the vec3 ojects or something else like this that i missed, or maybe someone knows a simple formula or something for this? Quote Whuts wroong? Link to comment Share on other sites More sharing options...
flachdrache Posted August 30, 2011 Share Posted August 30, 2011 You most likely dont need to calculate that since Le is all "entity-based" ... you simply want to set the position / rotation of your bolt / bullet to the position / rotation of the body or set its complete EntityMatrix. I took "all" the commands i would need to build game-subsystems from the command list and wrote samples for it. It doesnt help much with integration the game-engine itself but game-mechanics can be extracted from it. here is some, slightly changed, script metraton posted (dont know how to write that name though) --stuff which should be part of LE --the best working round function, works also for negative idp (written by Robert Jay Gould) function round(num, idp) return tonumber(string.format("%." .. (idp or 0) .. "f", num)) end BLEND_NONE=0 BLEND_ALPHA=1 --end of stuff which should be part of LE local width=1920 local height=1080 require("Scripts/constants/collision_const") require("Scripts/constants/engine_const") require("Scripts/LinkedList") require("Scripts/filesystem") require("Scripts/math/math") require("scripts/classes/bullet") if fw==nil then --we are not in Editor RegisterAbstractPath("") Graphics(800,600) fw=CreateFramework() scene=LoadScene("abstract::Actors_obstacleDM1.sbx") scene:SetCollisionType(COLLISION_SCENE) TFilter() AFilter() SetHDR(true) SetSSAO(true) SetBloom(true) standalone=1 end --Variables dx=0.0 dy=0.0 camerapitch=0.0 camerayaw=0.0 move=0.0 strafe=0.0 cameraheight=1.9 --ScrennshotBuffers local buffer=CreateBuffer(width,height,BUFFER_COLOR) if buffer==nil then Notify("Failed to create buffer!",1) return end local buffer2=CreateBuffer(width,height,BUFFER_COLOR) if buffer2==nil then Notify("Failed to create buffer!",1) buffer=nil return end --array of "info_playerstart" ... classname --lobby might use different spots. local playerstart = nil playerstart = scene:FindChild("playerstart") --Create a player controller controller=CreateController(1.8, 0.46, 0.25, 45) controller:SetCollisionType(COLLISION_CHARACTER,0) controller:SetMass(10) SweptCollision(controller, 1) if standalone==1 then if playerstart ~= nil then controller:SetPosition( playerstart:GetPosition() ) else controller:SetPosition(Vec3(0,20,0)) end else controller:SetPosition(fw.main.camera.position) end camerapitch=fw.main.camera.rotation.x camerayaw=fw.main.camera.rotation.y+180 controller:Move(Vec3(0,-0.9,0)) local gunscale=0.6 local vwep = LoadMesh("abstract::vwep_hands.gmf") LoadMesh("abstract::vwep_gun.gmf",vwep) vwep:SetParent(fw.main.camera,0) vwep:SetPosition(Vec3(-0.18*gunscale,-0.03*gunscale,0.37*gunscale),0) vwep:SetScale(Vec3(0.04*gunscale,0.04*gunscale,0.04*gunscale)) local gundisplayposition = vwep:GetPosition() sound_gunshot = LoadSound("abstract::gunshot.ogg") source_gunshot = CreateSource(sound_gunshot) source_gunshot:SetVolume(0.5) vwep :SetShadowMode(0,1) local displayposition=Vec3(-0.26/2.0,-0.03,0.19) local muzzleflash = CreatePointLight(3) muzzleflash:SetParent( vwep ) muzzleflash:SetColor(Vec4(1,0.6,0.0,1.0)) muzzleflash:SetPosition( displayposition ) muzzleflash:SetShadowMode(0) HideMouse() MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2) FlushKeys() FlushMouse() local pick local camera = fw.main.camera local remainingtime local starttime=AppTime() local gameduration=2--length of game in minutes local gamemode=0 gunpos = vwep.position:Copy() local smoothedgunswayamplitude= 0.0 local smoothedgunswayspeed = 0.0 local guntime = 0.0 local recoil = 0.0 local lastfiretime=0.0 local smoothrecoil=0.0 local swaydamping=0.0 local smoothswaydamping=0.0 local lightsmoothing =0.0 local gunlight = 0.0 --Flashlight flashlight = {} flashlight.light = CreateSpotLight(8) flashlight.light:Hide() flashlight.sound_switch = LoadSound("abstract::switch.wav") flashlight.state=0 flashlight.light:SetConeAngles(30,35) flashlight.light:SetRotation(Vec3(5,0,0)) flashlight.light:SetShadowmapSize(512) flashlight.light:Paint(LoadMaterial("abstract::flashlight.mat")) function flashlight:SetState( state ) if state~=self.state then self.state=state if state==0 then self.light:Hide() else self.light:Show() end if self.sound_switch~=nil then self.sound_switch:Play() end end end function ShootBullet( position, direction ) -- local speed=100.0 -- local pick = LinePick( position, Vec3(position.x+direction.x * speed) ) end function DrawHUD(contr) SetBlend(BLEND_ALPHA) DrawText("pos: "..round(contr.position.x,3)..","..round(contr.position.y,3)..","..round(contr.position.z,3),1,FontHeight()*5) DrawText("rot: "..round(contr.rotation.x,3)..","..round(contr.rotation.y,3)..","..round(contr.rotation.z,3),1,FontHeight()*6) SetBlend(BLEND_NONE) end --main function while KeyHit(KEY_ESCAPE)==0 do jump=KeyHit(KEY_SPACE)*6.0 if controller:IsAirborne()==1 then jump=0 end local time = AppTime()/3200.0 local frame = time*(179.0-96.0)+96.0 frame=Clamp( frame, 96, 179 ) vwep:Animate(96,1,0,1) --Camera look gx=Round(GraphicsWidth()/2) gy=Round(GraphicsHeight()/2) dx=Curve((MouseX()-gx)/4.0,dx,3.0/AppSpeed()) dy=Curve((MouseY()-gy)/4.0,dy,3.0/AppSpeed()) MoveMouse(gx,gy) camerapitch=camerapitch+dy camerayaw=camerayaw-dx camerapitch=math.min(camerapitch,89) camerapitch=math.max(camerapitch,-89) fw.main.camera:SetRotationf(camerapitch,camerayaw,0,1) movespeed=6 movesmoothing=10 if controller:IsAirborne()==1 then movesmoothing=200 end --Player movement move=Curve( (KeyDown(KEY_W)-KeyDown(KEY_S))*movespeed,move,movesmoothing) strafe=Curve( (KeyDown(KEY_D)-KeyDown(KEY_A))*movespeed,strafe,movesmoothing) --Use objects if KeyHit(KEY_E)==1 then pick=CameraPick(camera,Vec3(GraphicsWidth()/2,GraphicsHeight()/2,2.0),0,0) if pick~=nil then repeat if pick.entity:GetClass()==ENTITY_MODEL then break end pick.entity=pick.entity.parent until pick.entity==nil if pick.entity~=nil then pick.entity:SendMessage("use",controller,0) end end end --crouching if KeyHit(KEY_C)==1 then if controller:IsCrouched()==1 then crouch=0 cameraheight=1.7 else crouch=1 cameraheight=0.85 end end --Update controller --controller:Update(camerayaw,move,strafe,jump,40,10) controller:Update(camerayaw,move,strafe,jump,40,10,crouch) fw:Update() if KeyHit(8)==1 then dofile("GUIindex.lua") end if KeyHit(KEY_F)==1 then flashlight:SetState(1-flashlight.state) end --Position camera camera:SetPositionf(controller.position.x,controller.position.y+cameraheight,controller.position.z,1) time=AppTime() gunfirefrequency=80 gunswayspeed=0.001*20.0 gunoffset = gunpos:Copy() gunswayamplitude = 0.02 if KeyDown(KEY_W)==1 or KeyDown(KEY_D)==1 or KeyDown(KEY_A)==1 or KeyDown(KEY_S)==1 then gunswayamplitude = 0.03 gunswayspeed=0.005*20.0 end smoothedgunswayamplitude = Curve( gunswayamplitude, smoothedgunswayamplitude, 8.0 ) smoothedgunswayspeed = Curve( gunswayspeed, smoothedgunswayspeed,8.0 ) if smoothrecoil<0.001 then guntime = guntime + AppSpeed() * smoothedgunswayspeed * math.max(0.0,1.0 - smoothswaydamping) end gunoffset.z = gunoffset.z - smoothrecoil * 0.05 smoothedgunswayamplitude = smoothedgunswayamplitude * (1.0 - smoothswaydamping) gunoffset.x = gunoffset.x + math.sin( guntime ) * smoothedgunswayamplitude * gunscale gunoffset.y = gunoffset.y + (1.0-math.cos( guntime*2.0 )) * 0.005 * gunscale * math.min(1.0,1.0 - smoothswaydamping) vwep:SetPosition( gunoffset ) recoil = recoil-0.1 swaydamping = math.max( swaydamping - 0.05, 0.0 ) recoil = math.max(recoil,0.0) smoothrecoil=Curve(recoil,smoothrecoil,3.0) smoothswaydamping = Inc( swaydamping ,smoothswaydamping,0.01 ) gunlight = math.max( gunlight- 0.2, 0.0 ) lightsmoothing =gunlight-- Curve(gunlight,lightsmoothing,8.0) muzzleflash:SetColor(Vec4(1.0*lightsmoothing,0.6*lightsmoothing,0.0,1.0)) if lightsmoothing <0.01 then muzzleflash:Hide() end if MouseDown(1)==1 then if AppTime()-lastfiretime>gunfirefrequency then recoil = 1.0 lastfiretime=AppTime()+math.random(0,20) gunswayspeed=0.0 gunlight = 1.0 source_gunshot:Play() source_gunshot:SetPitch(1.0 + (math.random()-0.5)*0.05 ) swaydamping = 1.0 muzzleflash:Show() CreateBullet(vwep:GetPosition(1) , fw.main.camera.mat:K():Scale(300)) end end if MouseHit(2)==1 then phyproj = CreateBodyBox(1,1,1) projectile = CreateCube(phyproj) projectile:SetColor(Vec4(math.random(0,255)/255,math.random(0,255)/255,math.random(0,255)/255,1)) phyproj:SetMass( 1 ) phyproj:SetCollisionType(1,0) local V = TFormVector(Vec3(0, 0, 2), fw.main.camera, nil) V = Vec3(fw.main.camera.position.x + V.x, fw.main.camera.position.y + V.y, fw.main.camera.position.z + V.z) PositionEntity(phyproj, V, 0) RotateEntity(phyproj, EntityRotation(fw.main.camera, 0), 0) phyproj:AddForcef(0, 100, 1500, 0) end UpdateBullets() flashlight.light:SetPosition(fw.main.camera:GetPosition(1)) flashlight.light:SetRotationf( CurveAngle( fw.main.camera.rotation.x, flashlight.light.rotation.x, 3.0/AppSpeed() ), CurveAngle( fw.main.camera.rotation.y, flashlight.light.rotation.y, 3.0/AppSpeed() ) ) flashlight.light:Movef(-0.07,-0.04,0.02) fw:Render() -- screenshot : note the second fw:Render() if KeyDown(KEY_F12)==1 then SetBuffer(buffer) fw:Render() SetBuffer(buffer2) DrawImage(GetColorBuffer(buffer),0,0,buffer:Width(),buffer:Height()) SetBuffer(BackBuffer()) SaveBuffer(buffer2,"screenshot.png") end DrawHUD(controller) Flip(0) end buffer=nil buffer2=nil controller:Free() vwep:Free() ShowMouse() hth Quote AMD 64 X2 Dual 5k - 4GB - XFX GForce9800GT - nv196.21 - WinXP Sp3 zBrush4R2 - Silo2Pro - Unwrap3DPro - Gile - MaPZone2.5 Xxploration FPS in progress ... Link to comment Share on other sites More sharing options...
Mumbles Posted August 30, 2011 Share Posted August 30, 2011 I'm not quite sure what you mean. to attach a TMesh to a TBody is done by setting the mesh's parent to the body. EntityParent(Your Missile,your invisible body,1) to then make the mesh "look in the same direction" RotateEntity(Your missile,EntityRotation(invisible body,1),1) And now the mesh will move with the bullet automatically... Quote LE Version: 2.50 (Eventually) Link to comment Share on other sites More sharing options...
Josh Posted August 30, 2011 Share Posted August 30, 2011 For bullets I would perform a raycast each frame, and use AlignToVector to orient the visual mesh. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Road Kill Kenny Posted August 30, 2011 Share Posted August 30, 2011 I'm not quite sure what you mean. to attach a TMesh to a TBody is done by setting the mesh's parent to the body. EntityParent(Your Missile,your invisible body,1) to then make the mesh "look in the same direction" RotateEntity(Your missile,EntityRotation(invisible body,1),1) And now the mesh will move with the bullet automatically... If you load the mesh and the body at 0,0,0 and then parent it and then position the body and fire it you shouldn't have to rotate the mesh to be in line. Provided that the body and the mesh match.... But I would recommend what Josh said anyway, align to vector would be the best way to go Quote STS - Scarlet Thread Studios AKA: Engineer Ken Fact: Game Development is hard... very bloody hard.. If you are not prepared to accept that.. Please give up now! Link to comment Share on other sites More sharing options...
AnniXa Posted August 31, 2011 Author Share Posted August 31, 2011 You most likely dont need to calculate that since Le is all "entity-based" ... you simply want to set the position / rotation of your bolt / bullet to the position / rotation of the body or set its complete EntityMatrix. I took "all" the commands i would need to build game-subsystems from the command list and wrote samples for it. It doesnt help much with integration the game-engine itself but game-mechanics can be extracted from it. here is some, slightly changed, script metraton posted (dont know how to write that name though) --stuff which should be part of LE .... hth Thank you very well for this clearifing example code! I'm not quite sure what you mean. to attach a TMesh to a TBody is done by setting the mesh's parent to the body. EntityParent(Your Missile,your invisible body,1) to then make the mesh "look in the same direction" RotateEntity(Your missile,EntityRotation(invisible body,1),1) And now the mesh will move with the bullet automatically... I tried that, but since the body is not rotating well into the fly direction this not looked so good. For bullets I would perform a raycast each frame, and use AlignToVector to orient the visual mesh. If you load the mesh and the body at 0,0,0 and then parent it and then position the body and fire it you shouldn't have to rotate the mesh to be in line. Provided that the body and the mesh match.... But I would recommend what Josh said anyway, align to vector would be the best way to go i think thats what i do now =) Quote Whuts wroong? Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.