Retrogamecoding(.org) > BrowserBasic

physics

(1/4) > >>

Guilect:


Try it online by clicking here

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

You are the blue square.

SteveOW:
Awesome! 8) 8) 8)

Cybermonkey:
Cool! Is it possible to see the Basic source?

Rick3137:
 Looks good.

Guilect:
@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: ---#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
--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version