Physics question

Game development specific discussions.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Physics question

Post by dodicat »

Hi deltarho.
There is not too much going on, pset about 332 points + the number of polygon vertices chosen.
And painting the polygon.
The floating points are just moving and turning those points (sin, cos and some + - * /, but not many.
I tested on win XP 32 bits, OK, I get the full 60 fps.
I tested on Linux 32 bits, OK also.
And I coded here on win 10, no problems here, 32 bit gas/gcc and 64 bit optimised and not optimised.
The cpu jumps to maybe 40% while the points are dragged, but settles down to abour 12%.
Maybe you could put a sleep 10 in the pickanumber function before Loop Until ret to get your cpu settled.
fb 1.06 (official version)
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Physics question

Post by deltarho[1859] »

Thanks dodicat. No luck there.

The drag & drop can take between 10 and 20 seconds to work but the drop is nowhere near where it should be.

I will leave it and wait to see if anyone else has any issues. I don't want to waste your time so if no one else reports any issues then, probably, there is an incompatibility with my machine.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Physics question

Post by srvaldez »

it works poorly on my Windows 10 VM, dragging the red ball it not at all smooth
on the other hand it works smoothly on macOS, however if I drag the red dot around and around, the CPU usage rises to 92%
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Physics question

Post by BasicCoder2 »

Produced erratic behavior on my Windows10 as well. The program just didn't work properly. Tried to move a dot and nothing happened and then it went silly so I just gave up. The "angle example" for badidea worked ok.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Physics question

Post by deltarho[1859] »

srvaldez wrote:however if I drag the red dot around and around, the CPU usage rises to 92%
This can happen when code is running in a tight loop, so we need to release the time slice. This is not necessary for each iteration, perhaps every 128 or 256. However, to keep things simple I inserted 'Sleep 0' before the end of each of the four loops employed. This helped a great deal but the drop position was still incorrect. I need to remove the 'Sleep 0' one at a time to establish which loop is 'bottlenecking'.

I also noticed short oblique lines on the screen as if it was raining so something untoward is going on with the graphics.

The bottlenecking is CPU dependent, the more powerful it is the more likely the problem.

Why is the drag & drop not dropping where it should?
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Physics question

Post by srvaldez »

deltarho[1859] wrote: I also noticed short oblique lines on the screen as if it was raining so something untoward is going on with the graphics.
that I believe is by design, look at the code for rain.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Physics question

Post by deltarho[1859] »

srvaldez wrote:that I believe is by design, look at the code for rain.
In deep space? In deep space we are down to about three degrees Kelvin. Perhaps we are near to a black hole and in a Hawking radiation storm. <smile>
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Physics question

Post by deltarho[1859] »

I removed the 'rain'. That helped. I then removed all the 'Sleep 0', one at a time, but did not establish a bottleneck. So, the rain was problematic. Perhaps the rain should have been in a separate thread of execution (TOE). I have noticed that many, if not all, graphics apps are single core and have wondered whether any of them would benefit from having some code in a TOE.

The drag & drop definitely needs looking at but I don't think that I would be much help there.

Added: Come to think of it using a Threadpool may be better than a standard thread creation.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Physics question

Post by deltarho[1859] »

Found the problem. Forget everything that I have written re this problem.

In the 'Angle example' '#macro mouse(m)' there is 'Sleep 1,1' just after 'Display()'. That Sleep is not in dodicat's last code. Inserting 'Sleep 1, 1' as follows sees the drag & drop now work perfectly on my machine for both the red and green button.

<smile>

Code: Select all

#macro mouse(m)
Scope
  Dim As Long x=mx,y=my,dx2,dy2
  While btn = 1 Or btn=2
    Display() : Sleep 1,1      ' Sleep 1,1 added
...
...
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Physics question

Post by dodicat »

Thanks deltarho
I have edited the post, put the sleep directly into the display macro so it sleeps for a bit in all cases.
I was in error, bypassing sleep during a drag.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Physics question

Post by deltarho[1859] »

dodicat wrote:I was in error, bypassing sleep during a drag.
It happens to the best of us.

Graphics are not my strong point so it is probably not much coming from me but I reckon that code is one of the best that I have seen. I will make a point of studying it to better my understanding of FB's graphic's capabilities. Anybody wanting to get into graphics would do well to do the same. I would go further than that and say that it a good tutorial for FreeBASIC, you are doing things with 'Type' that I don't understand, yet. <smile>
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Physics question

Post by badidea »

Conclusion form Joshy's link:
I was thinking too complicated. I thought that I had divide the thrust force in 2 parts (for linear and angular acceleration). But this does not seem necessary.
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Physics question

Post by badidea »

deltarho[1859] wrote:
dodicat wrote:I was in error, bypassing sleep during a drag.
It happens to the best of us.

Graphics are not my strong point so it is probably not much coming from me but I reckon that code is one of the best that I have seen. I will make a point of studying it to better my understanding of FB's graphic's capabilities. Anybody wanting to get into graphics would do well to do the same. I would go further than that and say that it a good tutorial for FreeBASIC, you are doing things with 'Type' that I don't understand, yet. <smile>
I might sound like Mr Swiss now, but I would not call dodicat's 'rainy camping code' above a good tutorial. Yes, there is lots of interesting stuff going on, but the whole structure is a bit of a mess. The sleeping issue is a result of this I think. Other issues:
* mouse(n) : From the main loop at the bottom, not obvious what that does until you look at the code inside.
* display : It does way more than displaying stuff. Also 'hidden' in mouse(n).

Types are very convenient when doing vector maths.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Physics question

Post by deltarho[1859] »

badidea wrote:but the whole structure is a bit of a mess.
I think that Vincent van Gogh's paintings are a bit of a mess but some folk have parted with some serious money to get their hands on them. One thing is for sure and that is dodicat is not a John Constable. <smile>
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Physics question

Post by badidea »

I'm not camping yet, so I made a start:

Code: Select all

const as single PI = 4 * atn(1)
const as single PPM = 3 'pixels per meter
const SW = 800, SH = 600

screenres SW, SH, 32
width SW \ 16, SH \ 8

type sgl2d
	dim as single x, y
	declare constructor
	declare constructor(x as single, y as single)
	declare operator cast () as string
end type

constructor sgl2d
end constructor

constructor sgl2d(x as single, y as single)
	this.x = x : this.y = y
end constructor

operator sgl2d.cast () as string
	return str(x) & "," & str(y)
end operator

operator +(a as sgl2d, b as sgl2d) as sgl2d
	return sgl2d(a.x + b.x, a.y + b.y)
end operator

operator -(a as sgl2d, b as sgl2d) as sgl2d
	return sgl2d(a.x - b.x, a.y - b.y)
end operator

operator /(a as sgl2d, div as single) as sgl2d
	return sgl2d(a.x / div, a.y / div)
end operator

operator *(a as sgl2d, mul as single) as sgl2d
	return sgl2d(a.x * mul, a.y * mul)
end operator

function cross(a as sgl2d, b as sgl2d) as single
	return a.x * b.y - a.y * b.x
end function

sub clearScreen(c as ulong)
	line(0, 0)-(SW - 1, SH - 1), c, bf
end sub

'scaled circle using PPM, y-axis pointing up, center = 0, 0
sub drawCircle(p as sgl2d, r as single, c as ulong)
	circle(SW \ 2 + p.x * PPM, SH \ 2 - p.y * PPM), r * PPM, c
end sub

'scaled line using PPM, y-axis pointing up, center = 0, 0
sub drawLine(p1 as sgl2d, p2 as sgl2d, c as ulong)
	line(SW \ 2 + p1.x * PPM, SH \ 2 - p1.y * PPM)-_
		(SW \ 2 + p2.x * PPM, SH \ 2 - p2.y * PPM), c
	'print p1.x, p1.y
	'print p2.x, p2.y
end sub

function polarToCartesian(angle as single, radius as single) as sgl2d
	return sgl2d(cos(angle) * radius, sin(angle) * radius)
end function

function degToRad(degrees as single) as single
	return (degrees / 180) * PI
end function

type disc_object
	dim as single radius '[m]
	dim as single height '[m]
	dim as single density '[kg/m^3]
	dim as ulong colour '[m]
	'linear motion properties
	dim as sgl2d position 'position [m]
	dim as single lin_m 'mass [kg]
	dim as sgl2d lin_F 'force [N] [kg*m/s^2]
	dim as sgl2d lin_a 'acceleation [m/s^2]
	dim as sgl2d lin_v 'velocity [m/s]
	dim as sgl2d lin_p 'momemtum [kg*m/s]
	dim as single lin_E 'Kinetic energy [J] [kg*m^2/s^2]
	'Rotational motion properties
	dim as single angle 'angular position (theta) [rad]
	dim as single ang_F 'torque (tau) [N*m] [kg*m^2/s^2]
	dim as single ang_m 'angular mass, moment of inertia (I) [kg*m^2]
	dim as single ang_a 'angular velocity (alpha) [rad/s^2]
	dim as single ang_v 'angular velocity (omega) [rad/s]
	dim as single ang_p 'angular momentum (L) [kg*m^2/s]
	dim as single ang_E 'Kinetic energy [J] [kg*m^2/s^2]
	'
	declare sub init(r as single, h as single, d as single, p as sgl2d, c as ulong)
end type

'Set radius, height, density, position
'Calculate mass and rotational inertia
sub disc_object.init(r as single, h as single, d as single, p as sgl2d, c as ulong)
	radius = r
	height = h
	density = d
	position = p
	colour = c
	lin_m = PI * r ^ 2 * d
	ang_m = 0.5 * lin_m * r ^ 2
end sub

'-------------------------------------------------------------------------------

dim as disc_object disc
dim as sgl2d thrusterPos 
dim as single thrusterAngle 'for positioning thruster on disc

disc.init(10, 1, 5, sgl2d(0, -50), rgb(127, 255, 0))
disc.lin_F = sgl2d(0, 1e4) '10 kN

dim as double tNow = timer, tPrev = tNow, dt = 0
while inkey() <> chr(27)
	'calculate position and rotation
	with disc
		thrusterAngle = .angle + degToRad(-45)
		thrusterPos = disc.position + polarToCartesian(thrusterAngle, disc.radius)
		.lin_a = .lin_F / .lin_m
		.lin_v += .lin_a * dt
		.position += .lin_v * dt
		disc.ang_F = cross(polarToCartesian(thrusterAngle, disc.radius), disc.lin_F)
		.ang_a = .ang_F / .ang_m
		.ang_v += .ang_a * dt
		.angle += .ang_v * dt
	end with
	'display stuff
	clearscreen(0)
	screenlock
	locate 1,1 : print "<ESC> to exit";
	drawCircle(disc.position, disc.radius, disc.colour) 'flying saucer
	drawLine(thrusterPos, thrusterPos - disc.lin_F / 1e3, rgb(255, 127, 0)) 'thruster force indicator
	drawLine(disc.position, thrusterPos, rgb(255, 255, 0)) 'rotation indicator
	screenunlock
	'update time
	sleep 1
	tPrev = tNow
	tNow = timer
	dt = tNow - tPrev
wend
screen 0
print "End"

'links:
'http://www.hyperphysics.de/hyperphysics/hbase/mi.html
'https://www.real-world-physics-problems.com/rigid-body-dynamics.html
'https://en.wikipedia.org/wiki/Resultant_force
'https://research.ncl.ac.uk/game/mastersdegree/gametechnologies/physicstutorials/5collisionresponse/
'https://www.toptal.com/game/video-game-physics-part-i-an-introduction-to-rigid-body-dynamics
Orange line is the thruster action, not rotating with the space ship yet.
Last edited by badidea on Jul 20, 2019 21:56, edited 3 times in total.
Post Reply