Tuesday, July 12, 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
Digital camera is in Ortho mode
I need to transfer 3d world object to UI object place on click on
I can do that by utilizing digicam.screenToWorld. It really works advantageous till I need to change display screen measurement/decision. Then my 3d node strikes incorrect


However all the things works advantageous if I alter design width/peak in challenge settings to no matter values
I feel 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 observe place of different node

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

@ccclass(‘ScreenToWorldPoint’)

export class ScreenToWorldPoint extends Part {

@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.mother or father!.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 in some way associated to design width and design peak (which is 960×640 on this instance)


You may 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 may 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 alter it runtime to some other decision then screenToWorld will give me incorrect place

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments