2D Car Psichyc
Übersicht

HUNSharpBetreff: 2D Car Psichyc |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
Hi all. I try write a 2D network race game. But i have problem with psichic and moving of the car.
Here's the sourcecode. [There is The ProbeImage (Probe.jpg), in the default Blitz directory. It's a litle blue car picture. ] Code: [AUSKLAPPEN] Strict Graphics 800,600,0,70'FPS = 100 Global GWIDTH = GraphicsWidth(), GHeight = GraphicsHeight() 'physics variables Global delta_t# = 0.01 Global M_PI# = 3.1415926 Global DRAG# =5.0 Global RESISTANCE# =30.0 Global CA_R# = -5.20 Global CA_F# = -5.0 Global MAX_GRIP# = 2.0 Global MULT# = 57 Global MULT2# = 0.01745 Global CAR1_cartype = 1 Global CAR1_cartype_b# = 1.0 Global CAR1_cartype_c# = 1.0 Global CAR1_cartype_wheelbase# = CAR1_cartype_b# + CAR1_cartype_c# Global CAR1_cartype_h# = 1.0 Global CAR1_cartype_mass# = 1500 Global CAR1_cartype_inertia# = 1500 Global CAR1_cartype_width# = 1.5 Global CAR1_cartype_length# = 3.0 Global CAR1_cartype_wheellength# = 0.7 Global CAR1_cartype_wheelwidth# = 0.3 Global CAR1_car_position_wc_x# = 400.0 Global CAR1_car_position_wc_y# = 300.0 Global CAR1_car_velocity_wc_x# = 0 Global CAR1_car_velocity_wc_y# = 0 Global CAR1_car_angle# = 0 Global CAR1_car_angularvelocity# = 0 Global CAR1_car_steerangle# = 0 Global CAR1_car_throttle# = 0 Global CAR1_car_brake# = 0 Global front_slip = 0 Global rear_slip = 0 Global velocity_x# Global velocity_y# Global yawspeed# Global sn#, cs# Global x#, y# Global rot_angle# Global sideslip# Global slipanglefront# Global slipanglerear# Global flatf_x#, flatf_y# Global flatr_x#, flatr_y# Global weight# Global ftraction_x#, ftraction_y# Global resistance_x#, resistance_y# Global force_x#, force_y# Global torque# Global acceleration_x#, acceleration_y#, angular_acceleration# Global acceleration_wc_x#, acceleration_wc_y# Global a:Int Global renda1# Global renda2# Global renda3# Global renda4# Global rendx1# Global rendx2# Global rendx3# Global rendx4# Global rendy1# Global rendy2# Global rendy3# Global rendy4# Global ProbeImage:TImage = LoadImage("Probe.png") MidHandleImage(ProbeImage) Global speed Probe.CreateHuman() While Not KeyDown(Key_Escape) DrawText "FPS: "+FPS.Calc%(),10,580 If KeyHit( Key_N ) Then For a=1 To 20 Probe.CreateAI() Next EndIf Probe.UpdateALL() Flip;Cls Wend End 'A Thing Going around Type Probe Global List:TList Global LocalCount Field Name$,Local_ID,Net_ID Field X#,Y# Field Dir Field XSpeed#,YSpeed# Field TopSpeed,Speed# Field Acceleration# = 0.1 Field TurnSpeed Field Energy,Armor Field Control' HUMAN,AI or REMOTE Field UpdateAI,Mode Field Key1,Key2,Key3,Key4,KeyByte,LastKeyByte Const HUMAN = 1 Const AI = 2 Const REMOTE= 3 Const INACTIVE = 4 Function CreateRemote( Net_ID, Local_ID, Keys, X, Y, Dir, Armor=0, Energy=0 ) Local P:Probe = New Probe P.Control = REMOTE P.Net_ID = Net_ID P.Local_ID = Local_ID P.X = X ;P.Y = Y P.Dir = Dir P.KeyByte = Keys 'P.Armor = Armor 'P.Energy = Energy EndFunction Function CreateHuman() Local P:Probe = New Probe LocalCount:+1 P.Control = HUMAN P.Local_ID = LocalCount DebugLog "Human Probe Created - LocalID = "+P.Local_ID End Function Function CreateAI(Count = 1) For Local NR = 1 To Count Local P:Probe = New Probe LocalCount:+1 P.Control = AI 'On this computer P.Local_ID = LocalCount Next EndFunction Method New()'Defaults If List = Null Then List = CreateList() List.AddLast(Self) 'Name$ = "Probe #"+List.Count() X = Rand ( 10, GWIDTH -10 ) Y = Rand ( 10, GHEIGHT-10 ) Dir = Rand( 0,360) Energy = 80 Armor = 100 TopSpeed = 3 TurnSpeed = 3 Acceleration = 0.2 EndMethod Function UpdateALL() If List For Local P:Probe = EachIn List P.Update() Next EndIf EndFunction Method Update() If Control = HUMAN If KeyDown(Key_Up) Key1 = 1 Else Key1 = 0 If KeyDown(Key_Down) Key2 = 1 Else Key2 = 0 If KeyDown(Key_Left) Key3 = 1 Else Key3 = 0 If KeyDown(Key_Right) Key4 = 1 Else Key4 = 0 ElseIf Control = AI RunAI() ElseIf Control = REMOTE 'DebugLog "KeyByte: "+KeyByte+" Key1: "+Key1+" Key2: "+Key2+" Key3: "+Key3+" Key4: "+Key4'+" Key5: "+Key5+" Key6: "+Key6+" Key7: "+Key7+" Key8: "+Key8 LastKeyByte = KeyByte EndIf 'DrawText "Byte: "+KeyByte+" Key1: "+Key1+" Key2: "+Key2+" Key3: "+Key3+" Key4: "+Key4, 100,300 'Same Flags To Byte If Abs(Speed) < 0.1 Speed = 0 If Key1 CAR1_car_throttle# = 2000.0 ElseIf Not key1 CAR1_car_throttle# = 0.0 If Key2 Then CAR1_car_throttle# = 0.0 CAR1_car_brake# = 100.0 Else CAR1_car_brake# = 0.0 EndIf If Key4 CAR1_car_steerangle# = (-M_PI# / 4.0) * 0.15'* MULT2# If Key3 CAR1_car_steerangle# = (M_PI# / 4.0) * 0.15 'MULT2# If Speed > TopSpeed Speed = TopSpeed If Speed < -TopSpeed Speed = -TopSpeed If Dir > 360 Then Dir:-360 If Dir < 0 Then Dir:+360 'XSpeed = Speed*Cos(Dir) 'YSpeed = Speed*Sin(Dir) 'Friction 'If Speed > 0 Speed:- 0.01 'If Speed < 0 Speed:+ 0.01 'X:+XSpeed 'Y:+YSpeed x=CAR1_car_position_wc_x# y=CAR1_car_position_wc_y# 'render a small moving car rep. renda1# =( -CAR1_car_angle# * MULT# ) - 60 If (renda1#<0.0) Then renda1#=360.0+renda1# renda2# =( -CAR1_car_angle# * MULT# ) + 60 If (renda2#>359.0) Then renda2#=renda2#-360.0 renda3# =( -CAR1_car_angle# * MULT# ) - 120 If (renda3#<0.0) Then renda3#=360.0+renda3# renda4# =( -CAR1_car_angle# * MULT# ) + 120 If (renda4#>359.0) Then renda4#=renda4#-360.0 rendx1#=25*Cos(renda1#) rendy1#=25*Sin(renda1#) rendx2#=25*Cos(renda2#) rendy2#=25*Sin(renda2#) rendx3#=25*Cos(renda3#) rendy3#=25*Sin(renda3#) rendx4#=25*Cos(renda4#) rendy4#=25*Sin(renda4#) '**** Fizika************************************************************************** '************************************************************************ '************************************************************************ sn# = Sin(CAR1_car_angle# * MULT#)' * MULT2# cs# = Cos(CAR1_car_angle# * MULT#) '* MULT2# ' DrawText 400,0,"sn="+sn+" cs="+cs ' SAE convention: x is To the front of the car, y is To the Right, z is down ' transform velocity in world reference frame To velocity in car reference frame velocity_x# = cs# * CAR1_car_velocity_wc_y# + sn# * CAR1_car_velocity_wc_x# velocity_y# = -sn# * CAR1_car_velocity_wc_y# + cs# * CAR1_car_velocity_wc_x# ' Lateral force on wheels ' Resulting velocity of the wheels as result of the yaw rate of the car body ' v = yawrate * r where r is distance of wheel To CG (approx. half wheel base) ' yawrate (ang.velocity) must be in rad/s yawspeed# = CAR1_cartype_wheelbase# * 0.5 * CAR1_car_angularvelocity# If( velocity_x# = 0 ) rot_angle# = 0 Else ' rot_angle# = ATan2( (yawspeed# * MULT#) , (velocity_x# * MULT#) ) rot_angle# = ATan2( (yawspeed# ) , (velocity_x#) ) * MULT2# EndIf ' Calculate the side slip angle of the car (a.k.a. beta) If( velocity_x# = 0 ) sideslip# = 0.0 Else ' sideslip# = ATan2( (velocity_y# * MULT#) , (velocity_x# * MULT#) ) sideslip# = ATan2( (velocity_y# ) , (velocity_x#) ) * MULT2# EndIf ' Calculate slip angles For front And rear wheels (a.k.a. alpha) slipanglefront# = sideslip# + rot_angle# - CAR1_car_steerangle# slipanglerear# = sideslip# - rot_angle# ' weight per axle = half car mass times 1G (=9.8m/s^2) weight# = CAR1_cartype_mass# * 9.8 * 0.5 ' lateral force on front wheels = (Ca * slip angle) capped To friction circle * load flatf_x# = 0.0 flatf_y# = CA_F# * slipanglefront# ' If ( flatf_y# > MAX_GRIP# ) flatf_y# = MAX_GRIP# ' If ( flatf_y# < -MAX_GRIP# ) flatf_y# = -MAX_GRIP# flatf_y# = flatf_y# * weight# ' allow front wheels To slip If(front_slip=1) flatf_y# = flatf_y# * 0.5 ' lateral force on rear wheels flatr_x# = 0.0 flatr_y# = CA_R# * slipanglerear# ' If ( flatr_y# > MAX_GRIP# ) flatr_y# = MAX_GRIP# ' If ( flatr_y# < -MAX_GRIP# ) flatr_y# = -MAX_GRIP# flatr_y# = flatr_y# * weight# If(rear_slip = 1) flatr_y# = flatr_y# * 0.5 ' longtitudinal force on rear wheels - very simple traction model ftraction_x# = 100.0 * (CAR1_car_throttle# - CAR1_car_brake# * Sgn(velocity_x#)) ftraction_y# = 0.0 If(rear_slip = 1) ftraction_x# = ftraction_x# * 0.5 ' Forces And torque on body ' drag And rolling resistance resistance_x# = -( RESISTANCE# * velocity_x# + DRAG# * velocity_x# * Abs( velocity_x# ) ) resistance_y# = -( RESISTANCE# * velocity_y# + DRAG# * velocity_y# * Abs( velocity_y# ) ) ' sum forces force_x# = ftraction_x# + (Sin( CAR1_car_steerangle# * MULT# )) * flatf_x# + flatr_x# + resistance_x# force_y# = ftraction_y# + (Cos( CAR1_car_steerangle# * MULT# )) * flatf_y# + flatr_y# + resistance_y# ' torque on body from lateral forces torque# = CAR1_cartype_b# * flatf_y# - CAR1_cartype_c# * flatr_y# ' Acceleration ' Newton F = m.a, therefore a = F/m acceleration_x# = force_x# / CAR1_cartype_mass# acceleration_y# = force_y# / CAR1_cartype_mass# angular_acceleration# = torque# / CAR1_cartype_inertia# ' Velocity And position ' transform acceleration from car reference frame To world reference frame acceleration_wc_x# = cs# * acceleration_y# + sn# * acceleration_x# acceleration_wc_y# = -sn# * acceleration_y# + cs# * acceleration_x# ' velocity is integrated acceleration CAR1_car_velocity_wc_x# = CAR1_car_velocity_wc_x# + (delta_t# * acceleration_wc_x#) CAR1_car_velocity_wc_y# = CAR1_car_velocity_wc_y# + (delta_t# * acceleration_wc_y#) ' position is integrated velocity CAR1_car_position_wc_x# = CAR1_car_position_wc_x# + (delta_t# * CAR1_car_velocity_wc_x#) CAR1_car_position_wc_y# = CAR1_car_position_wc_y# + (delta_t# * CAR1_car_velocity_wc_y#) ' Angular velocity And heading ' integrate angular acceleration To get angular velocity CAR1_car_angularvelocity# = CAR1_car_angularvelocity# + (delta_t# * angular_acceleration#) ' integrate angular velocity To get angular orientation CAR1_car_angle# = CAR1_car_angle# + (delta_t# * CAR1_car_angularvelocity#) If ( CAR1_car_position_wc_x# < 0.0 ) CAR1_car_position_wc_x# = 800.0 If ( CAR1_car_position_wc_x# > 800.0 ) CAR1_car_position_wc_x# = 0.0 If ( CAR1_car_position_wc_y# < 0.0 ) CAR1_car_position_wc_y# = 600.0 If ( CAR1_car_position_wc_y# > 600.0 ) CAR1_car_position_wc_y# = 0.0 '*********************************************************************************** '************************************************************************ '************************************************************************ 'If Speed < 0.1 And Speed > 0 Then Speed = 0 'If Speed > -0.1 And Speed < 0 Then speed = 0 Draw() CollideScreenBorders() EndMethod Method Draw() SetColor 220,220,220 SetRotation Cos(rendx1)+Sin(rendy1) DrawImage Probeimage,x,y SetRotation 0 DrawText Speed, 10,10 'DrawTriangle(X,Y,Dir)'Req rotation 0 DrawLine rendx1#+x#,rendy1#+y#,rendx2#+x#,rendy2#+y# DrawLine rendx2#+x#,rendy2#+y#,rendx4#+x#,rendy4#+y# DrawLine rendx4#+x#,rendy4#+y#,rendx3#+x#,rendy3#+y# DrawLine rendx3#+x#,rendy3#+y#,rendx1#+x#,rendy1#+y# EndMethod Method DrawTriangle(X,Y,Dir,Size=5) Local HeadX#,HeadY#,LeftX#,LeftY#,RightX#,RightY# HeadX = X + Cos(Dir)*Size HeadY = Y + Sin(Dir)*Size LeftX = X + Cos(Dir-140)*Size LeftY = Y + Sin(Dir-140)*Size RightX = X + Cos(Dir+140)*Size RightY = Y + Sin(Dir+140)*Size DrawPoly ([HeadX,HeadY,LeftX,LeftY,RightX,RightY]) EndMethod Method CollideScreenBorders() If X < 0 Then X = GWIDTH If Y < 0 Then Y = GHEIGHT If Y > GHEIGHT Then Y = 0 If X > GWIDTH Then X = 0 EndMethod Method RunAI() 'Change State If UpdateAI < MilliSecs() Local NextUpdate Mode = Rand(1,5) Select Mode Case 1'MoveForward NextUpdate = 1000 Case 2'Forward - Left NextUpdate= Rand(10,25)*100 Case 3'Forward - Favor Right NextUpdate= Rand(10,25)*100 Case 4'Short Back NextUpdate= 500 Default NextUpdate= 100 End Select UpdateAI = MilliSecs() + NextUpdate EndIf Key1 = 0;Key2 = 0;Key3 = 0;Key4 = 0 'Use selected Mode If Mode = 1 Key1 = 1'Forward If Mode = 2 Key1 = 1 ; Key3 = 1 'Left If Mode = 3 Key1 = 1 ; Key4 = 1 'Right If Mode = 4 Key2 = 1'Back EndMethod End Type 'Set Keys How can i move my car like move the lines? Somebody explain me the working of transmission? How can i add this project sound? (Motor sound, kurve sound) And, if i push the "N" key... O_O See! ![]() THX! |
||
![]() |
Markus2 |
![]() Antworten mit Zitat ![]() |
---|---|---|
If you know the car angle you can rotate the car image like the lines
and after paint the car image set rotate 0 and then paint your line to see if the image are correct . I know this physic example that you convertet to bmax ![]() This example is ok but not really good and have some bugs in physic . if you know the forces in a kurve you can set a limit that the wheels slip like slip limit force =10000 , force like 12000 12000-10000=2000 max =8000=1 Volume 2000 = ? Volume 0=0 Volume for the motor sound you need the velocity and the engine speed like 0-7500 for each gear you have a different force a = F/m or F=m*a Physics of Racing Series http://www.miata.net/sport/Physics/ |
||
Dreamora |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
Please write german.
If you are non-german, please post at the english BlitzMax community at www.blitzbasic.com. ~GESCHLOSSEN~ Englische Threads nutzen niemandem Dreamora |
||
Ihr findet die aktuellen Projekte unter Gayasoft und könnt mich unter @gayasoft auf Twitter erreichen. |
Übersicht


Powered by phpBB © 2001 - 2006, phpBB Group