Jump to content

Reset the force and torque


Gilmer
 Share

Recommended Posts

I'm with a doubt. When my car vehicle get out the road, it pass over a trigger, that return back the car to road in certain position. The problem, is that when it pass in some trigger, it have a velocity, and when back to center road, the velocity coninue, but I need that the car back to the road stopped, like in all game cars, when the player get out from road.

 

I used:

SetBodyVelocity(car,Vec3(0,0,0),0);
SetBodyOmega(car,Vec3(0,0,0),0);

 

The car back to road stopped, but after millisecs for sample, it begins to turn to right or left randomly, like if have some forces applying over car.

 

What function I can use to "reset" the force in vehicle, and torques in tires?!

 

thx

Link to comment
Share on other sites

Yes, eg: the car is moving normally, and in certain moment, it's moved to position (0,0,0). But I want it is positioned, and stay stopped in new positioned. Whats occurring, is that it's being positioned and continues going to front, because of force applied in the wheels..

 

I'm using C/C++

Link to comment
Share on other sites

An example, of what I want. This is the script of driver.lua, of viperscout. I just add this IF in main loop:

 

require("Scripts/constants/keycodes")
require("Scripts/linkedlist")
require("Scripts/filesystem")
require("scripts/math/math")
require("scripts/constants/engine_const")

FlushKeys()
HideMouse()

local camera = fw.main.camera

chassis=LoadModel("abstract::vehicle_viperscout.gmf")
carobject=objecttable[chassis]
car=carobject.vehicle

if car==nil then
Notify("Vehicle object not found.",1)
chassis:Free()
return
end

chassis:SetPosition(TFormPoint(Vec3(0,-1,10),fw.main.camera,nil))
chassis:SetRotationf(0,camera.rotation.y+180.0,0)

--Variables
local dx=0.0
local dy=0.0
local camerapitch=camera.rotation.x
local camerayaw=camera.rotation.y
local move=0.0
local strafe=0.0
local steering = 0.0
local torque = 0.0
local steerlimit = 30.0
local steerrate = 2.0
local steerangle=0.0

MoveMouse(Round(GraphicsWidth()/2),Round(GraphicsHeight()/2))

while KeyHit(KEY_ESCAPE)==0 do	

--Camera look
gx=Round(GraphicsWidth()/2)
gy=Round(GraphicsHeight()/2)
dx=Curve((MouseX()-gx)/4.0,dx,3.0/AppSpeed())
dy=Curve((MouseY()-gy)/4.0,dy,3.0/AppSpeed())
MoveMouse(gx,gy)
camerapitch=camerapitch+dy
camerayaw=camerayaw-dx
camerapitch=math.min(camerapitch,90)
camerapitch=math.max(camerapitch,-90)
fw.main.camera:SetRotationf(camerapitch,camerayaw,0,1)

local tirespeed=-(KeyDown(KEY_UP)-KeyDown(KEY_DOWN))*2.0
car:AddTireTorque(tirespeed,0)
car:AddTireTorque(tirespeed,1)
car:AddTireTorque(tirespeed,2)
car:AddTireTorque(tirespeed,3)

steermode=0
if KeyDown(KEY_RIGHT)==1 then steermode=steermode-1 end
if KeyDown(KEY_LEFT)==1 then steermode=steermode+1 end

if steermode==1 then
	steerangle=steerangle+4.0*AppSpeed()
elseif steermode==-1 then
	steerangle=steerangle-4.0*AppSpeed()
else
	if steerangle>0 then
		steerangle=steerangle-4.0*AppSpeed()
		if steerangle<0.0 then steerangle=0.0 end
	else
		steerangle=steerangle+4.0*AppSpeed()
		if steerangle>0.0 then steerangle=0.0 end
	end
end
steerangle=Clamp(steerangle,-25,25)

car:SetSteerAngle(steerangle,0)
car:SetSteerAngle(steerangle,1)

fw:Update()

local campos=TFormPoint(Vec3(0,1,0),chassis,nil)

fw.main.camera:SetPosition(campos)
fw.main.camera:Move(Vec3(0,0,-10))

local t=TFormVector(Vec3(0,0,1),camera,chassis)
a=-math.deg(math.atan2(t.x,t.z))+180.0

carobject.turret:SetRotationf(0,CurveAngle(a,carobject.turret.rotation.y,10.0/AppSpeed()),0)

chassis:Hide()
local pick = LinePick(campos,camera.position,0.5,COLLISION_PROP)
chassis:Show()
if pick~=nil then
	camera:SetPosition(pick.position)
end

if(KeyHit(KEY_SPACE)==1) then
	chassis:SetPosition(Vec3(0,0,0))
end

fw:Render()
Flip(0)
end

chassis:Free()
chassis=nil
camera=nil

ShowMouse()

 

if(KeyHit(KEY_SPACE)==1) then
    chassis:SetPosition(Vec3(0,0,0))
end

 

If you are running with the car normally, and press SPACE, the car is back to position (0,0,0), but continue with velocity. I want to the car back to pos(0,0,0), and stay stopped, ie, without force..

Link to comment
Share on other sites

Yes, the wheels continues with the speed it was before I place in new position. But I used that Lumooja said, it's working. The only problem is that if the car is running in a curve, and while accelerate, if is positioned for a new position, the car go, stay stopped, but is turning to side that was turning before go to new position.

 

I can't explain better, but if u run this script, go to front, and right, in same time, it's will turn to right, when get a great velocity, press SPACE, and you will see whats happened. :D

Link to comment
Share on other sites

This will reset everything. The shocks bounce a bit, but I don't know if that is an issue:

require("Scripts/constants/keycodes")
require("Scripts/linkedlist")
require("Scripts/filesystem")
require("scripts/math/math")
require("scripts/constants/engine_const")

FlushKeys()
HideMouse()

local camera = fw.main.camera

chassis=LoadModel("abstract::vehicle_viperscout.gmf")
carobject=objecttable[chassis]
car=carobject.vehicle

if car==nil then
       Notify("Vehicle object not found.",1)
       chassis:Free()
       return
end

chassis:SetPosition(TFormPoint(Vec3(0,-1,10),fw.main.camera,nil))
chassis:SetRotationf(0,camera.rotation.y+180.0,0)

--Variables
local dx=0.0
local dy=0.0
local camerapitch=camera.rotation.x
local camerayaw=camera.rotation.y
local move=0.0
local strafe=0.0
local steering = 0.0
local torque = 0.0
local steerlimit = 30.0
local steerrate = 2.0
local steerangle=0.0

MoveMouse(Round(GraphicsWidth()/2),Round(GraphicsHeight()/2))

while KeyHit(KEY_ESCAPE)==0 do  

       --Camera look
       gx=Round(GraphicsWidth()/2)
       gy=Round(GraphicsHeight()/2)
       dx=Curve((MouseX()-gx)/4.0,dx,3.0/AppSpeed())
       dy=Curve((MouseY()-gy)/4.0,dy,3.0/AppSpeed())
       MoveMouse(gx,gy)
       camerapitch=camerapitch+dy
       camerayaw=camerayaw-dx
       camerapitch=math.min(camerapitch,90)
       camerapitch=math.max(camerapitch,-90)
       fw.main.camera:SetRotationf(camerapitch,camerayaw,0,1)

       local tirespeed=-(KeyDown(KEY_UP)-KeyDown(KEY_DOWN))*2.0
       car:AddTireTorque(tirespeed,0)
       car:AddTireTorque(tirespeed,1)
       car:AddTireTorque(tirespeed,2)
       car:AddTireTorque(tirespeed,3)

       steermode=0
       if KeyDown(KEY_RIGHT)==1 then steermode=steermode-1 end
       if KeyDown(KEY_LEFT)==1 then steermode=steermode+1 end

       if steermode==1 then
               steerangle=steerangle+4.0*AppSpeed()
       elseif steermode==-1 then
               steerangle=steerangle-4.0*AppSpeed()
       else
               if steerangle>0 then
                       steerangle=steerangle-4.0*AppSpeed()
                       if steerangle<0.0 then steerangle=0.0 end
               else
                       steerangle=steerangle+4.0*AppSpeed()
                       if steerangle>0.0 then steerangle=0.0 end
               end
       end
       steerangle=Clamp(steerangle,-25,25)

       car:SetSteerAngle(steerangle,0)
       car:SetSteerAngle(steerangle,1)

       fw:Update()

       local campos=TFormPoint(Vec3(0,1,0),chassis,nil)

       fw.main.camera:SetPosition(campos)
       fw.main.camera:Move(Vec3(0,0,-10))

       local t=TFormVector(Vec3(0,0,1),camera,chassis)
       a=-math.deg(math.atan2(t.x,t.z))+180.0

       carobject.turret:SetRotationf(0,CurveAngle(a,carobject.turret.rotation.y,10.0/AppSpeed()),0)

       chassis:Hide()
       local pick = LinePick(campos,camera.position,0.5,COLLISION_PROP)
       chassis:Show()
       if pick~=nil then
               camera:SetPosition(pick.position)
       end

       if(KeyHit(KEY_SPACE)==1) then
               chassis:SetPosition(Vec3(0,0,0))
	chassis:SetVelocity(Vec3(0,0,0))
	chassis:SetOmega(Vec3(0,0,0))
       end

       fw:Render()
       Flip(0)
end

chassis:Free()
chassis=nil
camera=nil

ShowMouse()

My job is to make tools you love, with the features you want, and performance you can't live without.

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