It is currently Sat Jul 04, 2020 3:06 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Simple CarPhysik , need help ;) c# to pb
PostPosted: Sat Apr 25, 2015 12:38 pm 
Offline
User
User

Joined: Sun Feb 03, 2013 1:50 am
Posts: 63
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:
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:

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:
[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


Top
 Profile  
Reply with quote  
 Post subject: Re: Simple CarPhysik , need help ;) c# to pb
PostPosted: Sat Apr 25, 2015 7:09 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1282
Location: Germany, Glienicke
Quote:
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())

Quote:
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.

_________________
ImageImage


Top
 Profile  
Reply with quote  
 Post subject: Re: Simple CarPhysik , need help ;) c# to pb
PostPosted: Sat Apr 25, 2015 7:39 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4789
Location: Germany
Hi,

unfortunately I had to create a WORKING EXAMPLE first.
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Simple CarPhysik , need help ;) c# to pb
PostPosted: Sat Apr 25, 2015 7:42 pm 
Offline
User
User

Joined: Sun Feb 03, 2013 1:50 am
Posts: 63
hm bad thanks for your try ;)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye