Author Topic: physics  (Read 12796 times)

Guilect

  • Guest
physics
« on: July 19, 2013, 12:22:17 AM »


Try it online by clicking here

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

You are the blue square.
« Last Edit: August 30, 2013, 12:44:11 PM by Guilect »

SteveOW

  • Guest
Re: preview of physics example coming in v0.6
« Reply #1 on: July 19, 2013, 06:28:25 AM »
Awesome! 8) 8) 8)

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: preview of physics example coming in v0.6
« Reply #2 on: July 20, 2013, 08:40:40 AM »
Cool! Is it possible to see the Basic source?

Rick3137

  • Guest
Re: preview of physics example coming in v0.6
« Reply #3 on: July 20, 2013, 11:27:02 AM »
 Looks good.

Guilect

  • Guest
Re: preview of physics example coming in v0.6
« Reply #4 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

bolbo

  • Guest
Re: preview of physics example coming in v0.6
« Reply #5 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

kevin

  • Guest
Re: preview of physics example coming in v0.6
« Reply #6 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?

Guilect

  • Guest
Re: preview of physics example coming in v0.6
« Reply #7 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.

bolbo

  • Guest
Re: preview of physics example coming in v0.6
« Reply #8 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.

Guilect

  • Guest
Re: preview of physics example coming in v0.6
« Reply #9 on: July 25, 2013, 12:08:45 AM »
Physics is optional.

bolbo

  • Guest
Re: preview of physics example coming in v0.6
« Reply #10 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.

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: preview of physics example coming in v0.6
« Reply #11 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.

bolbo

  • Guest
Re: preview of physics example coming in v0.6
« Reply #12 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/

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: preview of physics example coming in v0.6
« Reply #13 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.

SteveOW

  • Guest
Re: preview of physics example coming in v0.6
« Reply #14 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.