This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Creator's profile picture

Need Help With In game Physics Engine - How Does In Game Gravity Work?

Started by Creator, 26 February 2015 - 01:28 PM
Creator #1
Posted 26 February 2015 - 02:28 PM
Hello guys,

I was trying to make a game where the main character has to jump. Hoewever I had some problems with the physics engine. I wrote some test code and implemented the 1st, 2nd, and 3rd law of motion. The dot on the screen has some strange behavior. There are several possible reasons:

1. The coordinates are inverted: 1 is on the top and increases when we go downwards.
2. I don't get physics. ;)/>

Here is the test code:


local G = 1
local speed = 1
local position = 3
local w,h = term.getSize()
local timeS = os.clock()

function draw()
  term.setCursorPos(1,1)
  print(diff)
  print(speed)
  print(timeS)
  print(position)
  sleep(1)	  
  term.setBackgroundColor(colors.black)
  term.clear()  
  term.setBackgroundColor(colors.red)  
  term.setCursorPos(math.floor(w/4),position)  
  term.write(" ")
end

function phy()
  diff = os.clock()- timeS	  
  position = position + (speed*diff) + (G*diff*diff)/2  
  timeS = os.clock()
  speed = speed + G*diff
end

while true do
  sleep(.5)	
  phy()
  draw()
end

G is acceleration. I put it to 1 for testing reasons and because the screen is small and there are only 19 posible positions on the y axis and if it was 9.8 the dot would practically never be on the screen.
The sleep .5 is normally .05 but I put it to 0.5 in order to be able to visualize the movement. Any help is appreciated.

Thanks a lot, and be kind ( It's my first physics engine)

~Creator
Edited on 26 February 2015 - 03:33 PM
Lemmmy #2
Posted 26 February 2015 - 05:00 PM
I'm not too sure what the problem is exactly because it looks pretty fine to me. I clamped the position to the screen height and it worked perfectly, even if slow. Could you describe the strange behaviour you are experiencing?
Creator #3
Posted 26 February 2015 - 05:02 PM
I'm not too sure what the problem is exactly because it looks pretty fine to me. I clamped the position to the screen height and it worked perfectly, even if slow. Could you describe the strange behaviour you are experiencing?

The pixel goes out of the screen and never comes back. Moreover, the idea is that the pixel goes up and fals down.
wieselkatze #4
Posted 26 February 2015 - 05:17 PM
Okay, I remember that topics from my physics class - not that long ago.
The thing why your character is only moving up, is, because you're never actually subtracting anything from your cursor position.
I took a try on that thing and came up with the following - usually you have to use v*sin( a )*t, but as the angle we're jumping from is 90°, it's 1 all the time.
v is in m/s - 9.81 is gravitation speed ( 9.81 m/s² ).


local v = 15 -- m/s
local g = 9.81 --m/s^2
local diff = 0.05

local function write( pos )
    term.setBackgroundColor( colors.black )

    for i = 1, 19 do
	    term.setCursorPos( 1, i )
	    term.write( " " )
    end

    term.setBackgroundColor( colors.red )
    term.setCursorPos( 1, pos )
    term.write( " " )
end

local function getPosition()
    local pos = 0
    local t = 0

    repeat
	    pos = v*t - 0.5*g*math.pow( t, 2 )
	    write( 19-pos )
	    t = t + diff
	    sleep( diff )
    until pos <= 0 and t > diff
end

getPosition()

Creator #5
Posted 26 February 2015 - 05:33 PM
Okay, I remember that topics from my physics class - not that long ago.
The thing why your character is only moving up, is, because you're never actually subtracting anything from your cursor position.
I took a try on that thing and came up with the following - usually you have to use v*sin( a )*t, but as the angle we're jumping from is 90°, it's 1 all the time.
v is in m/s - 9.81 is gravitation speed ( 9.81 m/s&sup2; ).


local v = 15 -- m/s
local g = 9.81 --m/s^2
local diff = 0.05

local function write( pos )
	term.setBackgroundColor( colors.black )

	for i = 1, 19 do
		term.setCursorPos( 1, i )
		term.write( " " )
	end

	term.setBackgroundColor( colors.red )
	term.setCursorPos( 1, pos )
	term.write( " " )
end

local function getPosition()
	local pos = 0
	local t = 0

	repeat
		pos = v*t - 0.5*g*math.pow( t, 2 )
		write( 19-pos )
		t = t + diff
		sleep( diff )
	until pos <= 0 and t > diff
end

getPosition()


Thanks.

I fixed the code and it seems to work.


--Variables--
term.redirect(term.native())
local w,h = term.getSize()
local gameWindow = window.create(term.current(),1,2,w,h-1,true)
local tW, tH = gameWindow.getSize()
local dead = false
local position = math.floor(tH/2)
local G = -1
local speed = 4
local w,h = term.getSize()
local timeS = os.clock()
local defaultConfing = {
birdColor = colors.yellow,
bgColor = colors.black
}
--Functions--
local GUI = {}
local Physics = {}
local Events = {}
function GUI.DrawPixel()
gameWindow.setBackgroundColor(defaultConfing.bgColor)
gameWindow.clear()
gameWindow.setCursorPos(math.floor(tW/4),tH-position)
gameWindow.setBackgroundColor(defaultConfing.birdColor)
gameWindow.write(" ")
end
function Physics.PixelMove()
diff = os.clock()- timeS	  
position = position + (speed*diff) + (G*diff*diff)/2
timeS = os.clock()
speed = speed + G*diff
end
function Events.main()
local timer = os.startTimer(.05)
local event, p2 = os.pullEvent()
if event == "key" and p2 == keys.space then
  os.cancelTimer(timer)
  direction = "up"
  speed = 3
  move = true
end
end
--Code--

GUI.DrawPixel()
while true do
if dead then
  error("you died")
end
Events.main()
GUI.DrawPixel()
Physics.PixelMove()
end

Hint, it is for a game that you may soon see in the program section.

Feel free to guess the name of the game. I have replaced the name of the main character with Pixel.

Post your guesses!
Edited on 26 February 2015 - 04:41 PM