Main Menu

elastical vehicle movements

Started by stef, December 26, 2005, 11:33:52 AM

Previous topic - Next topic

stef

Hi!

This program I made for "cool gliding".
It is an example of movement-management.
The base of this program are examples in other languages.

The demo-examples are for showing some effects.
If you use the code you must change the parameters for best result.

Greetings
stef

PlayBASIC Code: [Select]
; PROJECT : elastical vehicle movement
; AUTHOR : stef
; CREATED : 20.11.2005
; EDITED : 26.12.2005
; ---------------------------------------------------------------------

;screen_bounce# must be >1.0 !!!!!!!!!!!!!

screenw = 800
screenh = 600
OpenScreen screenw,screenh,16,2


;precalc of sine and cosine - 'lookup table'
Dim sin_angle#(360)
Dim cos_angle#(360)
For ang = 0 To 360
sin_angle#(ang) = Sin(ang)
cos_angle#(ang) = Cos(ang)
Next

;generating the vehicle and starfield-background
Gosub prepare_stardraw
Gosub prepare_vehicledraw

total_vehicles=7
vehicle_number=0


;-------------------------------------------------loopstart
Do
RenderToScreen
Cls 0

Gosub checkinput
Gosub calculate_move_vehicle


TileImage stars1,-screen_posx#,-screen_posy#,1

RotateSprite veh1sp,vehicle_turnangle#
PositionSprite veh1sp,x#,y#
DrawSprite veh1sp

Print "Use arrow-keys for steering; use f1 - f5 to select vehicle"
Print "FPS: "+Str$(FPS())
Print vehicle$

Sync
Loop
;-------------------------------------------------loopend
;-------------------------------------------------checkinput
Checkinput:
If UpKey()=True
vehicle_dx#=vehicle_dx#+Cos_angle#(vehicle_turnangle#)*vehicle_xacceleration#
vehicle_dy#=vehicle_dy#+Sin_angle#(vehicle_turnangle#)*vehicle_yacceleration#

EndIf

If RightKey()=True
vehicle_turnspeed#=vehicle_turnspeed#+vehicle_turnacceleration#
If vehicle_turnspeed#>vehicle_turnmaxspeed# Then vehicle_turnspeed#=vehicle_turnmaxspeed#
EndIf
If LeftKey()=True
vehicle_turnspeed#=vehicle_turnspeed#-vehicle_turnacceleration#
If vehicle_turnspeed#< -vehicle_turnmaxspeed# Then vehicle_turnspeed#= -vehicle_turnmaxspeed#
EndIf

If FunctionKeys(1)=True Then Gosub v1
If FunctionKeys(2)=True Then Gosub v2
If FunctionKeys(3)=True Then Gosub v3
If FunctionKeys(4)=True Then Gosub v4
If FunctionKeys(5)=True Then Gosub v5



Return

;------------------------------------------------calculate_move_vehicle
calculate_move_vehicle:

vehicle_x#=vehicle_x#+vehicle_dx#
vehicle_y#=vehicle_y#+vehicle_dy#

vehicle_y#=vehicle_y#+vehicle_gravity#; add gravity

speed#=Sqrt(vehicle_dx#*vehicle_dx#+vehicle_dy#*vehicle_dy#);'diagonal' speed

If speed#>vehicle_maxspeed# Then speed#=vehicle_maxspeed#;speedlimit
speed#=speed#-vehicle_friction#;friction applied
If speed#<0 Then speed#=0


vehicle_dx#=speed#*cos_angle#(vehicle_turnangle#)
vehicle_dy#=speed#*sin_angle#(vehicle_turnangle#)

vehicle_turnangle# =WrapAngle(vehicle_turnangle#,vehicle_turnspeed#)

If vehicle_turnspeed#>0
vehicle_turnspeed#=vehicle_turnspeed#- vehicle_turnfriction#
If vehicle_turnspeed#<0 Then vehicle_turnspeed#=0

Else
vehicle_turnspeed#=vehicle_turnspeed#+ vehicle_turnfriction#
If vehicle_turnspeed#>0 Then vehicle_turnspeed#=0
EndIf

screen_dx#=(screen_dx#+(vehicle_x#-screen_offsetx#-screen_posx#+screen_runaheadx#*vehicle_dx#)*screen_move#)/screen_bounce#
screen_dy#=(screen_dy#+(vehicle_y#-screen_offsety#-screen_posy#+screen_runaheady#*vehicle_dy#)*screen_move#)/screen_bounce#

screen_posx#=screen_posx#+screen_dx#
screen_posy#=screen_posy#+screen_dy#

x#=vehicle_x#-screen_posx#+screenw/2
y#=vehicle_y#-screen_posy#+screenh/2

Return

v1:
vehicle$= "small fast spaceship"
;Defining of position and movement of the vehicle
vehicle_x#=screenw/2;x position
vehicle_y#=screenh/2;y position
vehicle_dx#=0;x speed
vehicle_dy#=0;y speed
vehicle_gravity#=0.0
vehicle_maxspeed#=12
vehicle_friction#=0.06;friction applied to speed
vehicle_xacceleration#=0.5;x acceleration, how fast boosting
vehicle_yacceleration#=0.5;y acceleration, up- and downwards boosting
vehicle_turnacceleration#=0.3;acceleration when turning
vehicle_turnfriction#=0.2;friction applied when turning
vehicle_turnmaxspeed#=5;friction applied when turning
vehicle_turnspeed#=0;current speed in turning
vehicle_turnangle#=0;current angle of vehicle

;defining of offset and moving of the screen in relation to the vehicle
screen_move#=0.12;Elasticity of screen (together with screen_bounce#)
screen_bounce#=1.58;Elasticity of screen
screen_dx#=0;screenspeed in x direction
screen_dy#=0;screenspeed in y direction
screen_offsetx#=0;x-offset of vehicle (from screencenter)
screen_offsety#=0;y-offset of vehicle
screen_runaheadx#=0;change position of vehicle to screen in combination with speed
screen_runaheady#=0
screen_posx#=1000;position of screenmiddle
screen_posy#=1000
Return
Login required to view complete source code



kevin


medwayman

That's hyper fast!  :o

Nice work

:)

stef

Hi!

Yes it's really fast. (It is of course too fast now. You better use 'setfps 100' for better looking.) But I let it that, to show that you have enough fps-reserves.

I am always astonished how fast PB is. In particular the rotation.In Blitzbasic it is necessary to pre-calculate and to pre-render rotated images/sprite. Nevertheless PB is faster ( with realtime rotation!).

Greetings
stef

medwayman

Yes I limited it to 150fps (was getting 950fps!) when I tried it and spent some time zooming around the cosmos. I know it's only a test but I still enjoyed looping around the stars :)

Digital Awakening

#5
Neat, I got 1300 FPS on my laptop :)

When I locked it to 100FPS the heli camera became really jumpy when it started and stopped.

Next up you should add some sliding for the car and the spaceships need some free space movement.
Wisit my site at: DigitalAwakening.net

stef

#6
Hi!

QuoteNeat, I got 1300 FPS on my laptop

1300 FPS is fast (sounds already dangerously). Hope you got no motion sickness :)

What laptop do you use?

Greetings
stef

Digital Awakening

It's a Fujitsu-Siemens Amilo M1425. It's about a year old with a Centrino CPU and a Mobility R9700. I love the widescreen, loads of space for code :)
Wisit my site at: DigitalAwakening.net