Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Simple 2d car physics, looking for examples please!

Author
Message
b83
12
Years of Service
User Offline
Joined: 30th Nov 2011
Location:
Posted: 8th May 2016 13:36
hey guys, I tried converting some 2d car physics code from purebasic to AppGameKit tier 1 but don't really know what I'm doing... if anyone has any simple 2d car physics code I can build on I would appreciate some feedback, thanks a ton!


// Project: car2d
// Created: 2016-05-08

// set window properties
SetWindowTitle( "car2d" )
SetWindowSize( 1024, 768, 0 )

// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )

function min(a, d)
If a < d
//ProcedureReturn a
Else
//ProcedureReturn d
EndIf
endfunction

function max(a as float, d as float)
If a > d
//ProcedureReturn a
Else
//ProcedureReturn d
EndIf
Endfunction

function Sign1(a as float)
If a < 0
//ProcedureReturn -1.0
Else
//ProcedureReturn 1.0
EndIf
Endfunction


function adjust(v as float,threshold as float,maximum as float)
vx = 0
If (v <= threshold)
vx=(v/threshold)*1.0
Else
vx = 1
EndIf
//ProcedureReturn vx
Endfunction

false = 0

accel as float = 0.2 // acceleration due To gas (up arrow)
steer as float = 0.08 // steering tightness
steer_normal as float = 0.08 // Default Steering
steer_handbrake as float = 0.099 // Incremental increases For handbrakeys
steer_threshold as float = 4 // Speed where steering at its most manouvreable
MINspd as float = -5 // max reverse speed
MAXspd as float = 22 // max speed on straightaway
MAXskiddisplay as float = 2 // Display skids on slides till this value reached
MAXturnSpd as float = 10 // maximum speed While turning
MaxTurnSkid as float = 18 // Maximum speed in turn before skidding
deccel as float = 0.92 // decelleration due To brakes (down arrow)
handbrake as float = 0.93 // decelleration due To brakes (down arrow)
handbrake_traction as float = 20 // Amount of slide For handbrake higher is more
drift as float = 0.98 // deceleration after letting off gas

speed as float = 0 // duuuh!
surface as float = 3 // Tar = 10 gravel = 1 ice = 0.5
traction as float = 5 // the higher the more slippery init value
traction_max as float = 1 // Max Traction
Dim vector[1] // vector of the direction of the car
bounceBack as float = 0.6 // percent of speed when bounced back from hitting an obstacle

angle as float

collide = false // Used To make sure car doesnt get stuck in collision checks

LoadImage(1,"car.png")
CreateSprite(1,1)

do
rotation = angle * 180 / 3.1415


UP = GetRawKeyPressed(38)
DOWN = GetRawKeyPressed(40)
HAND = GetRawKeyPressed(17)
LEFT = GetRawKeyPressed(37)
RIGHT = GetRawKeyPressed(39)

UD = UP - DOWN
LR = RIGHT - LEFT

If LR
angle + LR*steer*adjust(Abs(speed),steer_threshold, MAXSpd)
EndIf


If UD = false
speed * drift
ElseIf UD <> false And HAND = false
speed = Min(Max(speed+UD*accel,MINspd),MAXspd)
EndIf

If (Abs(speed) < accel/2)
speed = 0
EndIf

If HAND
speed * handbrake
If (speed > MAXskiddisplay)
steer = steer_handbrake
EndIf
traction = handbrake_traction / surface
Else
traction = Max(traction_max, Abs((speed-MAXturnSpd) / surface))
steer = steer_normal
EndIf

If speed < 0
traction = 1.0
EndIf

xv as float = (Cos(angle)) * speed - vector(0) / traction
yv as float = (Sin(angle)) * speed) - vector(1) / traction

vector(0) + xv
vector(1) + yv

track_x + vector(0)
track_y + vector(1)

Print( ScreenFPS() )
Sync()
loop
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 9th May 2016 10:11
hi, i edited your source

try using "type" = a struct for the car values
type TCar
a as float
b as float
endtype
global Car as TCar
Car.a=0.0


AGK (Steam) V2.0.18 : Windows 10 Pro 64 Bit : AMD (15.30.1025) Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 9th May 2016 11:39
Here's one I made a while back: https://forum.thegamecreators.com/thread/197957
Using AppGameKit V2 Tier 1
b83
12
Years of Service
User Offline
Joined: 30th Nov 2011
Location:
Posted: 9th May 2016 12:24
thanks Baxslash and Markus!

Login to post a reply

Server time is: 2024-11-25 08:37:40
Your offset time is: 2024-11-25 08:37:40