SCNPhysicsVehicle, wheel are rotate 90 deg - scenekit

I'm try to study Scenekit and SCNPhysicsVehicle, I created a simple car as per picture below:
i wrote the following code that should load the physics to the model and place it to the scene.
func setupVeicles(nodePos: SCNVector3){
// load file usdz
let truck = loadAssetWithName(nameFile: "car", nameNode: "car", type: "usdz", scale: SCNVector3(1, 1, 1))
let chassie = truck.childNode(withName: "Chassie", recursively: true)!
// add chassie at position touch
chassie.position = SCNVector3(nodePos.x, nodePos.y+0.1, nodePos.z)
//Set the physic body
let body = SCNPhysicsBody.dynamic()
body.physicsShape = SCNPhysicsShape(node: chassie)
body.categoryBitMask = BodyType.car.rawValue //2 int, assegnato solo a quel oggetto
body.collisionBitMask = BodyType.floor.rawValue //1 con cosa puo collidere
body.contactTestBitMask = BodyType.floor.rawValue //1 attiva il delegato
body.allowsResting = false
body.mass = 5
body.restitution = 0.1
body.friction = 0.5
body.rollingFriction = 0
chassie.physicsBody = body
// Load the wheel
let wheelFL = chassie.childNode(withName: "WheelFL", recursively: true)!
let wheelFR = chassie.childNode(withName: "WheelFR", recursively: true)!
let wheelBL = chassie.childNode(withName: "WheelBL", recursively: true)!
let wheelBR = chassie.childNode(withName: "WheelBR", recursively: true)!
// test rotate , but not work
// wheelFL.eulerAngles = SCNVector3(deg2rad(90), 0, 0)
// wheelPhysic
let phywheelFL = createPhysicsVehicleWheel(wheelNode: wheelFL, position: SCNVector3(-0.212, -0.085, 0.146))
let phywheelFR = createPhysicsVehicleWheel(wheelNode: wheelFR, position: SCNVector3(-0.212, -0.085, -0.15))
let phywheelBL = createPhysicsVehicleWheel(wheelNode: wheelBL, position: SCNVector3(0.182, -0.085, 0.15))
let phywheelBR = createPhysicsVehicleWheel(wheelNode: wheelBR, position: SCNVector3(0.182, -0.085, -0.15))
let physicsVehicle = SCNPhysicsVehicle(chassisBody: chassie.physicsBody!, wheels: [phywheelFL,phywheelFR,phywheelBL,phywheelBR])
self.arView.scene.physicsWorld.addBehavior(physicsVehicle)
self.arView.scene.rootNode.addChildNode(chassie)
}
func createPhysicsVehicleWheel(wheelNode: SCNNode, position: SCNVector3) -> SCNPhysicsVehicleWheel {
let wheel = SCNPhysicsVehicleWheel(node: wheelNode)
wheel.connectionPosition = position
wheel.axle = SCNVector3(x: -1.0, y: 0, z: 0)
wheel.maximumSuspensionTravel = 4.0
wheel.maximumSuspensionForce = 100
wheel.suspensionRestLength = 0.08
wheel.suspensionDamping = 2.0
wheel.suspensionStiffness = 2.0
wheel.suspensionCompression = 4.0
wheel.radius = 0.04
wheel.frictionSlip = 0.9
return wheel
}
Can't understand why my wheel are rotate 90 deg as you cans se on the picture:
Why this wheel are like this? how to rotate them.. I tried to rotate the node but nothing happen..

Download the file rc_car.dae from the Scenekit Demo App that Apple provided in the past.
https://github.com/NXAristotle/appleSample/tree/master/SceneKitVehicleDemo/SceneKitVehicle/resources
You will see, that the geometry is a bit more complex, containing subnodes on the wheels. I ran into a similar issue. Then I used Blender to adapt the geometry the same way as it is in rc_car.dae to my own model. It was quite a bit of work.
Export it to DAE and use it in SceneKit.
If you need code examples, I can provide, but what you made looks quite good to me.

Related

Babylon js texture issue when used with reactjs

I'm implementing 3D demo application using Babylonjs library for 3D Demo.I'm importing 3D model from S3 and adding texture image on top of material in Reactjs.
But when i add texture image on top of material, rest of area on 3D model gets black color and i want get rid of it. Code works fine in Babylon playground but fails in React app.
Here is the source code
var mat = new BABYLON.CustomMaterial("mat", scene);
mat.diffuseTexture = new BABYLON.Texture(textureImage, scene, false, false);
materialedMeshes.forEach(mesh => mesh.material = mat);
mat.emissiveColor = new BABYLON.Color3(1, 1, 1);
// mat.diffuseColor = new BABYLON.Color3(1, 0, 1);
// mat.specularColor = new BABYLON.Color3(0.5, 0.6, 0.87);
// mat.emissiveColor = new BABYLON.Color3(1, 1, 1);
// mat.ambientColor = new BABYLON.Color3(0.23, 0.98, 0.53);
mat.diffuseTexture.uOffset = -0.1000;
mat.diffuseTexture.vOffset = -1.1800;
mat.diffuseTexture.uScale = 1.2200;
mat.diffuseTexture.vScale = 2.2200;
mat.diffuseTexture.uAng = Math.PI;
mat.diffuseTexture.wrapU = BABYLON.Constants.TEXTURE_CLAMP_ADDRESSMODE;
mat.diffuseTexture.wrapV = BABYLON.Constants.TEXTURE_CLAMP_ADDRESSMODE;
mat.Fragment_Custom_Alpha(`
if (baseColor.r == 0. && baseColor.g == 0. && baseColor.b == 0.) {
baseColor.rgb = vec3(0.85, 0.85, 0.85);
}
baseColor.rgb = mix(vec3(0.85, 0.85, 0.85), baseColor.rgb, baseColor.a);
`)

Adding a movieclip in each square of my calendar (AS3)

I've got a calendar with squares in wich each date is wrote :
var myArray:Array = new Array();
var row:Number = 0;
var moonNum:Number;
var holder_txt:MovieClip = new MovieClip;
addChild(holder_txt);
holder_txt.x = 35;
holder_txt.y = 10;
startDay -= 1;
for (var t:int = 0; t < getDays(myDate); t++) {
myArray[t] = (t+1);
var textNum:String = myArray[t];
import box;
import moonPhase;
var square:MovieClip = new box();
var moon:MovieClip = new moonPhase();
holder_txt.addChild(square);
square.name = textNum
moonNum= calculateMoonPhase(myDate.fullYear, myDate.month,t+1);
square.texter.text = textNum +" "+ moonNum;
square.x = (startDay) *75
square.y = (row+1)*65
startDay++;
if(startDay >= 7){
startDay = 0;
row++;
I've got a function that calculate the moonPhase for everyday.
moonNum= calculateMoonPhase(myDate.fullYear, myDate.month,t+1);
It results as a number (between 0 and 8).
I've got movieClip of a moon with 8 frames (new moon,full moon...etc).
I'd like to add the movieClip of the moon on each square with the corresponding frame number.
moonClip.gotoAndStop(moonNum);
I've add each moonNum to each square day :
square.texter.text = textNum +" "+ moonNum;
But I've no idea how to add movieclips to each square day...
Any help ?
It's better to add moonPhase in the class box directly. So for each box that you copied, you have an instance of moon in it to.
But, in your sample code, you can add moon to the square directly to.
square.addChild(moon);

Scnview nodes appear semi transparent

3D Perspective should block shelves etc correctly
Sample Image of the ScnView:
Very Simple. Added Scnview. Added Scnnodes ( shapes and lights )
Using standard Omni type light.
Why are the objects in front not blocking the objects behind ?
Here's the CODE:
[self.studioView addSubview:showTimeView];
showTimeView.frame = CGRectMake(self.paperView.frame.origin.x,
self.paperView.frame.origin.y,
self.paperView.frame.size.width,
self.paperView.frame.size.height);
SCNView *sceneView = (SCNView *)showTimeView;
sceneView.backgroundColor = [UIColor whiteColor];
sceneView.scene = [SCNScene scene];
SCNNode *root = sceneView.scene.rootNode;
sceneView.allowsCameraControl = YES;
sceneView.autoenablesDefaultLighting = NO;
// Add Camera
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.position = SCNVector3Make(0, 0, 100);
cameraNode.eulerAngles = SCNVector3Make(0, -M_PI/8, 0);
cameraNode.camera.zNear = 0;
cameraNode.camera.zFar = thisModuleDepth;
cameraNode.camera.xFov = thisWallWidth;
cameraNode.camera.yFov = thisModuleHeight;
[root addChildNode:cameraNode];
// Add Cabinet Piece
SCNBox *cubeGeom = [SCNBox boxWithWidth:tW
height:tH
length:tD
chamferRadius:0.0];
SCNNode *cubeNode = [SCNNode nodeWithGeometry:cubeGeom];
cubeNode.position = SCNVector3Make(tX, tY, tZ);
// Tag Material
SCNMaterial *material = [SCNMaterial material];
material.diffuse.contents = [UIImage imageNamed:thisColor];
[material.diffuse.contents setAccessibilityIdentifier:thisColor] ;
[material.diffuse.contents setAccessibilityLabel:thisID] ;
cubeNode.geometry.firstMaterial = material;
cubeNode.geometry.firstMaterial.locksAmbientWithDiffuse = NO;
cubeNode.physicsBody = [SCNPhysicsBody staticBody];
[root addChildNode:cubeNode];
// Add spotlight
SCNLight *spotLight = [SCNLight light];
spotLight.type = SCNLightTypeOmni;
spotLight.color = [UIColor whiteColor];
SCNNode *spotLightNode = [SCNNode node];
spotLightNode.light = spotLight;
spotLightNode.position = SCNVector3Make(thisWallWidth / 2 ,thisModuleHeight, thisModuleDepth *2);
spotLightNode.light.intensity = 1000;
[root addChildNode:spotLightNode];
Thanks Noah,
I finally figured out what was wrong!!!
Set the automaticallyAdjustsZRange property of your SCNCamera to true and it will ensure that nothing is clipped because of the wrong zNear or zFar being set.
THANKS FOR YOUR HELP !!!
HERE IT IS FIXED: Perfect 3D Perspective

Actionscript 3, Flash CC: Placing Objects In An Array From The Library Onto The Stage

Hello programming gurus of stackoverflow, I am hoping that at least one of you will be able to help me with my coding problem. This is the first time I'm posting on this site, so if I miss something with the structure of my post, or anything please let me know (preferably not in a condescending matter) and I will gladly change it.
I actually had a different problem I was going to ask about, but I recently realized that some objects from my library weren't showing up on my stage. Hopefully, if this gets solved I won't have my other problem.
I am creating a learning module app using Flash CC and Actionscript 3, I like to think I am fairly proficient with Flash, but right now all my code is on the timeline because when I started I wasn't aware of the package setup. When I finish with the learning module I'll try and move everything to an AS package, so please bear with me.
This current frame of the module is a drag and drop game where the user drags the correct food, for the animal they chose in the previous frame, to the animal in the middle. The animal is dynamically placed on the stage, as well as an array of six possible food choices, all MovieClips pulled from the library. The array of food elements is actually not what I'm having problem with, they appear on my stage with no problems at all. The problem I'm having is when the user drags the correct food onto the animal, and the win condition is met, the array of balloon elements does not show up on the stage. I find it weird because I'm using near identical code for both the food and balloon array.
Here is my full code:
import flash.display.MovieClip;
import flash.events.MouseEvent;
foodPet();
function foodPet():void {
//all of my pet, food, and balloon library objects have been exported for AS
var theBird:pet_bird = new pet_bird;
var theCat:pet_cat = new pet_cat;
var theChicken:pet_chicken = new pet_chicken;
var theDog:pet_dog = new pet_dog;
var theDuck:pet_duck = new pet_duck;
var theGuinea:pet_guinea = new pet_guinea;
var theHamster:pet_hamster = new pet_hamster;
var birdSeed:food_bird_seed = new food_bird_seed;
var catFood:food_cat_food = new food_cat_food;
var chickenFeed:food_chicken_feed = new food_chicken_feed;
var chocolate:food_chocolate = new food_chocolate;
var dogFood:food_dog_food = new food_dog_food;
var duckFood:food_duck_food = new food_duck_food;
var animalList:Array = [theBird, theCat, theChicken, theDog,
theDuck, theGuinea, theHamster];
var food1Array:Array = [birdSeed, catFood, chickenFeed,
chocolate, dogFood, duckFood, 4];
var xPosFood:Array = new Array();
var yPosFood:Array = new Array();
xPosFood = [32, 71, 146, 363, 431, 512];
yPosFood = [304, 222, 123, 123, 222, 304];
var animalClip:MovieClip;
animalClip = animalList[chosenAnimal];
addChild(animalClip);
animalClip.x = 256;
animalClip.y = 287;
animalClip.name = "selectedAnimal";
for (var i:uint = 0; i < food1Array.length - 1; i++){ //Where the food gets added
var isItRight:Boolean = false;
var foodName:String = ("food" + i);
var foodClip:MovieClip;
foodClip = food1Array[i];
foodClip.x = xPosFood[i];
foodClip.y = yPosFood[i];
foodClip.name = foodName;
addChild(foodClip);
trace(foodClip.parent);
foodDragSetup(foodClip, animalClip, food1Array[food1Array.length - 1], isItRight);
}
}
function foodDragSetup(clip:MovieClip, targ:MovieClip, correctNum:uint, isItRight:Boolean) {
var beingDragged:Boolean = false;
var xPos:Number = clip.x;
var yPos:Number = clip.y;
clip.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
function beginDrag(event:MouseEvent):void
{
clip.startDrag();
if (int(clip.name.substr(4)) == correctNum){
isItRight = true;
}
this.beingDragged = true;
setChildIndex(clip, numChildren - 1);
clip.addEventListener(MouseEvent.MOUSE_UP, endDrag);
}
function endDrag(event:MouseEvent):void
{
if (this.beingDragged) {
this.beingDragged = false;
clip.stopDrag();
if ((isItRight) && (clip.hitTestPoint(targ.x, targ.y, true))){
trace(targ.name + " has been hit.");
clip.x = targ.x;
clip.y = targ.y;
win_animal_food();
} else {
isItRight = false;
clip.x = xPos;
clip.y = yPos;
}
}
}
}
function win_animal_food():void {
const BALLOON_ROW:int = 4;
var count:uint = 0;
var altX:uint = 0;
var bBalloon:blue_balloon = new blue_balloon;
var gBalloon:green_balloon = new green_balloon;
var oBalloon:orange_balloon = new orange_balloon;
var pBalloon:purple_balloon = new purple_balloon;
var rBalloon:red_balloon = new red_balloon;
var yBalloon:yellow_balloon = new yellow_balloon;
var balloonList:Array = [bBalloon, gBalloon, oBalloon,
pBalloon, rBalloon, yBalloon, bBalloon, gBalloon,
oBalloon, pBalloon, rBalloon, yBalloon, bBalloon,
gBalloon, oBalloon, pBalloon];
var balloonY:Array = [144, -205, -265, -325];
var balloonX:Array = [0, 140, 284, 428, 68, 212, 356, 500];
for (var ballY:uint = 0; ballY < balloonY.length; ballY++){ //Where balloons
for (var ballX:uint = altX; ballX < altX + BALLOON_ROW; ballX++){ //get added
var balloonName:String = ("balloon" + count);
var balloonClip:MovieClip;
balloonClip = balloonList[count];
balloonClip.x = balloonX[ballX];
balloonClip.y = balloonY[ballY];
balloonClip.name = balloonName;
addChild(balloonClip);
trace(balloonClip.parent);
trace(balloonClip + " has been added!");
balloonClip.addEventListener(MouseEvent.CLICK, balloonPop);
count++;
}
if (altX == 0) {
altX = BALLOON_ROW;
} else {
altX = 0;
}
}
function balloonPop(event:MouseEvent):void {
event.target.play();
event.target.removeEventListener(MouseEvent.CLICK, balloonPop);
}
}
I thought there might have been a problem with my balloon MovieClips, so I subbed them in the food array:
var birdSeed:blue_balloon = new blue_balloon;
var catFood:green_balloon = new green_balloon;
var chickenFeed:orange_balloon = new orange_balloon;
var chocolate:purple_balloon = new purple_balloon;
var dogFood:red_balloon = new red_balloon;
var duckFood:yellow_balloon = new yellow_balloon;
They all showed up on the stage, so there's nothing wrong with the MovieClips.
Added: The first values of balloonXArray and balloonYArray were originally -4 and -145 respectively, but when I started having problems I wanted to make sure the balloons were showing up so I set the first values to 0 and 144 the balloon height and width are both 144 and their cross (not sure on it's name) is in the top left corner.
Added: The reason why there are multiple instances of the same balloon in the balloonList is because I need four rows of four balloons, but only have six different balloons.
I know the balloons are on the stage because the debug display shows their x and y values on the viewable stage. Using trace(foodClip.parent) and trace(balloonClip.parent) shows that the balloons and food all have the same parent, MainTimeline, so I know the balloons aren't getting added to some different space.
I have searched online, but have not come across anyone with a similar problem. Thus, I am asking on this forum if anyone can tell me why my balloons will not show up on the stage.
Please and thank you.
One thing I see straight off in the baloonList is that you have the same object instances listed multiple times. Each instance can only exist on stage exactly once. If you addChild() an instance that is already on stage, the instance is first removed, then re-added at the top of the display list.
You should change:
var bBalloon:blue_balloon = new blue_balloon;
var gBalloon:green_balloon = new green_balloon;
var oBalloon:orange_balloon = new orange_balloon;
var pBalloon:purple_balloon = new purple_balloon;
var rBalloon:red_balloon = new red_balloon;
var yBalloon:yellow_balloon = new yellow_balloon;
var balloonList:Array = [bBalloon, gBalloon, oBalloon,
pBalloon, rBalloon, yBalloon, bBalloon, gBalloon,
oBalloon, pBalloon, rBalloon, yBalloon, bBalloon,
gBalloon, oBalloon, pBalloon];
to:
var balloonList:Array = [
new blue_balloon,
new green_balloon,
new orange_balloon,
new purple_balloon,
new red_balloon,
new yellow_balloon,
new blue_balloon,
new green_balloon,
new orange_balloon,
new purple_balloon,
new red_balloon,
new yellow_balloon,
new blue_balloon,
new blue_balloon,
new green_balloon,
new orange_balloon,
new purple_balloon
];

Nevron BarChart with DateTimeScaleConfigurator weired series plotting?

I am using Nevron Charting Control ver.11.1.17.12 in application. I am facing problem in drawing the chart correct with the DateTimeScaleConfigurator. Here are following problem:
Series Bar overlapping each other if series count increases.
Series getting out of the Axis lines.
X Axis Scale automatically add previous year December and next year Jan in the scale which cause the chart to have blank area in case of Surface Chart.
//code snippet to draw Bar Chart Series
NBarSeries bar = new NBarSeries();
bar.UniqueId = new Guid(outputVariable.UniqueId);
bar.Name = outputVariable.LegendText;
chart.Series.Add(bar);
bar.HasBottomEdge = false;
bar.MultiBarMode = chart.Series.Count == 1 ? MultiBarMode.Series : MultiBarMode.Clustered;
// bar.InflateMargins = true;
bar.UseZValues = false;
indexOfSeries = chart.Series.IndexOf(bar);
ConfigureChartSeries(bar, indexOfSeries, outputVariable);
SetSeriesAxisInformation(bar, outputVariable.Unit);
bar.UseXValues = true;
foreach (DataRow row in seriesDataTable.Rows)
{
bar.XValues.Add(Convert.ToDateTime(row["TimeStamp"]).ToOADate());
}
code snippet to Add Surface Chart Series
chart.Enable3D = true;
chart.BoundsMode = BoundsMode.Stretch;
(chart as NCartesianChart).Fit3DAxisContent = true;
chart.Projection.SetPredefinedProjection(PredefinedProjection.OrthogonalTop);
chart.LightModel.EnableLighting = false;
chart.Wall(ChartWallType.Back).Visible = false;
chart.Wall(ChartWallType.Left).Visible = false;
chart.Wall(ChartWallType.Floor).Visible = false;
// setup Y axis
chart.Axis(StandardAxis.PrimaryY).Visible = false;
// setup Z axis
NAxis axisZ = chart.Axis(StandardAxis.Depth);
axisZ.Anchor = new NDockAxisAnchor(AxisDockZone.TopLeft);
NLinearScaleConfigurator scaleZ = new NLinearScaleConfigurator();
scaleZ.InnerMajorTickStyle.Visible = false;
scaleZ.MajorGridStyle.ShowAtWalls = new ChartWallType[0];
scaleZ.RoundToTickMin = false;
scaleZ.RoundToTickMax = false;
axisZ.ScaleConfigurator = scaleZ;
axisZ.Visible = true;
// add a surface series
NGridSurfaceSeries surface = new NGridSurfaceSeries();
surface.UniqueId = new Guid(outputVariable.UniqueId);
surface.Name = outputVariable.LegendText;
chart.Series.Add(surface);
surface.Legend.Mode = SeriesLegendMode.SeriesLogic;
surface.ValueFormatter = new NNumericValueFormatter("0.0");
surface.FillMode = SurfaceFillMode.Zone;
surface.FrameMode = SurfaceFrameMode.Contour;
surface.ShadingMode = ShadingMode.Flat;
surface.DrawFlat = true;
// Already set this property to false and working in other chart.
surface.InflateMargins = false;
surface.FrameColorMode = SurfaceFrameColorMode.Zone;
surface.SmoothPalette = true;
surface.Legend.Format = "<zone_value>";
surface.FillMode = SurfaceFillMode.Zone;
surface.FrameMode = SurfaceFrameMode.Contour;
CreateSurfaceSeries(outputVariable, surface);
chartControl.Refresh();
And the ScaleConfigurator configuration
chartPrimaryXAxis = chart.Axis(StandardAxis.PrimaryX);
// X Axis Configuration
dateTimeScale = new NDateTimeScaleConfigurator();
dateTimeScale.Title.Text = string.Empty;
dateTimeScale.LabelStyle.Angle = new NScaleLabelAngle(ScaleLabelAngleMode.Scale, 90);
dateTimeScale.LabelStyle.ContentAlignment = ContentAlignment.MiddleLeft;
dateTimeScale.LabelStyle.TextStyle.FontStyle = new NFontStyle("Times New Roman", 6);
dateTimeScale.LabelFitModes = new LabelFitMode[] { LabelFitMode.AutoScale };
chartPrimaryXAxis.ScaleConfigurator = dateTimeScale;
chartPrimaryXAxis.ScrollBar.ResetButton.Visible = true;
chartPrimaryXAxis.ScrollBar.ShowSliders = true;
dateTimeScale.EnableUnitSensitiveFormatting = true;
Here is the generated output:
Any idea regarding this problem will be deeply appreciated.
Thanks in advance.
Series Bar overlapping each other if series count increases.
&
Series bar getting out of the Axis lines.
Answer: When you are using categorial data then use NOrdinalScaleConfigurator rather than NDateTimeScaleConfigurator. It will not solve the problem and put the series bar in the center of the scale and auto resize them according to the chart size also.
X Axis Scale automatically add previous year December and next year
Jan in the scale which cause the chart to have blank area in case of
Surface Chart.
Answer:
Set the following properties of the DateTimeScaleConfigurator to false to avoid such behavior.
dateTimeScale.RoundToTickMax = false;
dateTimeScale.RoundToTickMin = false;

Resources