1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Hey Guest, is it this your first time on the forums?

    Visit the Beginner's Box

    Introduce yourself, read some of the ins and outs of the community, access to useful links and information.

    Dismiss Notice

[Solved] Physics Homework meets KAG

Discussion in 'Modding Help' started by Vermilicious, Feb 6, 2016.

  1. Verrazano

    Verrazano Flat Chested Haggy Old Souless Witchy Witch Witch THD Team Global Moderator Forum Moderator Tester
    1. Practitioners of War Extreme Revolution - POWER

    Messages:
    477
    Make sure you are applying it in the same direction as the direction you want it to go. Besides that, dividing the forces by 30 (you should probably be diving the friction force by the same factor as what you divide the intended force by) will likely result in odd behavior, because it's unecessary. I would again highly recommend you not fiddle with addForce, but instead use setVelocity. Though in the real world conveyors are in fact modeled by forces even though they keep you at a constant velocity, it's not very practical to do this in code, when you have access to skip all those steps, via setVelocity.

    You can see solutions to issues like this on other game forums http://www.gamasutra.com/blogs/MarkHogan/20141103/229201/Easy_Conveyor_Belt_Physics_in_Unity.php

    http://phaser.io/examples/v2/box2d/conveyor-belt < this is a box2d example that uses setTangentSpeed, you don't have access to this, but it is just setting velocity in the direction you want it to move.
     
  2. Vermilicious

    Vermilicious Ballista Bolt Thrower

    Messages:
    232
    The alternative to dividing by framerate/tickrate is to simply perform it only every 30th frame or so, which is certainly better performance-wise, but less responsive. I don't see what should cause odd behavior; the force to counter friction is only applied if the object is not moving. The problem, I believe, with simply manipulating velocity, is that player characters moving across the belt by actively holding directional keys, will cause strange behavior.

    I might be wrong, but trial and error plus the tips I received here led me up to this. I didn't expect having to fiddle so much with this.

    Edit: Well, this is embarrassing. It was, apparently, so simple:
    Code:
    //Check if conveyor is facing left
    if(isFacingLeft) {
    
      //Set horizontal velocity in left direction
      targetVelocity.x *= -1.0f;
    
      //Set target velocity or current, whichever is higher (to the left)
      nearbyBlob.setVelocity(Vec2f(Maths::Min(targetVelocity.x, currentVelocity.x), currentVelocity.y));
    
    }
    
    //Otherwise, facing right
    else {
    
      //Set target velocity or current, whichever is higher (to the right)
      nearbyBlob.setVelocity(Vec2f(Maths::Max(targetVelocity.x, currentVelocity.x), currentVelocity.y));
    
    }
    [​IMG]
     
    Last edited: Feb 19, 2016