Author Topic: physics  (Read 3211 times)

Guilect

  • Moderator
  • Full Member
  • *****
  • Posts: 110
    • View Profile
physics
« on: 19. July 2013, 02:22:17 »


Try it online by clicking here

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

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

SteveOW

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Re: preview of physics example coming in v0.6
« Reply #1 on: 19. July 2013, 08:28:25 »
Awesome! 8) 8) 8)

Cybermonkey

  • Administrator
  • Hero Member
  • *****
  • Posts: 574
    • View Profile
    • Home of EGSL
Re: preview of physics example coming in v0.6
« Reply #2 on: 20. July 2013, 10:40:40 »
Cool! Is it possible to see the Basic source?
Best regards,
Cybermonkey

Rick3137

  • Full Member
  • ***
  • Posts: 108
    • View Profile
    • Rick's Programs
Re: preview of physics example coming in v0.6
« Reply #3 on: 20. July 2013, 13:27:02 »
 Looks good.
http://rb23.yolasite.com  Ricks Programs
http://rb27.synthasite.com   Sight and Sound

Guilect

  • Moderator
  • Full Member
  • *****
  • Posts: 110
    • View Profile
Re: preview of physics example coming in v0.6
« Reply #4 on: 20. July 2013, 14:21:22 »
@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

  • Newcomer
  • *
  • Posts: 25
    • View Profile
Re: preview of physics example coming in v0.6
« Reply #5 on: 24. July 2013, 03:11:25 »
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

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Re: preview of physics example coming in v0.6
« Reply #6 on: 24. July 2013, 08:29:01 »
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

  • Moderator
  • Full Member
  • *****
  • Posts: 110
    • View Profile
Re: preview of physics example coming in v0.6
« Reply #7 on: 24. July 2013, 14:13:38 »
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

  • Newcomer
  • *
  • Posts: 25
    • View Profile
Re: preview of physics example coming in v0.6
« Reply #8 on: 24. July 2013, 21:14:51 »
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

  • Moderator
  • Full Member
  • *****
  • Posts: 110
    • View Profile
Re: preview of physics example coming in v0.6
« Reply #9 on: 25. July 2013, 02:08:45 »
Physics is optional.

bolbo

  • Newcomer
  • *
  • Posts: 25
    • View Profile
Re: preview of physics example coming in v0.6
« Reply #10 on: 25. July 2013, 03:07:51 »
I was referring to the physics engine, to allow less computations in certain scenarios depending on hardware needs and game simplicity.

Cybermonkey

  • Administrator
  • Hero Member
  • *****
  • Posts: 574
    • View Profile
    • Home of EGSL
Re: preview of physics example coming in v0.6
« Reply #11 on: 25. July 2013, 08:56:10 »
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.
Best regards,
Cybermonkey

bolbo

  • Newcomer
  • *
  • Posts: 25
    • View Profile
Re: preview of physics example coming in v0.6
« Reply #12 on: 25. July 2013, 12:07:14 »
This javascript physics engine runs nicely on my crappy laptop
http://soulwire.co.uk/experiments/coffee-physics/

Cybermonkey

  • Administrator
  • Hero Member
  • *****
  • Posts: 574
    • View Profile
    • Home of EGSL
Re: preview of physics example coming in v0.6
« Reply #13 on: 25. July 2013, 13:45:22 »
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.
Best regards,
Cybermonkey

SteveOW

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Re: preview of physics example coming in v0.6
« Reply #14 on: 25. July 2013, 16:02:22 »
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.