Okay, here’s a fun one.
I’m making this tank game. I have a simple model. The barrel of the tank is a separate model that is childed to the tank “character,” and rotates yaw and pitch with the mouse.
On a mouse click I have it instantiate a ball and ApplyForce to shoot it across the map. I’m having trouble instantiating the ball at the end of the barrel.
Here are the relevant sections:
--makes the tank. I've taken out the wheels and other parts. Below is the hull and the turret top with the barrel
function CreateCharacter()
characterNode = scene_:CreateChild("Player")
characterNode.position = Vector3(0.0,0.0, 0.0)
main= characterNode:CreateChild("Player")
main.position = Vector3(0.0, 0.0, 0.0)
local object = main:CreateComponent("StaticModel")
object.model = cache:GetResource("Model", "Models/hull.mdl")
local body = characterNode:CreateComponent("RigidBody")
body.collisionLayer = 1
body.mass = 100.0
body.angularFactor = Vector3(0.0, 1.0, 0.0)
body.collisionEventMode = COLLISION_ALWAYS
body:SetAngularDamping(.9)
body:SetLinearDamping(.9)
local shape = characterNode:CreateComponent("CollisionShape")
shape:SetBox(Vector3(3.6,1.8,7.5))
barrel= characterNode:CreateChild("Player")
barrel.position = Vector3(0.0, 0, 0.0)
local barrel = tube:CreateComponent("StaticModel")
barrel.model = cache:GetResource("Model", "Models/top.mdl")
...
end
--this just creates a bullet
function Shoot()
local pos=characterNode:GetPosition()
local objectNode = scene_:CreateChild("Box")
objectNode.position=Vector3(characterNode.position.x,characterNode.position.y+1.8,characterNode.position.z) + objectNode.rotation* Quaternion(character.controls.yaw, Vector3(0.0, 1.0, 0.0))* Quaternion(character.controls.pitch, Vector3(0.0, 0.0, 1.0)) * Quaternion(0,90,-character.controls.pitch)
local yaw = Quaternion(character.controls.yaw+90, Vector3(0.0, 1.0, 0.0))
local pitch=Quaternion(character.controls.pitch, Vector3(0.0, 0.0, 1.0))
local dir = yaw * pitch
objectNode.rotation=dir
objectNode:SetScale(Vector3(.5,.5,.5))
local object = objectNode:CreateComponent("StaticModel")
object.model = ball
local body = objectNode:CreateComponent("RigidBody")
body.mass = 2
body.collisionEventMode = COLLISION_ALWAYS
local shape = objectNode:CreateComponent("CollisionShape")
shape:SetBox(Vector3(1.0, 1.0, 1.0))
body:ApplyImpulse(objectNode.rotation*Vector3(0,0,1)*500)
end