Wednesday, July 13, 2022
HomeGame DevelopmentscreenToWorld after altering display screen measurement at runtime - Cocos Creator

screenToWorld after altering display screen measurement at runtime – Cocos Creator


Hello!
I’ve two nodes on my display screen. One is UI object and one is 3D world object
Digicam is in Ortho mode
I need to transfer 3d world object to UI object place on click on
I can do that through the use of digicam.screenToWorld. It really works advantageous till I need to swap display screen measurement/decision. Then my 3d node strikes incorrect


However the whole lot works advantageous if I modify design width/peak in undertaking settings to no matter values
I believe the issue is altering measurement at runtime

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

Hello!
In that reference field was transferring by mouse contact place
However I’m getting incorrect place making an attempt to comply with 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(Digicam)

Camera3D !: Digicam;

@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.mum or dad!.getComponent(UITransform)!.convertToNodeSpaceAR(pos);

}

onDistanceChanged(slider: Slider) {

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

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

}

}

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

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

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


You’ll be able to select to take heed 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 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 is because the 3d digicam show window has been modified, however the worldPosition of the nodes are nonetheless the identical.The calculations for second 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 need to do the other – transferring 3d node to UI node place

Then you have to be touching the label node to maneuver the dice node to comply with 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?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments