Don't understand these C++ box2d Errors - c++

I've trying to get this C++ method to work in my iOS game.
-(b2Vec2) RayCheckWithInput:p1 andX:p2
{
b2RayCastInput input;
input.p1 = p1;
input.p2 = p2;
input.maxFraction = 1;
//check every fixture of every body to find closest
float closestFraction = 1; //start with end of line as p2
b2Vec2 intersectionNormal(0,0);
for (b2Body* b = self.world.world->GetBodyList(); b; b = b->GetNext()) {
for (b2Fixture* f = b->GetFixtureList(); f; f = f->GetNext()) {
b2RayCastOutput output;
if ( ! f->RayCast( &output, input ) )
continue;
if ( output.fraction < closestFraction ) {
closestFraction = output.fraction;
intersectionNormal = output.normal;
}
}
}
b2Vec2 intersectionPoint = p1 + closestFraction * (p2 - p1);
return intersectionPoint;
}
I know a fair bit about obj-c, but not C++. Here's a screen shot of the errors for that method it's showing.
Any help appreciated (in Chrome you can do right click and open image in new tab to see it better)

You need to be more explicit with the variables passed into the Objective-C function. You are passing without a type so it is not able to infer that it is actually a C++ type rather than an Objective-C type.
Try something like:
-(b2Vec2) RayCheckWithInput: (CPPTye*)p1 andX: (CPPTye*)p2
Edit: a quick check of the box2d docs says you need something like this:
-(b2Vec2) RayCheckWithInput: (b2Vec2)p1 andX: (b2Vec2)p2
Edit 2: Also worth noting that the Raycast function takes a third parameter childIndex as described in the docs.

Related

Spawning Actor from array of classes UE5

I'm attempting to use SpawnActor() to spawn random actors from an array of classes. I've spent several years using UE4/UE5 blueprints, but this is my first time working in cpp in a long time. The trouble I'm running into is with the SpawnActor function, maybe I'm hooking up the parameters wrong.
Below is the code for my spawning logic in my cpp file:
void ADS_Generator::SpawnFlora()
{
FHitResult CurrentHit;
FActorSpawnParameters SpawnInfo;
for (int F = 0; F < FloraIterations; ++F)
{
if (SpawnedFlora.Num() <= MaxFlora)
{
CurrentHit = SpawnLineTrace(Vertices[FMath::RandRange(0, Vertices.Num() - 1)]);
if (CurrentHit.bBlockingHit == true)
{
if (FMath::RandRange(1, 100) >= FloraTolerance)
{
AActor* CurrentFlora = GetWorld()->SpawnActor(Flora[0], CurrentHit.ImpactPoint, FVector(0, 0, FMath::RandRange(-180, 180)), SpawnInfo);
SpawnedFlora.Add(CurrentFlora);
}
}
}
}
}
Below is the code for my header file where I declare the array of actor classes:
UPROPERTY(EditAnywhere)
TArray<TSubclassOf<class AActor>> Flora;
UPROPERTY(EditAnywhere)
TArray<TSubclassOf<class AActor>> Fauna;
The specific error I'm getting is this:
"C++ no instance of overloaded function matches the argument list
argument types are: (TSubclassOf<AActor>, FVector_NetQuantize, FVector, FActorSpawnParameters)
object type is: UWorld"
I've tried adding "::StaticClass()" as well as using "->StaticClass" to no avail.
Any thoughts or advice? I definitely need to watch some videos and brush up on my cpp, I'm very rusty.
Note: I'm using Flora[0] as a placeholder instead of randomizing the array index just to test.
Found the issue! The problem was my Rotation was defined as a plain FVector, but it needed to be converted to a rotation and then a quaternion, respectively. Below is the code, but fixed:
void ADS_Generator::SpawnFlora()
{
FHitResult CurrentHit;
FActorSpawnParameters SpawnInfo;
for (int F = 0; F < FloraIterations; ++F)
{
if (SpawnedFlora.Num() <= MaxFlora)
{
CurrentHit = SpawnLineTrace(Vertices[FMath::RandRange(0, Vertices.Num() - 1)]);
if (CurrentHit.bBlockingHit == true)
{
UE_LOG(LogTemp, Warning, TEXT("HIT!"));
if (FMath::RandRange(1, 100) >= FloraTolerance)
{
FTransform SpawnLoc;
SpawnLoc.SetLocation(CurrentHit.ImpactPoint);
SpawnLoc.SetRotation((FVector(0, 0, FMath::RandRange(-180, 180)).Rotation().Quaternion()));
SpawnLoc.SetScale3D(FVector(1, 1, 1));
AActor* CurrentFlora = GetWorld()->SpawnActor<AActor>(Flora[0]->StaticClass(), SpawnLoc, SpawnInfo);
SpawnedFlora.Add(CurrentFlora);
UE_LOG(LogTemp, Warning, TEXT("Spawned Flora!"));
}
}
else if(CurrentHit.bBlockingHit == false)
{
UE_LOG(LogTemp, Warning, TEXT("NO HIT"));
}
}
}
}
TLDR; Locations and Rotations are stored differently and I totally forgot.

Camera Shake Not Activating Unreal C++

I am trying to set a camera shake for my character in Unreal C++.
Currently, I declare the instance variable in my header class.
Using log outputs, the code is reaching the right location in the OnFire() function, meaning none of the pointers are null. But the camera shake doesn't work.
UPROPERTY(EditAnywhere)
UCameraShake* CShake;
// And here is how I call it
void AZombieCharacter::BeginPlay(){
SetUpCameraShake();
}
...
void AZombieCharacter::OnFire() {
...
auto CTRLR = UGameplayStatics::GetPlayerController(GetWorld(), 0);
if (CTRLR) {
auto CM = CTRLR->PlayerCameraManager;
if (CM) {
CM->PlayCameraShake(CShake->GetClass(), 1.0f);
}
else {
UE_LOG(LogTemp, Warning, TEXT("CANNOT FIND CM"));
}
}
else {
UE_LOG(LogTemp, Warning, TEXT("CANNOT FIND CTRLR"));
}
}
...
void AZombieCharacter::SetUpCameraShake() {
CShake = UCameraShake::StaticClass()->GetDefaultObject<UCameraShake>();
CShake->OscillationDuration = 4.f;
CShake->OscillationBlendInTime = 0.5f;
CShake->OscillationBlendOutTime = 0.5f;
CShake->RotOscillation.Pitch.Amplitude = FMath::RandRange(5.0f, 10.0f);
CShake->RotOscillation.Pitch.Frequency = FMath::RandRange(25.0f, 35.0f);
CShake->RotOscillation.Yaw.Amplitude = FMath::RandRange(5.0f, 10.0f);
CShake->RotOscillation.Yaw.Frequency = FMath::RandRange(25.0f, 35.0f);
}
Thanks.
Play Camera Shake Accepts an UClass / TSubClassOf<UCameraShake> knowing that would expect us to think that this function would eventually construct a new object and ignore the default object options.
I would recommend to subclass the UCameraShake class and use TSubClassOf to store your class type instead of the pointer like CShake. As it may be garbage collected without you knowing who knows?
Here is a C++ tutorial on UCameraShake's.
https://unrealcpp.com/camera-shake/
The reason your shake is not playing is likely having no actual parameters passed to the shake. Only the default ones despite your try to set the default values, it does not seem to take it into account for newly created objects.

Unable to pass the string variable to Sprite in cocos2dx

I'm having problem with the strings in cocos2dx & C++. I want to pass the variable background to Sprite::create(background) however, I get an error. If it was in java the following code will work, but since I'm not used to C++ it may be different. Plus, if it was a int how will I pass it ? How will I be able to solve this? Some tips or samples will be great! I will love to hear from you!
void GameLayer::initBackground()
{
UserDefault *_userDef = UserDefault::getInstance();
//int型
auto _int =_userDef->getIntegerForKey("back");
auto string background = "Background1.png";
if (_int == 0) {
background = "Background2.png";
}
auto bgForCharacter = Sprite::create(background);
bgForCharacter->setAnchorPoint(Point(0, 1));
bgForCharacter->setPosition(Point(0, WINSIZE.height));
addChild(bgForCharacter, ZOrder::BgForCharacter);
auto bgForPuzzle = Sprite::create("Background2.png");
bgForPuzzle->setAnchorPoint(Point::ZERO);
bgForPuzzle->setPosition(Point::ZERO);
addChild(bgForPuzzle, ZOrder::BgForPuzzle);
}
auto userDefault=UserDefault::getInstance();
int value=userDefault->getIntegerForKey("back"); //find value for back if exist then it return that value else return 0
auto sprite = Sprite::create(value==0?"Background2.png":"Background1.png");
sprite->setPosition(100, 100);
this->addChild(sprite, 0);
When you want to change your background just put any value rather than 0
UserDefault::getInstance()->setIntegerForKey("back", 1);

Maya API - Move CV with Kinect

I’m trying to create a 64-bit plug-in for Autodesk Maya 2013. The aim of this plug-in is to be able to model objects through the Kinect.
In context, with the plug-in you can select the CV of a created object. To specify you want to move the selected points, the position of selected CV is updated constantly.
Here is my problem, when I try to update the position of a selected CV of an object, the result is not displayed on the screen.
I have tried many ways without getting any result, as an example:
1) when I do the setPosition the point doesn’t update.
for ( ; !iter.isDone(); iter.next() ) {
iter.getDagPath( mdagPath, mComponent );
MItSurfaceCV sCvFn( mdagPath, mComponent, true, &stat );
if ( MS::kSuccess == stat ) {
for ( ; !sCvFn.isDone(); sCvFn.nextRow() ) {
for ( ; !sCvFn.isRowDone(); sCvFn.next() ) {
MPoint pto(pHand.posX, pHand.posY, pHand.posZ);
sCvFn.setPosition(pto, MSpace::kWorld);
}
}
}
sCvFn.updateSurface();
}
2) in this case, if you iterate over the array newCVS before doing the setCVs you can see the updated points.
But then, when doing setCVs and looking for the position in the CV array of the object (doing a getCVs) the position of the modified CV doesn’t change.
for ( ; !iter.isDone(); iter.next() ) {
stat = iter.getDagPath( mdagPath, mComponent );
if(stat == MS::kSuccess) {
MFnNurbsSurface surf(mdagPath);
MItSurfaceCV sCvFn( mdagPath, mComponent, true, &stat );
MPointArray currentCV;
surf.getCVs(currentCV, MSpace::kWorld);
MPointArray newCVS(currentCV.length(), MPoint::origin);
newCVS.setLength(currentCV.length());
for(unsigned i = 0; i < currentCV.length(); i++){
newCVS[i] = newCVS[i] + currentCV[i];
}
int index;
if ( MS::kSuccess == stat ) {
for ( ; !sCvFn.isDone(); sCvFn.nextRow() ) {
for ( ; !sCvFn.isRowDone(); sCvFn.next() ) {
sCvFn.getIndex(indexU, indexV);
int posArray = indexU * surf.numCVsInV() + indexV;
index = posArray;
MVector diff = MPoint(pHand.posX, pHand.posY, pHand.posZ) - currentCV[posArray];
newCVS[posArray] = newCVS[posArray] + diff;
}
}
surf.setCVs(newCVS, MSpace::kWorld);
surf.updateSurface();
}
}
3) When having CV selected in Maya, I tried to move this points with MEL commands. For this, I use the “MGlobal::executeCommand” function, but still nothing happens.
Finally, I changed executeCommand to executeCommandOnIdle. In this case, the object change correctly but the procedure crashes, perhaps, because this procedure is executed many times per second.
I would appreciate if anyone can help me with this problem or knows something about this.

NSInvocation not passing pointer to c++ array

I think I'm making just a fundamental mistake, but I cannot for the life of me see it.
I'm calling a method on an Objective-C object from within a C++ class (which is locked). I'm using NSInvocation to prevent me from having to write hundreds methods just to access the data in this other object.
These are the steps I'm going through. This is my first call, and I want to pass s2. I can't really provide a compilable example, but hopefully it's just a DUHRRRRR problem on my part.
float s2[3];
id args2s[] = {(id)&_start.x(),(id)&_start.y(),(id)&s2};
_view->_callPixMethod(#selector(convertPixX:pixY:toDICOMCoords:),3,args2s);
This is the View method being called
invokeUnion View::_callPixMethod(SEL method, int nArgs, id args[])
{
DataModule* data;
DataVisitor getdata(&data);
getConfig()->accept(getdata);
invokeUnion retVal;
retVal.OBJC_ID = data->callPixMethod(_index, _datasetKey, method, nArgs, args);
return retVal;
}
Invoke Union is a union so I can get the float value returned by NSInvocation.
union invokeUnion {
id OBJC_ID;
int intValue;
float floatValue;
bool boolValue;
};
This is the method in the data Object (pthread locked with lock() and unlock());
id DataModule::callPixMethod(int index, std::string predicate, SEL method, int nArgs, id args[] )
{
// May Block
DCMPix *pix =[[getSeriesData(predicate) pix] objectAtIndex:index];
lock();
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMethodSignature *signature;
NSInvocation *invocation;
signature = [DCMPix instanceMethodSignatureForSelector:method];
invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:method];
[invocation setTarget:pix];
if (nArgs > 0) for (int n = 0; n < nArgs; n++) {
SFLog(#"invocation: i=%d, *ptr=0x%x, valf=%f, vald=%d",n,args[n],*args[n],*args[n]);
[invocation setArgument:args[n] atIndex:2+n];
}
id retVal;
[invocation invoke];
[invocation getReturnValue:&retVal];
[pool release];
unlock();
return retVal;
}
The method in the DCMPix object (which I can't modify, it's part of a library) is the following:
-(void) convertPixX: (float) x pixY: (float) y toDICOMCoords: (float*) d pixelCenter: (BOOL) pixelCenter
{
if( pixelCenter)
{
x -= 0.5;
y -= 0.5;
}
d[0] = originX + y*orientation[3]*pixelSpacingY + x*orientation[0]*pixelSpacingX;
d[1] = originY + y*orientation[4]*pixelSpacingY + x*orientation[1]*pixelSpacingX;
d[2] = originZ + y*orientation[5]*pixelSpacingY + x*orientation[2]*pixelSpacingX;
}
-(void) convertPixX: (float) x pixY: (float) y toDICOMCoords: (float*) d
{
[self convertPixX: x pixY: y toDICOMCoords: d pixelCenter: YES];
}
It's crashing when it tries to access d[0]. BAD_EXC_ACCESS which I know means it's accessing released memory, or memory outside of it's scope.
I'm getting lost keeping track of my pointers to pointers. the two float values come across fine (as does other info in other methods) but this is the only one asking for a float* as a parameter. From what I understand the convertPixX: method was converted over from a C program written for Mac OS 9... which is why it asks for the c-array as an out value... I think.
Anyway, any insight would be greatly appreciated.
I've tried sending the value like this:
float *s2 = new float[3];
void* ps2 = &s2;
id args2s[] = {(id)&_start.x(),(id)&_start.y(),(id)&ps2};
_view->_callPixMethod(#selector(convertPixX:pixY:toDICOMCoords:),3,args2s);
But that gives a SIGKILL - plus I'm sure it's bogus and wrong. ... but I tried.
anyway... pointers! cross-language! argh!
Thanks,
An array is not a pointer. Try adding the following line
NSLog(#"%p, %p", s2, &s2);
just above.
id args2s[] = {(id)&_start.x(),(id)&_start.y(),(id)&s2};
s2 and &s2 are both the address of the first float in your array, so when you do:
[invocation setArgument:args[n] atIndex:2+n];
for n = 2, you are not copying in a pointer to the first float, but the first float, possibly the first two floats if an id is 64 bits wide.
Edit:
To fix the issue, this might work (not tested).
float s2[3];
float* s2Pointer = s2;
id args2s[] = {(id)&_start.x(),(id)&_start.y(),(id)&s2Pointer};
_view->_callPixMethod(#selector(convertPixX:pixY:toDICOMCoords:),3,args2s);
s2Pointer is a real pointer that will give you the double indirection you need.