# PureBasic Forum

 It is currently Thu Sep 24, 2020 10:34 am

 All times are UTC + 1 hour

 Page 1 of 1 [ 4 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Simple CarPhysik , need help ;) c# to pbPosted: Sat Apr 25, 2015 12:38 pm
 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

 Post subject: Re: Simple CarPhysik , need help ;) c# to pbPosted: Sat Apr 25, 2015 7:09 pm
 Addict

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1295
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.

_________________

Top

 Post subject: Re: Simple CarPhysik , need help ;) c# to pbPosted: Sat Apr 25, 2015 7:39 pm
 Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4928
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

 Post subject: Re: Simple CarPhysik , need help ;) c# to pbPosted: Sat Apr 25, 2015 7:42 pm
 User

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

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 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 forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - IDE    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite

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