Jump to content

Steering a car by code


diedir
 Share

Recommended Posts

Hi all

 

since a week i am stuck with my code not working as expected, i need a fresh look at what i am doing wrong.I am surely doing something wrong but i can't get it.Hope someone could help.

 

Here the facts:

 

i need to make a car to follow a path made by some given points.

the car need to steer realisticaly (sorry for bad saying) so every loop,

i put a pivot at car' place, then i point it to the given point to retrieve y angle

then i divide the entitydistance from the car pivot to given point by this angle,

and i steer my car by this result.

 

example of my print log:

(car rotation - pointrotation) = 12.03..

mydistance = 43.17..

so my sterrangle = -3.58....

 

for this first point all run fine the car slowly move (with a given torque) to the given point smoothly but there i change the pivot to point the new given point and got:

 

(car rotation - new pointrotation) = -20.54..

new distance = 54.01

steerangle = 2.62

 

not so bad but the car don't react as wished, it run away nearly straight and don't point ever where i wanted.

I tried a lot different thigs with no luck.

 

here a part of my code which doing this.

Hope someone here point my bad maths and that i can move on....

thanks for reading

 

local pivps = CreatePivot()
PositionEntity(pivps,Vec3(pscx[bornai],1.0,pscz[bornai]))
local hypdist = EntityDistance(chassai[1],pivps)
local pivcarai = CreatePivot()
PositionEntity(pivcarai,EntityPosition(chassai[1]))
PointEntity(pivcarai,pivps,2,1.0)
local Angrot = (EntityRotation(pivps).y - EntityRotation(pivcarai).y )

if bornai < 3 then
 steerangleai[1] = -(hypdist/Angrot)
 steerangleai[1]=Clamp(steerangleai[1],-steerlimit,steerlimit)
 carai[1]:SetSteerAngle(steerangleai[1],0)
 carai[1]:SetSteerAngle(steerangleai[1],1)
end

AMD Ryzen 5900HX - Nvidia RTX 3070 - 32 Go - 1To SSD - W11

Link to comment
Share on other sites

Just an observation but you quoted:

 

i point it to the given point to retrieve y angle then i divide this angle by the entitydistance from the car pivot to given point, and i steer my car by this result.

 

but in your code you are doing the opposite, that is you are dividing your distance by the angle:

 

steerangleai[1] = -(hypdist/Angrot)

 

I can understand the angle divided by the distance as it will progressively approach the final angle as the distance gets less (closer) although presumably you would want to clamp the distance value at 1 once it goes lower than that. I take it you are aiming for a curved movement towards the target? I've not tried the code so I don't know if this is the issue or not, it just was an obvious discrepancy between your code and your description!

 

Also, are you creating new pivots in every iteration of the game loop, you really only need to create them once outside of the loop and then reposition them each time!

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

Hi Pixel Perfect

 

you are right on the division direction, i have said the opposite of what i did, ( edited my post).

i think it is right to divide the distance by the angle, to retrieve needed angle at each distance.

 

you are right too with that i aim a curved move from 2 points.(it works well on the first one then ... failed)

i must think about clamping distance or angle below 1.0, perhaps good idea;

 

For the pivot re-created every loop, naïvely i thought that it would re-use the first one as it is still "alive",

but you are right, it is not very clever to recreate it anyway, that part of code don't really bother me.

 

thanks for answering

AMD Ryzen 5900HX - Nvidia RTX 3070 - 32 Go - 1To SSD - W11

Link to comment
Share on other sites

Update on this:

 

I tried your code tonight and I couldn't get it to work beyond the first target either. I'm pretty sure it has to do with which quadrant the angle falls into when its returned and the need to process it differently depending on that.

 

I've come up with a simpler solution which may or may not be suitable depending on exactly what you are trying to do with this but it relies on the PointEntity rate setting to produce the gradual curved change of direction. The codes below but in C++. You would probably need to modify the rate and movement values by app speed to adjust for frame rate changes.

 

There is a small vid of it in action here, I had to zip it up as we don't seem to have a way of loading vids unless its on you tube: Move from point to point.zip

 

// ====================================================================
// This file was generated by LEBuilder
// http://leadwerks.com/werkspace
// ====================================================================
#include "engine.h"
#include <iostream>
#include <string>
#include "Framewerk.h"
using namespace leadwerks;
const int  ScreenWidth = 800;
const int  ScreenHeight = 600;
const char* AppTitle = "Test";
void ErrOut( const std::string& message ) { std::cerr << message << std::endl; }


int main( int argn, char* argv[] )
{

if( !Initialize() )
 return 1;	  

// Set graphics mode	  
if( !Graphics(ScreenWidth,ScreenHeight) )
{			  
 ErrOut( "Failed to set graphics mode."  );
 return 1;	  
}
SetAppTitle( AppTitle );

// Create framework object and set it to a global object so other scripts can access it
TFramework fw = CreateFramework();	  
if( fw == NULL )	  
{
 ErrOut( "Failed to initialize engine." );			  
 return 1;	  
}	  

// Set Lua framework object	  
SetGlobalObject( "fw", fw );			  

// Set Lua framework variable	  
BP lua = GetLuaState();	  
lua_pushobject( lua, fw );	  
lua_setglobal( lua, "fw" );	  
lua_pop( lua, 1 );

// Get framework main camera	  
TCamera camera = GetLayerCamera( GetFrameworkLayer(0) );	  
PositionEntity( camera, Vec3(0,9,0) );

// Create cube to steer
TMaterial material = LoadMaterial( "abstract::cobblestones.mat" );	  
TMesh mesh = CreateCube();	  
EntityColor(mesh,Vec4(1,0,0,1),1);
PositionEntity( mesh, Vec3(0,0,0) );  

// Create an TMesh array for the targets
TMesh targetMesh[3];


// Create the targets
targetMesh[0]=CopyEntity(mesh,0);
ScaleMesh(targetMesh[0],Vec3(0.5));
EntityColor(targetMesh[0],Vec4(0,0,1,1),1);
PositionEntity(targetMesh[0], Vec3(-2,0,3));

targetMesh[1]=CopyEntity(targetMesh[0],0);
PositionEntity(targetMesh[1], Vec3(3,0,2));


targetMesh[2]=CopyEntity(targetMesh[0],0);
PositionEntity(targetMesh[2], Vec3(3,0,-3));

// Point the camear down at the main mesh
PointEntity(camera,mesh);

// Create ground
TMesh ground = CreateCube();	  
ScaleEntity( ground, Vec3(10,1,10) );	  
PositionEntity( ground, Vec3(0,-2,0) );	  
PaintEntity( ground, material );	  

// Add some light
TLight light = CreateDirectionalLight();	  
RotateEntity( light, Vec3(45,45,45) );


int targetNumber = 0;
float targetDistance;
float angle;
TEntity piv1 = CreatePivot();
bool move = true;

// Loop
while( !KeyHit() && !AppTerminate() )	  
{			  
  if( !AppSuspended() )
 {
  if(move)
  {
// Poisition pivot at main cube
PositionEntity(piv1,EntityPosition(mesh,1),1);

// Point it at the current target
PointEntity(piv1,targetMesh[targetNumber],3,0.001);

// Calc the angle
angle = EntityRotation(piv1).Y - EntityRotation(targetMesh[targetNumber]).Y;

// Calc the distance to the target
targetDistance = EntityDistance(mesh,targetMesh[targetNumber]);

// If within 0.25 units then set up the next target
if(targetDistance < 0.25)
{
 targetNumber++;
 if(targetNumber > 2)
 {
  move = false;
 }
}
RotateEntity(mesh, Vec3(EntityRotation(mesh,1).X,angle,EntityRotation(mesh,1).Z),1);
MoveEntity(mesh,Vec3(0,0,0.001),0);
  }
  UpdateFramework();			  
  RenderFramework();			  
  Flip(0);	  
 }
}

return Terminate();
}

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

well i tried some changes without any luck, the second point is always avoided despite my efforts.

i read your code but i am curious why you put :

// Point it at the current target
    PointEntity(piv1,targetMesh[targetNumber],3,0.001);

it appears to be the Z axis (3) targeted, wouldn't it be Y axis (2) ?

anyway z or y none is accurate for me.

not reacting as your video at all.

I must dig it more and find why the steerangle don't apply well..

thanks anyway for your time.

AMD Ryzen 5900HX - Nvidia RTX 3070 - 32 Go - 1To SSD - W11

Link to comment
Share on other sites

You may need to increase the PointEntity rate setting (the one that's currently set to 0.001) depending on the frame rate you are getting to ensure it curves sufficiently to reach the target, especially if the targets are close together. Try the setting initially as 1.0, this should not curve at all and send you vehicle from one to the next in a straight line. This is a good first check to see that the rest of the code is working correctly.

 

I have always used 3 (z) in this scenario as 2 has never worked for me, not sure if this is something unique to the C dll.

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...