RetroBASIC

Retrogamecoding(.org) => BrowserBasic => Topic started by: Guilect on July 19, 2013, 12:22:17 am

Title: physics
Post by: Guilect on July 19, 2013, 12:22:17 am
(http://www.pewtersoftware.com/browserbasic/examples/physics/ss.png)

Try it online by clicking here (http://www.pewtersoftware.com/browserbasic/examples/physics/test_physics.html)

Left and right arrow keys to move,
spacebar to jump.

You are the blue square.
Title: Re: preview of physics example coming in v0.6
Post by: SteveOW on July 19, 2013, 06:28:25 am
Awesome! 8) 8) 8)
Title: Re: preview of physics example coming in v0.6
Post by: Cybermonkey on July 20, 2013, 08:40:40 am
Cool! Is it possible to see the Basic source?
Title: Re: preview of physics example coming in v0.6
Post by: Rick3137 on July 20, 2013, 11:27:02 am
 Looks good.
Title: Re: preview of physics example coming in v0.6
Post by: Guilect on July 20, 2013, 12:21:22 pm
@cybermonkey,

Here is the BASIC source code.
I have not had time yet to add enough comments, so hopefully it is clear enough with the just the source code.
Code: [Select]
#uses "physics"

setBackgroundColor(128,128,128)

var notJumping as number
notJumping = true

Type TPoint
var x as number
var y as number
EndType

Type TPlayer
var name as string
var x as number
var y as number
var height as number
var width as number
var fixedRotation as number
var friction as number
var restitution as number
var color as string
var maxVelocityX as number
EndType

var player as TPlayer

        player.name = "player"
        player.x = .5
        player.y = 12
        player.height = .4
        player.width = .4
        player.fixedRotation = true
        player.friction = .3
        player.restitution = 0
        player.color = "blue"
        player.maxVelocityX = 4
   
var player_entity as number
player_entity = CreateEntity(player)


    'Car thing
    Type wheelTemplate
        var name as string
        var shape as string
        var radius as number
        var image as string
        var imageStretchToFit as number
        var x as number
        var y as number
    EndType

var wheel1 as wheelTemplate
wheel1.name = "wheel"
wheel1.shape = "circle"
wheel1.radius = 1
wheel1.image = "img/wheel.png"
wheel1.imageStretchToFit = true
wheel1.x = 1
wheel1.y = 1

var wheel2 as wheelTemplate
wheel2.name = "wheel"
wheel2.shape = "circle"
wheel2.radius = 1
wheel2.image = "img/wheel.png"
wheel2.imageStretchToFit = true
wheel2.x = 4
wheel2.y = 1

var wheel1entity as number
wheel1entity = CreateEntity(wheel1)

var wheel2entity as number
wheel2entity = CreateEntity(wheel2)


Type TJointOptions
    var type as string
var allowCollisions as number
endtype

var wheeljointoptions as TJointOptions

wheeljointoptions.type = "distance"
        wheeljointoptions.allowCollisions = false

createJoint(wheel1entity, wheel2entity,wheeljointoptions)

' the ground
    Type groundTemplate
        var name as string
        var type as string
        var height as number
        var color as string
        var borderColor as string
        var borderWidth as number
var width as number
var x as number
var y as number
    EndType

var ground as groundTemplate
ground.name = "ground"
ground.type = "static"
ground.height = .2
ground.color = "green"
ground.borderColor = "rgba(0, 100, 0, .5)"
ground.borderWidth = 3
ground.width = 20
ground.x = 10
ground.y = 13.22
CreateEntity(ground)

var ground1 as groundTemplate
ground1.name = "platform1"
ground1.type = "static"
ground1.height = .2
ground1.color = "green"
ground1.borderColor = "rgba(0, 100, 0, .5)"
ground1.borderWidth = 3
ground1.width = 6
ground1.x = 3
ground1.y = 5
    CreateEntity(ground1)

var ground2 as groundTemplate
ground2.name = "platform2"
ground2.type = "static"
ground2.height = .2
ground2.color = "green"
ground2.borderColor = "rgba(0, 100, 0, .5)"
ground2.borderWidth = 3
ground2.width = 8
ground2.x = 16
ground2.y = 5
    CreateEntity(ground2)

    Type TSquare
var name as string
var x as number
var y as number
var height as number
var width as number
var imageOffsetY as number
EndType

var square as TSquare
        square.name = "square"
        square.x = 13
        square.y = 8
        square.height = 1.6
        square.width = .4
        square.imageOffsetY = -.2
CreateEntity(square)

Type TCircle
var name as string
var shape as string
var radius as number
var x as number
var y as number
var density as number
var image as string
var imageStretchToFit as number
EndType

var circle as TCircle
        circle.name = "circle"
        circle.shape = "circle"
        circle.radius = 2
        circle.x = 14
        circle.y = 3
        circle.density = .5
        circle.image = "img/wheel.png"
        circle.imageStretchToFit = true
    CreateEntity(circle)

   
Type TPoly
var name as string
var shape as string
var x as number
var y as number
EndType

var poly as TPoly
poly.name = "poly"
poly.shape = "polygon"
poly.x = 5
poly.y = 8
CreateEntity(poly)     


Type TPlatform
var name as string
var fixedRotation as number
var height as number
var width as number
EndType

var platform as TPlatform
        platform.name = "platform"
        platform.fixedRotation = true
        platform.height = .2
        platform.width = 2
var platform_entity as number
    platform_entity = CreateEntity(platform)


Type Empty
endtype

Type coinTemplate
var name as string
var shape as string
var radius as number
var color as string
var onStartContact as number
var x as number
var y as number
var width as number
var height as number
endtype

    var coin as coinTemplate
        coin.name = "coin"
        coin.shape = "circle"
        coin.radius = .1
        coin.color = "yellow"
coin.x = 2
coin.y = 4
CreateEntity(coin)

    var coin2 as coinTemplate
        coin2.name = "coin"
        coin2.shape = "circle"
        coin2.radius = .1
        coin2.color = "yellow"
coin2.x = 2
coin2.y = 12
CreateEntity(coin2)

var coin3 as coinTemplate
coin3.x = 16
coin3.y = 4
coin3.color = "yellow"
coin3 width = .2
coin3.height = .6
CreateEntity(coin3)

function OnKeyPressed(key as string, code as number)

If code = 32 AND notJumping then
notJumping = false
ApplyImpulse(player_entity, 2)
endif

' when airborn movement is restricted
var force as number
force = 8
If notJumping = false then
force = 1
endif

If key = "right" Then
setForce(player_entity, "movement" , force, 90)
setFriction(player_entity,0.1)
elseif key = "left" then
setForce(player_entity, "movement" , force, 270)
setFriction(player_entity,0.1)
endif

endfunction

Function OnKeyReleased(key as string, code as number)
        if key = "left" OR key = "right" then
            ClearForce(player_entity,"movement")
            setFriction(player_entity,3)
EndIf
notJumping = true
endfunction

Function OnUpdate()
var p1 as TPoint
p1 = getCanvasPosition(platform_entity)
If p1.y < 100 then
setVelocity(platform_entity, "moving platform", 5, 180)
endif
If p1.y > 380 then
setVelocity(platform_entity, "moving platform", 5, 0)
EndIF
EndFunction

function OnDraw()
var p1 as TPoint
var p2 as TPoint
p1 = getCanvasPosition(wheel1entity)
p2 = getCanvasPosition(wheel2entity)
Line(p1.x,p1.y,p2.x,p2.y)
Print(getFPS(), 10,10)
endfunction
Title: Re: preview of physics example coming in v0.6
Post by: bolbo on July 24, 2013, 01:11:25 am
Hi
I've tested the sample on my 1gb ram, 2ghz laptop,
it does not work on opera, on firefox and chrome performace is poor and irregular: from 6 to 27 fps
Title: Re: preview of physics example coming in v0.6
Post by: kevin on July 24, 2013, 06:29:01 am
I really like this example - very powerful. However, I have similar performance issues  to @bolbo on an older laptop. In my case it is 1.8 ghz cpu and 1 gb ram......On Chrome 28  I get 10 - 20 fps, which is watchable, and Opera 12.02 will start  at that rate for a second or two then give a blank screen - reloading starts it for a second or two again. Best results for me are on Maxthon 3.4.5 where it is smooth and peaks at 30's or sometimes 40 fps - not surprising as it is probably the lightest of my browsers and everything seems to work quicker on it. I know I should run a newer laptop but this one will normally cope with most things I need, and using Maxthon BrowserBasic seems to run well enough for me. Hope the feedback helps?
Title: Re: preview of physics example coming in v0.6
Post by: Guilect on July 24, 2013, 12:13:38 pm
Hi bolbo and kevin,

Thanks for the performance feedback.

As you probably know, compatibility between browsers is an issue.
What works on one, does not on another, or it works poorly.

Also, as you have seen, running math intensive programs like a physics simulation in a browser requires both a reasonably fast machine and a fast video card.  My last PC was a 1.5 GHZ single core with an older video card.  It could not run any browser physics sims at better than 2 fps.
My new PC, which I bought second hand, is a dual core 2.66 GHz with a nice video card.  The video card by itself has 2 Gig of ram.  Now I can run anything and everything as smooth as melted butter.

Making a physics sim for a mobile device is even tougher as they usually have lower specs than desktops or laptops.

All I can say is that having physics available is an option for those who want to play with it.
Title: Re: preview of physics example coming in v0.6
Post by: bolbo on July 24, 2013, 07:14:51 pm
One possible approach, to fit different device specs and type of games, could be let the coder to disable or simplify the behavior of certain modules of the engine.
Title: Re: preview of physics example coming in v0.6
Post by: Guilect on July 25, 2013, 12:08:45 am
Physics is optional.
Title: Re: preview of physics example coming in v0.6
Post by: bolbo on July 25, 2013, 01:07:51 am
I was referring to the physics engine, to allow less computations in certain scenarios depending on hardware needs and game simplicity.
Title: Re: preview of physics example coming in v0.6
Post by: Cybermonkey on July 25, 2013, 06:56:10 am
I was referring to the physics engine, to allow less computations in certain scenarios depending on hardware needs and game simplicity.
I would guess the physics engine is Box2D, so I don't think Guilect can change that easily.
Title: Re: preview of physics example coming in v0.6
Post by: bolbo on July 25, 2013, 10:07:14 am
This javascript physics engine runs nicely on my crappy laptop
http://soulwire.co.uk/experiments/coffee-physics/
Title: Re: preview of physics example coming in v0.6
Post by: Cybermonkey on July 25, 2013, 11:45:22 am
This looks more like CoffeeScript (http://en.wikipedia.org/wiki/CoffeeScript) than JavaScript to me and it doesn't work at all here neither on Firefox nor on Chrome.
Title: Re: preview of physics example coming in v0.6
Post by: SteveOW on July 25, 2013, 02:02:22 pm
With both WebGL and Canvas this works well on my Firefox22/Windows7-32bit/3Gb ram/2.2GHz/i3 laptop.

Core program seems to be a javascript script.
Title: Re: preview of physics example coming in v0.6
Post by: bolbo on July 25, 2013, 02:43:29 pm
I had only tested the Coffephysics samples on Opera, now I've tried them on other browsers.
Opera: it works very well ; Chrome: It works but performs worse ; Firefox : seems to work only with canvas. I had not tested them on Safari or ios because I don't own any portable device.
As for coffescript seems to be just a number of preprocessors that compile directly to javascript.
There you have more information about the engine, an interview with the developer and a review of the engine on several browsers.
http://badassjs.com/post/18503583619/coffeephysics-a-fast-new-physics-engine-written-in
http://blog.duncanhall.net/2012/03/painting-pixels-with-easeljs-and-coffee-physics/

I have read that most of the current physics engines for javascript are ports from other languages more focused on features than on performance. My only point it was to remark that performance may be not only a matter of hardware requirements but also compatibility with browsers.   
Title: Re: preview of physics example coming in v0.6
Post by: Cybermonkey on July 25, 2013, 03:26:42 pm
WebGL is enabled on my Firefox on Windows here, all I get is a grey image.
Guilects phyiscs example works fluently on my old Core2Duo with 60 fps. This should work even better on a i3?

EDIT: All other examples on that webpage are working, though. Don't know why the physics example does not work on any of my computers ...
Title: Re: preview of physics example coming in v0.6
Post by: bolbo on July 26, 2013, 04:08:36 pm
@Cybermonkey
perhaps your problem is related with the user interface of that page, could you try the same samples located on the githup server:
http://soulwire.github.io/Coffee-Physics/

By the way, coud anyone check if Guilect's sample works on mobile devices ?
Title: Re: preview of physics example coming in v0.6
Post by: SteveOW on August 30, 2013, 12:05:07 pm
@Guilect
Maybe rename this thread to "BB Physics"  i.e. drop the "Preview" ?

I am getting pretty good 35 to 55 fps on the physics example on my Tablet when using Firefox browser.  Chrome and Samsung not so good.

But the app is designed for key control, not for touch, so I cant interact with it on the tablet.




Title: Re: physics
Post by: Guilect on August 30, 2013, 12:45:27 pm
@SteveOW
That is a good frame rate from a tablet.
Glad to hear it.