Simple CarPhysik , need help ;) c# to pb

Advanced game related topics
True29
User
User
Posts: 64
Joined: Sun Feb 03, 2013 1:50 am

Simple CarPhysik , need help ;) c# to pb

Post by True29 »

Hallo ,

i had found on this site
http://gamedev.stackexchange.com/questi ... -dimension
a simple looking carphysik.
now after implement it , my car move without do something ^^

perhaps somebody see my mistake ;) thanks for help.

code from site:

Code: Select all

Vector2 position;
Vector2 frontTire, backTire;
float direction;
float speed;
float angle;
float wheelBase = 32;

frontTire = position + wheelBase / 2 * new Vector2((float)Math.Cos(direction), (float)Math.Sin(direction));
backTire = position - wheelBase / 2 * new Vector2((float)Math.Cos(direction), (float)Math.Sin(direction));
backTire += speed * elapsed * new Vector2((float)Math.Cos(direction), (float)Math.Sin(direction));
frontTire += speed * elapsed * new Vector2((float)Math.Cos(direction + angle), (float)Math.Sin(direction + angle));
position = (frontTire + backTire) / 2;
direction = (float)Math.Atan2(frontTire.Y - backTire.Y, frontTire.X - backTire.X);
my code:

Code: Select all


Structure Vector
	X.f
	Y.f
EndStructure

Structure Car     
  Position.Vector
  frontTire.Vector
  backTire.Vector
  speed.f
  angle.f
  wheelbase.f
  direction.f
EndStructure

Procedure.i V_Add(*Use.Vector, *Source.Vector, Factor.VT=1.0)
	*Use\X + *Source\X*Factor
	*Use\Y + *Source\Y*Factor
	ProcedureReturn *Use
EndProcedure

Procedure.i V_Divide(*Use.Vector, Value.VT)
	*Use\X / Value
	*Use\Y / Value
	ProcedureReturn *Use
EndProcedure

Procedure.f gSin(Ang.f) 
  ProcedureReturn Sin(Radian(Ang))
EndProcedure

Procedure.f gCos(Ang.f)
  ProcedureReturn Cos(Radian(Ang))
EndProcedure

;//frontTire = position + wheelBase / 2 * new Vector2((float)Math.Cos(direction), (float)Math.Sin(direction));
PlayerCar()\frontTire\x = PlayerCar()\Position\X + PlayerCar()\wheelbase /2 * gCos(PlayerCar()\direction)
PlayerCar()\frontTire\y = PlayerCar()\Position\Y + PlayerCar()\wheelbase /2 * gSin(PlayerCar()\direction)
  
;//backTire = position - wheelBase / 2 * new Vector2((float)Math.Cos(direction), (float)Math.Sin(direction));
PlayerCar()\backTire\x = PlayerCar()\Position\X - PlayerCar()\wheelbase /2 * gCos(PlayerCar()\direction)
PlayerCar()\backTire\y = PlayerCar()\Position\Y - PlayerCar()\wheelbase /2 * gSin(PlayerCar()\direction)  

;//backTire += speed * elapsed * new Vector2((float)Math.Cos(direction), (float)Math.Sin(direction));
PlayerCar()\backTire\X + PlayerCar()\speed * elapsed * gCos(PlayerCar()\direction) 
PlayerCar()\backTire\Y + PlayerCar()\speed * elapsed * gSin(PlayerCar()\direction) 

;//frontTire += speed * elapsed * new Vector2((float)Math.Cos(direction + angle), (float)Math.Sin(direction + angle));
PlayerCar()\frontTire\X + PlayerCar()\speed * elapsed * gCos(PlayerCar()\direction + PlayerCar()\angle) 
PlayerCar()\frontTire\Y + PlayerCar()\speed * elapsed * gSin(PlayerCar()\direction + PlayerCar()\angle) 

;// position = (frontTire + backTire) / 2;

V_Add(PlayerCar()\Position,V_Division(PlayerCar()\Position,V_Add(PlayerCar()\frontTire,PlayerCar()\backTire),2))

;direction = (float)Math.Atan2(frontTire.Y - backTire.Y, frontTire.X - backTire.X);
PlayerCar()\direction = ATan2(PlayerCar()\frontTire\Y - PlayerCar()\backTire\Y,PlayerCar()\frontTire\x - PlayerCar()\backTire\x)

debug:

Code: Select all

[13:38:35] speed0
[13:38:35] direction0
[13:38:35] angle0
[13:38:35] frontTire0
[13:38:35] frontTire0
[13:38:35] backTire0
[13:38:35] backTire0
[13:38:35] Position512
[13:38:35] Position400

[13:38:35] speed0
[13:38:35] direction0.9149441719
[13:38:35] angle0
[13:38:35] frontTire2048
[13:38:35] frontTire1600
[13:38:35] backTire1008.0020751953
[13:38:35] backTire799.7424316406
[13:38:35] Position2048
[13:38:35] Position1600
User avatar
STARGÅTE
Addict
Addict
Posts: 2084
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Simple CarPhysik , need help ;) c# to pb

Post by STARGÅTE »

PlayerCar()\direction = ATan2(PlayerCar()\frontTire\Y - PlayerCar()\backTire\Y,PlayerCar()\frontTire\x - PlayerCar()\backTire\x)
wrong parameters, in PB: ATan2(X, Y)

If you want PlayerCar()\direction in degree, use Degree(ATan2())
V_Add(PlayerCar()\Position,V_Division(PlayerCar()\Position,V_Add(PlayerCar()\frontTire,PlayerCar()\backTire),2))
Wrong usage of this procedures, the first operand is changed
Use X and Y in singe lines.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
infratec
Always Here
Always Here
Posts: 6866
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Simple CarPhysik , need help ;) c# to pb

Post by infratec »

Hi,

unfortunately I had to create a WORKING EXAMPLE first.

Code: Select all

EnableExplicit

#ScrWidth = 800
#ScrHeight = 600

Structure Vector
   X.f
   Y.f
EndStructure

Structure Car     
  Position.Vector
  frontTire.Vector
  backTire.Vector
  speed.f
  angle.f
  wheelbase.f
  direction.f
  Sprite.i
EndStructure


Define.i Event
Define.f elapsed
NewList Playercar.Car()

;-main
InitSprite()
InitKeyboard()
InitMouse()


OpenWindow(0, 0, 0, #Scrwidth, #ScrHeight, "Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #Scrwidth, #ScrHeight)

AddElement(Playercar())
Playercar()\Position\X = #ScrWidth / 2
Playercar()\Position\Y = #ScrHeight / 2
Playercar()\wheelbase = 32
Playercar()\Sprite = CreateSprite(#PB_Any, 20, 20)
StartDrawing(SpriteOutput(Playercar()\Sprite))
Box(0, 0, 5, 5, #Red)
Box(16, 1, 4, 4, #Red)
Box(0, 5, 20, 10, #Red)
Box(16, 15, 4, 4, #Red)
Box(0, 15, 5, 5, #Red)
StopDrawing()


elapsed = 0.1

Repeat
  ClearScreen(0)
  
  ExamineKeyboard()
  
  
  If KeyboardPushed(#PB_Key_Up)
    Playercar()\speed + 1
  ElseIf KeyboardPushed(#PB_Key_Down)
    Playercar()\speed - 1
  EndIf
  
  If KeyboardPushed(#PB_Key_Left)
    Playercar()\angle - 0.03
  ElseIf KeyboardPushed(#PB_Key_Right)
    Playercar()\angle + 0.03
  EndIf

  If Playercar()\speed < 0
    Playercar()\speed = 0
  ElseIf Playercar()\speed > 30
    Playercar()\speed = 30
  EndIf
  
  Debug ""
  
  Debug Playercar()\angle
  Debug Playercar()\speed
  
  
  ;//frontTire = position + wheelBase / 2 * new Vector2((float)Math.Cos(direction), (float)Math.Sin(direction));
  PlayerCar()\frontTire\x = PlayerCar()\Position\X + PlayerCar()\wheelbase / 2 * Cos(PlayerCar()\direction)
  PlayerCar()\frontTire\y = PlayerCar()\Position\Y + PlayerCar()\wheelbase / 2 * Sin(PlayerCar()\direction)
  
  ;//backTire = position - wheelBase / 2 * new Vector2((float)Math.Cos(direction), (float)Math.Sin(direction));
  PlayerCar()\backTire\x = PlayerCar()\Position\X - PlayerCar()\wheelbase / 2 * Cos(PlayerCar()\direction)
  PlayerCar()\backTire\y = PlayerCar()\Position\Y - PlayerCar()\wheelbase / 2 * Sin(PlayerCar()\direction)
  
  ;//backTire += speed * elapsed * new Vector2((float)Math.Cos(direction), (float)Math.Sin(direction));
  PlayerCar()\backTire\X + (PlayerCar()\speed * elapsed * Cos(PlayerCar()\direction))
  PlayerCar()\backTire\Y + (PlayerCar()\speed * elapsed * Sin(PlayerCar()\direction))
  
  ;//frontTire += speed * elapsed * new Vector2((float)Math.Cos(direction + angle), (float)Math.Sin(direction + angle));
  PlayerCar()\frontTire\X + (PlayerCar()\speed * elapsed * Cos(PlayerCar()\direction + PlayerCar()\angle))
  PlayerCar()\frontTire\Y + (PlayerCar()\speed * elapsed * Sin(PlayerCar()\direction + PlayerCar()\angle))
  
  ;// position = (frontTire + backTire) / 2;
  PlayerCar()\Position\X = (PlayerCar()\frontTire\X + PlayerCar()\backTire\X) / 2
  PlayerCar()\Position\Y = (PlayerCar()\frontTire\Y + PlayerCar()\backTire\Y) / 2
  
  ;direction = (float)Math.Atan2(frontTire.Y - backTire.Y, frontTire.X - backTire.X);
  PlayerCar()\direction = ATan2(PlayerCar()\frontTire\x - PlayerCar()\backTire\x, PlayerCar()\frontTire\Y - PlayerCar()\backTire\Y)
  
    
  Debug StrF(PlayerCar()\Position\X, 2) + " " + StrF(Playercar()\Position\Y, 2)
  
  DisplaySprite(PlayerCar()\Sprite, PlayerCar()\Position\X, Playercar()\Position\Y)
  
  FlipBuffers()
  
  Event = WindowEvent()
  
Until KeyboardPushed(#PB_Key_Escape) Or Event = #PB_Event_CloseWindow
I also changed already the Atan2() parameters, but still funny results.

Bernd
True29
User
User
Posts: 64
Joined: Sun Feb 03, 2013 1:50 am

Re: Simple CarPhysik , need help ;) c# to pb

Post by True29 »

hm bad thanks for your try ;)
Post Reply