Think i'll do a quick little 2d racer for ya. most of the code i already have written for a 3d boat racer.
It's a start, not much really.
sync on
sync rate 60
backdrop on
color backdrop 0
temp = 2
wallN = 1
null = make vector2(wallN)
null = make vector2(temp)
impact_strength# = 0.7
braking# = 1.1
null = make vector2(3)
type player
ang as float
acc as float
topspeed as float
x as float
z as float
fx as float
fz as float
px as float
pz as float
oldpx as float
oldpz as float
endtype
player1 as player
player1.ang = 0
player1.acc = 0.05
player1.topspeed = 5
load image "cannonfull.bmp", 1
sprite 1, 0, 0, 1
offset sprite 1, sprite width(1)/2, sprite height(1)/2
scale sprite 1, 50
point_count = 20
dim points(20,2)
restore map
for t=1 to 20
read z
points(t,1) = z
read z
points(t,2) = z
next t
player1.px = 300
player1.pz = 300
player1.oldpx = 300
player1.oldpz = 300
DO
gosub _controls
gosub _collision
gosub _apply_forces
gosub _position_player
sync
LOOP
_controls:
rem apply player's force to current movement force
if upkey()=1
player1.x = player1.x + player1.fx
player1.z = player1.z + player1.fz
endif
if downkey()=1
player1.x = player1.x / braking#
player1.z = player1.z / braking#
endif
if leftkey()=1
player1.ang = wrapvalue(player1.ang - 2)
endif
if rightkey()=1
player1.ang = wrapvalue(player1.ang + 2)
endif
RETURN
REM If player hits wall, calculate new angle of movement, and
REM new speed amount
_apply_forces:
rem player direction
dx# = cos(wrapvalue(player1.ang-90))
dz# = sin(wrapvalue(player1.ang-90))
rem player's force
player1.fx = dx# * player1.acc
player1.fz = dz# * player1.acc
rem player's speed
player_speed = sqrt(player1.x*player1.x + player1.z*player1.z)
rem if a wall has been hit:
if wall_hit = 1
player1.px = player1.oldpx
player1.pz = player1.oldpz
wall_hit = 0
rem wall's normal
vx# = x vector2(wallN)
vz# = y vector2(wallN)
rem reflect player's movement
`player1.x = player1.x + vx#*player_speed*impact_strength#
`player1.z = player1.z + vz#*player_speed*impact_strength#
set vector2 temp, player1.x,player1.z
player1.x = (player1.x - (2*vx#*dot product vector2(wallN,temp)))*impact_strength#
player1.z = (player1.z - (2*vz#*dot product vector2(wallN,temp)))*impact_strength#
`player1.x = player1.x*1.5
`player1.z = player1.z*1.5
endif
rem restrict player from moving too fast
if player_speed > player1.topspeed
player1.x = (player1.x * player1.topspeed) / player_speed
player1.z = (player1.z * player1.topspeed) / player_speed
endif
RETURN
REM update player's position
_position_player:
player1.oldpx = player1.px
player1.oldpz = player1.pz
player1.px = player1.px + player1.x
player1.pz = player1.pz + player1.z
sprite 1,player1.px,player1.pz,1
rotate sprite 1, player1.ang
RETURN
_collision:
rem outside wall
for t = 1 to point_count
t2 = t+1
if t = point_count then t2 = 1
line points(t,1),points(t,2),points(t2,1),points(t2,2)
length# = sqrt((points(t2,1)-points(t,1))^2 + (points(t2,2)-points(t,2))^2)
r# = ((points(t,2)-player1.pz)*(points(t,2)-points(t2,2)) - (points(t,1)-player1.px)*(points(t2,1)-points(t,1))) / (length#^2)
ix# = points(t,1) + r#*(points(t2,1)-points(t,1))
iy# = points(t,2) + r#*(points(t2,2)-points(t,2))
dist# = (ix#-player1.px)^2 + (iy#-player1.pz)^2
`if r# >= 0.0 AND r# <= 1.0 AND dist# <= player1.topspeed^2
if r# >= 0.0 AND r# <= 1.0 AND dist# <= 49
if point_line(player1.px,player1.pz,points(t,1),points(t,2),points(t2,1),points(t2,2)) >= 0
set vector2 wallN,points(t2,2)-points(t,2),(points(t2,1)-points(t,1))*-1
normalize vector2 wallN,wallN
wall_hit = 1
endif
endif
next t
RETURN
REM if point(px,py) is right or left of line segment (x1,y1),(x2,y2)
REM return negative if left, positive if right, 0 if on
function point_line(px#,py#, x1#,y1#,x2#,y2#)
dp# = (x2# - x1#) * (py# - y1#) - (px# - x1#) * (y2# - y1#)
endfunction dp#
map:
data 50, 0
data 50, 100
data 0, 125
data 0, 200
data 100, 275
data 150, 250
data 220, 250
data 220, 280
data 250, 300
data 300, 300
data 400, 200
data 300, 130
data 300, 90
data 330, 70
data 345, 60
data 335, 50
data 300, 40
data 250, 50
data 150, 50
data 100, 0
"eureka" - Archimedes