Announcement

Collapse
No announcement yet.

Add Force

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Add Force

    I have a weird problem that needs an equally weird solution. Basically, I am trying to have my player land on a platform while retaining 'overlap all' for the player and the platform (Paper2D, but shouldn't matter).

    Code:
    CapsuleComponent->AddForceAtLocation(FVector(0, 0, GetVelocity().Z * -1), GetActorLocation());
    Shouldn't this counteract the Z gravity and hold the player in place while it runs on the tick?

    I asked a similar question on the answer hub: https://answers.unrealengine.com/que...-question.html

    Code:
    void ATestPlatCharacter::Tick(float DeltaSeconds){
    	Super::Tick(DeltaSeconds);
    	GEngine->ClearOnScreenDebugMessages();
    
    	FVector coords = GetActorLocation();
    	x = coords.X;
    	z = coords.Z;
    	depth = coords.Y;
    
    	if (place_meeting<APlatformParent>(x,z - 1)) {
    		free = 0;
    		
    	}
    	else {
    		free = 1;
    	}
    
    	if (free == 0) {
    		/* Does not work. */
    		//CapsuleComponent->SetEnableGravity(false);
    		//CapsuleComponent->AddImpulseAtLocation(FVector(0, 0, GetVelocity().Z * -1), GetActorLocation());
    		//CapsuleComponent->AddForceAtLocation(FVector(0, 0, GetVelocity().Z * -1), GetActorLocation());
    		//CapsuleComponent->ComponentVelocity = FVector(0, 0, 0);
    		//CapsuleComponent->SetEnableGravity(false);
    		//bSimGravityDisabled = true;
    
    		/* Kinda works but doesnt'. */
    		//CharacterMovement->Velocity = FVector(0, 0, 0);
    		//CharacterMovement->Velocity.Z = 0;
    
    		/* This tries to work, but then there is no friction. Perhaps compensate? */
    		//CharacterMovement->GravityScale = 0.0f;
    		//CharacterMovement->Velocity.Z = 0;
    
    		CapsuleComponent->AddForceAtLocation(FVector(0, 0, GetVelocity().Z * -1), GetActorLocation());
    		
    	}
    	else {
    		//The character is not on the ground.
    	}
    
    	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::SanitizeFloat(GetVelocity().Z));
    	//GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString("free = ") + FString::FromInt(free));
    }

    #2
    shouldnt it be GlobalGravityZ * character mass or something like that
    tegleg.co.uk - indie electronic music label
    Android + HTML5 WIP Physics Game
    PC Games - Android Apps

    Comment


      #3
      Originally posted by tegleg View Post
      shouldnt it be GlobalGravityZ * character mass or something like that
      Code:
      CapsuleComponent->AddForceAtLocation(FVector(0, 0, (GetWorld()->GetGravityZ() * CapsuleComponent->GetMass()) * -1), GetActorLocation());
      This didn't do it, but I appreciate the reply anyhow.

      Comment


        #4
        Force = mass * acceleration. Acceleration = change in velocity / time. In your case, acceleration = -1*velocity.z / deltatime. Force = acceleration * mass. SO... You need to multiply what you have with the character's mass and deltatime. Using gravity wouldn't work because it would only stop gravity, but you would still keep falling at the same speed as when gravity was turned off.

        Check out my discord -> https://discord.gg/kQdVwJ3

        Follow us on twitter to get updates on new products and special offers -> https://twitter.com/BlackFangTech

        Black Fang Technologies' products -> https://www.unrealengine.com/marketp...20Technologies

        Comment


          #5
          Originally posted by BlackRang666 View Post
          Force = mass * acceleration. Acceleration = change in velocity / time. In your case, acceleration = -1*velocity.z / deltatime. Force = acceleration * mass. SO... You need to multiply what you have with the character's mass and deltatime. Using gravity wouldn't work because it would only stop gravity, but you would still keep falling at the same speed as when gravity was turned off.
          On top of that, I can't turn off gravity anyhow (it messes with friction). This should have been obvious, but my brain has been fried converting a project from Game Maker. I tried what you said, and the character is still plowing right through the platform.

          Code:
          CapsuleComponent->AddForceAtLocation(FVector(0, 0, (-1 * (GetVelocity().Z / DeltaSeconds)) * CapsuleComponent->GetMass()), GetActorLocation());
          Am I doing this right? Basically, I am trying to apply an equal force against gravity to the character while it is contacting the platform to simulate a solid contact. I have all the collision code and such worked out, this is the only piece holding us back.

          I tried AddForce as well:

          Code:
          CapsuleComponent->AddForce(FVector(0, 0, (-1 * (GetVelocity().Z / DeltaSeconds)) * CapsuleComponent->GetMass()));
          Thanks for replying, it is very appreciated.
          Last edited by The Britain; 12-06-2014, 11:22 PM.

          Comment


            #6
            It doesn't even slow down? Maybe you should add force to the movement component instead? IDK.

            Check out my discord -> https://discord.gg/kQdVwJ3

            Follow us on twitter to get updates on new products and special offers -> https://twitter.com/BlackFangTech

            Black Fang Technologies' products -> https://www.unrealengine.com/marketp...20Technologies

            Comment


              #7
              Originally posted by BlackRang666 View Post
              It doesn't even slow down? Maybe you should add force to the movement component instead? IDK.
              Alright, some progress, the following line of code will cause the character to bounce for some reason, and then slowly make its way into the ground. Also, friction is not being applied again.

              Code:
              CharacterMovement->AddForce(FVector(0, 0, (-1 * (GetVelocity().Z / DeltaSeconds)) * CapsuleComponent->GetMass()));

              Comment


                #8
                Shouldn't it be * DeltaTime instead of /?

                Comment


                  #9
                  Originally posted by SmittyJ View Post
                  Shouldn't it be * DeltaTime instead of /?
                  I think it would be velocity divided by time for that part. This solution will not work anyhow, as it makes you float (as it should) and nulls out friction. I was unaware of how advanced the collision channels were in UE4. So I am instead going to try a raycast and setting the character collision channel against world statics to see if I can get the same effect minus the headache.

                  Comment


                    #10
                    I got it working, and I will be posting a tutorial here soon. Thank you all for your help.

                    Comment


                      #11
                      Using custom collision channels solved our problem:

                      Answer here:
                      https://forums.unrealengine.com/show...forms&p=189343

                      Thanks to BadderThanBad for pointing me in the right direction.

                      Comment

                      Working...
                      X