Thursday, July 14, 2022
HomeGame DevelopmentscreenToWorld after altering display measurement at runtime - Cocos Creator

screenToWorld after altering display measurement at runtime – Cocos Creator


Hello!
I’ve two nodes on my display. One is UI object and one is 3D world object
Digital camera is in Ortho mode
I wish to transfer 3d world object to UI object place on click on
I can do that by utilizing digital camera.screenToWorld. It really works positive till I wish to change display measurement/decision. Then my 3d node strikes incorrect


However all the pieces works positive if I modify design width/peak in venture settings to no matter values
I believe the issue is altering measurement at runtime

I examined it with this demo and it really works positive.

Hello!
In that reference field was shifting by mouse contact place
However I’m getting incorrect place making an attempt to observe place of different node

I added richtext “PlaceHere” as targer place node for the field

@ccclass(‘ScreenToWorldPoint’)

export class ScreenToWorldPoint extends Element {

@property(Node)

field !: Node;

@property(Label)

distanceLabel !: Label;

@property(Digital camera)

Camera3D !: Digital camera;

@property(Node)

goal !: Node

_distance = 0;

begin () {

    var canvas = discover('Canvas')!;

    canvas.on(Node.EventType.TOUCH_START, this.onTouchStart, this);

    canvas.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);

    this._distance = 0.5;

}

onTouchStart(evt: EventTouch) {

    this.moveBox(evt.getLocation());

}

onTouchMove(evt: EventTouch) {

    this.moveBox(evt.getLocation());

}

moveBox (touchPos: Vec2) {

    const targetPosition = this.goal.worldPosition

    let pos = this.Camera3D.screenToWorld(new Vec3(targetPosition.x, targetPosition.y, this._distance));

    this.field.place = this.field.guardian!.getComponent(UITransform)!.convertToNodeSpaceAR(pos);

}

onDistanceChanged(slider: Slider) {

    this._distance = parseFloat(slider.progress.toFixed(2));

    this.distanceLabel.string = 'distance : ' + this._distance;

}

}

However it does look appropriate to me if I rotate display to 90 levels

The query is why does screenToWorld works like this? What ought to I do to get field place change appropriately with any display decision in debug mode?

My guess it’s by some means associated to design width and design peak (which is 960×640 on this instance)


You’ll be able to select to hearken to both of those two occasions to carry out a place reset.

Yeah however I don’t get it
screenToWorld offers me incorrect values everytime I’m not utilizing my design peak and design width
You’ll be able to see this on my second image. Display screen is 960×640 which is my design peak and design width. Field was positioned to richtext place utilizing values from screenToWorld.

But when I modify it runtime to another decision then screenToWorld will give me incorrect place

Sorry, I misunderstood it.I believed you wished the label to maintain following the dice node.
It’s because the 3d digital camera show window has been modified, however the worldPosition of the nodes are nonetheless the identical.The calculations for 2nd and 3d cameras differ.

Okay
I’ve no clue then
Are you able to present a working resolution for my case please?

Creator3.5.0_3D_ModelText.zip (3.7 MB)
like this?

On this instance UI node following 3D node
I wish to do the alternative – shifting 3d node to UI node place

Then you have to be touching the label node to maneuver the dice node to observe the label, see the code now’s contact to maneuver the dice?
do you simply need the dice node and label node to be in the identical place after the decision change? If that’s the case, the dice’s worldPosition will change. Is that you really want?

I would like that after I’m clicking on a 3d object, it strikes to the place of the label
The whole lot works positive till the display decision adjustments.

NewProject_112.zip (1.6 MB)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments