Announcement

Collapse
No announcement yet.

Adding collision to a object via C++

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

    Adding collision to a object via C++

    Hi guys.

    I am having trouble with adding collision to a object via C++.

    I am still new to the programming side of things but I am quickly learning. I have checked the DOCs as well but did not see anything helpful'

    Here is my code.

    =====
    Ground.h file:

    // Fill out your copyright notice in the Description page of Project Settings.

    #pragma once

    #include "GameFramework/Actor.h"
    #include "Ground.generated.h"

    UCLASS()
    class CPP_IME_API AGround : public AActor
    {
    GENERATED_BODY()

    public:
    AGround();

    };


    Ground.cpp file:

    // Fill out your copyright notice in the Description page of Project Settings.

    #include "CPP_IME.h"
    #include "Ground.h"


    // Sets default values
    AGround::AGround()
    {

    // Create and position a mesh component so we can see where our sphere is
    UStaticMeshComponent* GroundVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
    GroundVisual->AttachTo(RootComponent);
    static ConstructorHelpers::FObjectFinder<UStaticMesh> GroundVisualAsset(TEXT("/Game/Ground/Ground"));
    if (GroundVisualAsset.Succeeded())
    {
    GroundVisual->SetStaticMesh(GroundVisualAsset.Object);
    GroundVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));
    GroundVisual->SetWorldScale3D(FVector(0.8f));
    }




    //Adding collision
    SetActorEnableCollision(true);
    // Our root component will be a sphere that reacts to physics
    UBoxComponent* BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("RootComponent"));
    RootComponent = BoxComponent;
    BoxComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
    BoxComponent->InitBoxExtent(FVector(50.0f, 50.0f, 50.0f));
    BoxComponent->SetCollisionProfileName(TEXT("Pawn"));


    }

    =====

    Any thoughts?

    Thank you in advance.

    #2
    BoxComponent()->SetCollisionEnabled(ECollisionEnabled::Type::QueryAndPhysics); // set collision type query/physics/query only /physics only
    BoxComponent()->SetCollisionObjectType(ECollisionChannel::ECC_Pawn); // set your collision channel

    // here you set your responces to other channels the channels that you add in project settings are defined as
    ECollisionChannel::ECC_GameTraceChannel from 1 to 18 and ECollisionChannel::ECC_EngineTraceChannel
    where ECollisionChannel::ECC_GameTraceChannel are for objects

    BoxComponent()->SetCollisionResponseToChannel(ECollisionChannel::ECC_GameTraceChannel1, ECollisionResponse::ECR_Overlap);
    BoxComponent()->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Block);
    Last edited by Mlody "Swidwin"; 05-15-2016, 06:55 AM.

    Comment


      #3
      Resolved but I can not see how to close the thread or mark the answer.

      Comment

      Working...
      X