How to move actor with collision in c++?

Hello!
The problem is that the actors move through walls.
If I add ProjectileMovement component in editor end set Initial Speed then collision done:

52297-detail_collision.jpg

52299-done.jpg

But if I try some code then the actors move through each other:

HitMovement = Cast<UProjectileMovementComponent>(HitActor->GetComponentByClass(UProjectileMovementComponent::StaticClass()));
HitMovement->Velocity = (NewLocation - HitActor->GetActorLocation())*4;
/* or
HitActor->SetActorLocation(NewLocation, true) ;
*/

52298-error.jpg

Thanks in advance.

Can you provide more code for your initialization of the UProjectileMovementComponent ? From the snippet you provided, it looks like you are trying to move actor’s location. What you want to do when you instantiate a projectile movement component is to provide the initial velocity as a vector. Once this is done, in the UProjectileMovementComponent::TickComponent function you will see that moving the UpdatedComponent takes place with calls to UMovementComponent::SafeMoveUpdatedComponent and UMovementComponent::MoveUpatedComponent respectively depending on whether or not the projectile can be deflected.

These functions perform a sweep of the movement delta on the UpdatedComponent in order to determine if they hit anything along the way, and they will fill out the HitResult with impact data.

All you should need to do is give it initial velocity and let it loose. If you do want to move it arbitrarily then use SafeMoveUpdatedComponent and MoveUpdatedComponent as needed.

		// Move the component
		if (bShouldBounce)
		{
			// If we can bounce, we are allowed to move out of penetrations, so use SafeMoveUpdatedComponent which does that automatically.
			SafeMoveUpdatedComponent( MoveDelta, NewRotation, true, Hit );
		}
		else
		{
			// If we can't bounce, then we shouldn't adjust if initially penetrating, because that should be a blocking hit that causes a hit event and stop simulation.
			TGuardValue<EMoveComponentFlags> ScopedFlagRestore(MoveComponentFlags, MoveComponentFlags | MOVECOMP_NeverIgnoreBlockingOverlaps);
			MoveUpdatedComponent(MoveDelta, NewRotation, true, &Hit );
		}

Thanks for the answer.
I’m trying to move the mouse cubes of the player controller.
In *.h

	TWeakObjectPtr<class AActor> HitActor;
	TWeakObjectPtr<class UProjectileMovementComponent> HitMovement;

In *.cpp

GetHitResultUnderCursor(ECC_Visibility, false, Hit);
HitActor = Hit.GetActor();`

`HitMovement = Cast(HitActor->GetComponentByClass(UProjectileMovementComponent::StaticClass()));

then in PlayerTick

GetHitResultUnderCursor(ECC_Visibility, false, Hit);
FVector NewLocation = Hit.ImpactPoint;
HitMovement->Velocity = (NewLocation - HitActor->GetActorLocation())*4;
/* or
 HitMovement->SafeMoveUpdatedComponent
				(
				NewLocation - HitActor->GetActorLocation(),
				HitActor->GetActorQuat(),
				true,
				HitMove
				); 
*/

But always actors move through each other.
I tried to set HitMovement->Velocity. I can see how the cube is moving but without collisions.

I’ll see if I can replicate the issue tonight. In the meantime, if you figure out the solution, remember to post the answer.

No, I can’t figure out the solution.
If I use ProjectileMovementComponent->Velocity or ProjectileMovementComponent->SafeMoveUpdatedComponent the actor moving, but collision ignored.

Maybe there’s another way to move the actors?

I have projectiles working in a few personal projects but not in the same use case. Until i get home. Cant test my theory but i do know that if the collision component is simulating physics then the physics sim takes over for the ballistic component after initial velocity is set. Have you tried spawning a projectile from the camera and shooting it at your cube rather than moving a cube with a projectile compnent which has turned over sim duties to the physics core?

Edit… meant to post this as a comment will fix when not on phone

Dear ZeroParadigm !
I found a bug in my code: I myself canceled collision for cube.
I’m very embarrassed that I took your time. Sorry.
Thanks for the help.

No worries. Post the offending line and the solution as an answer so others with the same problem can see your solution. =]

That’s my fault. I myself am disabled collisions

HitActor->SetActorEnableCollision(false);

It is necessary to enable the collision:

HitActor->SetActorEnableCollision(true);

Then the actor can be moved with collision:

HitActor->SetActorLocation(NewLocation, true) ;