aiaf Posted January 27, 2017 Share Posted January 27, 2017 Initial model position: Vec3(1, 2, 0) TargetDirection vector: Vec3(1, 4, 0) When model arrives at certain position i want to send it back the same way. Force is applied: model->AddForce(targetDirection[0], targetDirection[1], targetDirection[2], true); if (model->GetPosition(true).DistanceToPoint(Vec3(targetDirection[0], targetDirection[1], targetDirection[2])) < 0.2 && back == false) { model->SetVelocity(Vec3(0), true); model->AddForce(targetDirection.Inverse(), true); back = true; } if (model->GetPosition(true).DistanceToPoint(startPosition) < 0.2 && back == true) { model->SetVelocity(Vec3(0), true); model->AddForce(targetDirection, true); back = false; } I expected this to work and have the box bouncing between the 2 points. If i set condition for distance to be < 0.5 will work, but not with 0.2 value. I dont understand why this happens, is something wrong with the way i do AddForce ? sorry if this will be stupid question but dont know what i can do Quote I made this with Leadwerks/UAK: Structura | Stacky Desktop Edition Website: Binary Station Link to comment Share on other sites More sharing options...
macklebee Posted January 28, 2017 Share Posted January 28, 2017 You are evaluating a force vector against a Vec3 position based on the distance between their respective Vec3 values? Seems like there would be a lot of things that could affect the outcome on whether or not this worked correctly. Since the model is a physics object, it can be affected by other physics forces in the scene which could make the distance check fail. What are you setting the mass of the model to since that will affect how far your force (targetDirection) will move it? Also, the inverse of targetDirection is Vec3(-1,-4,0) which when used as a force will try to force the model down and to the negative X axis. Since it is a physics object, I assume the model is sitting on the ground so a negative Y force will not work. If you are really wanting to use a physics object then i would try a more generic approach for alternating the force values without looking at positions. example: window = Window:Create() context = Context:Create(window) world = World:Create() camera = Camera:Create() camera:SetPosition(0,2,-4) camera:SetRotation(0,0,0) light = DirectionalLight:Create() light:SetRotation(35,35,0) ground = Model:Box(10,1,10) ground:SetColor(0,1,0) shape = Shape:Box(0,0,0, 0,0,0, 10,1,10) ground:SetShape(shape) ground:SetCollisionType(Collision.Scene) ground:SetPosition(0,-0.5,0) shape:Release() model = Model:Box() model:SetColor(1,0,0) model:SetPosition(0,0.5,0) shape = Shape:Box(0,0,0, 0,0,0, 1,1,1) model:SetShape(shape) model:SetCollisionType(Collision.Prop) shape:Release() model:SetMass(1) Force = Vec3(100,400,0) back = true time = Time:Millisecs() while window:KeyDown(Key.Escape)==false do if window:Closed() then break end if Time:Millisecs() > time + 1000 then if back == true then model:AddForce(Force, true) back = false else model:AddForce(Vec3(Force.x*-1,Force.y,Force.z), true) back = true end time = Time:Millisecs() end Time:Update() world:Update() world:Render() context:Sync(true) end If you are just trying to accomplish a bouncing back and forth object, maybe an animated object would suit your needs better? Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
aiaf Posted January 28, 2017 Author Share Posted January 28, 2017 The model is not sitting on the ground, can go in any direction, model->SetMass(0.09); model->SetGravityMode(false); Model need to arrive to another entity that is moving , may encounter moving obstacles that will try to avoid.Then need to come back to the original position. Thats why i tried to use physics, then most basic step is what i tried with distances. Guess i could try with animated objects and have some kind of graph to create dynamically paths that wont intersect each other. Quote I made this with Leadwerks/UAK: Structura | Stacky Desktop Edition Website: Binary Station Link to comment Share on other sites More sharing options...
aiaf Posted February 5, 2017 Author Share Posted February 5, 2017 I used PhysicsSetPosition to do the forward/back animation. if(forward) model->PhysicsSetPosition(targetPosition.x, targetPosition.y, targetPosition.z, 0.01); if(!forward) model->PhysicsSetPosition(startPosition.x, startPosition.y, startPosition.z, 0.01); if (forward && model->GetPosition(true).DistanceToPoint(targetPosition) < 0.05) { forward = false; } if (!forward && model->GetPosition(true).DistanceToPoint(startPosition) < 0.05) { forward = true; } Quote I made this with Leadwerks/UAK: Structura | Stacky Desktop Edition Website: Binary Station Link to comment Share on other sites More sharing options...
Recommended Posts
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.