Bird Flight Physics

Hi, I am currently working on a school coding project which has to relate to physics in some way. I want to create something like bird simulator where you can fly in the direction you are looking. I also want to make it such that when you pitch down your airspeed would increase. The bird would be controlled by your mouse.
The problem: I do not understand how to use the body movers to make you fly in the direction that your mouse is pointing in
Any help would be great. Thanks!

1 Like

Not sure how far you are willing to go for a school project, but even the basic form of these physics can be difficult to implement. It will definitely take some work to put everything together properly.

Here’s a page with information on basic aerodynamics: This site has moved to a new URL

1 Like

Alright, I will take a look. Thanks!

This will do it.

local userInputService = game:GetService(“UserInputService”)
local runService = game:GetService(“RunService”)

local plr = game.Players.LocalPlayer
local char = plr.Character
local hRP = char.HumanoidRootPart

local camera = game.Workspace.CurrentCamera

local flying = false
local speed = .5

local bodyPos = Instance.new(“BodyPosition”,hRP)
bodyPos.MaxForce = Vector3.new()
bodyPos.D = 10
bodyPos.P = 10000

local bodyGy = Instance.new(“BodyGyro”,hRP)
bodyGy.MaxTorque = Vector3.new()
bodyGy.D = 10

local function flight()
flying = true
bodyPos.MaxForce = Vector3.new(500000, 500000, 500000)
bodyGy.MaxTorque = Vector3.new(400000, 400000, 400000)
while flying do
–[[ local dragon = char:WaitForChild(“Humanoid”)
local flyAnim = dragon.LoadAnimation() – remove once flight animation is ready

	flyAnim:Play()--]]
	runService.RenderStepped:Wait()
	bodyPos.Position = hRP.Position + ((hRP.Position - camera.CFrame.p).unit * speed)
	bodyGy.CFrame = CFrame.new(camera.CFrame.p, hRP.Position)
end

end

local function endFlight()
bodyPos.MaxForce = Vector3.new()
bodyGy.MaxTorque = Vector3.new()
flying = false
end

userInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
if not flying then
flight()
else
endFlight()
end
end
end)

2 Likes

Thanks, But can you help me understand this?

1 Like

Im not very good at explaining things in writing. Pressing f will make your character fly and you can change the direction with looking. This is more of a character manipulation that a direct correlation to real world physics. I have a physics degree.

1 Like

Ever heard of Bernoulli’s principle? Now I’m sure incorporating that into the game is going to be a nightmare, but it would be a really good achievement if accomplished.

2 Likes

If you want to simplify your ‘bird’ controls to simply be a Velocity in the direction of your mouse + speed controlled by pitch, it’s pretty simple.

velocity = unit_direction * speed_constant * pitch.

Firstly, you need to determine unit_direction. You can do this simply with (mouse.Hit - current_position).Unit. Next, you need to determine pitch. To do this easily, you can actually reuse your unit_direction. Just take the y-component. When you look straight down, your pitch would simply be 1. During straight flight, it is 0. Lastly is your speed_constant. This determines how fast you go depending on your pitch. Keep in mind this means that during level flight, you will have a velocity of zero (aka not move). To resolve this, simply add a constant velocity term with unit_direction.

Applying the velocity to your character is up to you. You could implement velocity explicitly by updating the position/CFrame in a loop. Or you could leave it to the Roblox engine, and use a BodyMover/Velocity property.

If you want your flight controls to be more realistic (ie; incorporating drag, lift, etc), I would hold off from that until you’ve taken an introductory physics class and have become familiar with vector basics and simple Newtonian physics. From there, you should have the knowledge/tools to incorporate force/drag equations to create a realistic bird/plane/whatever.

4 Likes