MiniMap - Only show a part of a big Sprite

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Godot_Starter

I want to make a MiniMap, and I have a idea, but please write if this idea is a bad attempt.

So this is my idea:

I have a Frame for my MiniMap and inside of the Frame, I want to show the MiniMap. I made the whole Map as a png File and now I want to show just a small part of this map inside of my Frame and then, when the Player moves, I want to move my map as well, but I want that only the part of the map inside the Frame is visible. Is there a way to do that or is this a bad attempt for a MiniMap?

Thanks for answers and ask if there are ambiguities.

PS: Sorry for bad English and the word ambiguities is from the google translator…

:bust_in_silhouette: Reply From: supper_raptor

I am using TextureRect for minimap and i am using shader to move map with my player.
Create TextureRect and check expand and set stretch mode to tile. Then add shader

Shader code

shader_type canvas_item;
uniform vec2 pos;

void fragment()
{
	COLOR = texture(TEXTURE, UV + pos);
}

code

var worldsize = Vector2() #your world size


func moveMapWithPlayer():
	#half resolution , used to keep player centered in minimap
	var half_res = get_viewport().size / 2	
	var pos = player.position - half_res

	#clamp
	pos.x = clamp(pos.x,0, worldsize.x)
	pos.y = clamp(pos.y,0, worldsize.y)
	material.set_shader_param("pos",pos / worldsize)

func _process():
	moveMapWithPlayer()

This is how it looks like in my game : https://www.youtube.com/watch?v=7Hd66AzfdXo&t=17

supper_raptor | 2020-05-22 05:07

I add the second code to the TextureRect and the first code to the shader of the TextureRect, right? Because I got a error, which says that the variable pos isnt declared.

What did I wrong?

Godot_Starter | 2020-05-22 07:32

sorry it’s
var pos = player.position - half_res

and u have to set worldsize

supper_raptor | 2020-05-22 07:38

I set the worldsize to 128,128 and I wrote 0,0 as the player.position, in order to test it.

And does it matters where the minimap is located? Because it still happens nothing.

Godot_Starter | 2020-05-22 07:53

I can still see the rest of the MiniMap outside the Frame

Godot_Starter | 2020-05-22 07:53

worldsize is in pixels 128x128 is very small

supper_raptor | 2020-05-22 07:59

ok I increesd it to 12800, 12800 (my world isnt finished so its just a number) and this is, how it looks in the editor right now:Image

Godot_Starter | 2020-05-22 08:05

Obviously, you cant open the image… In fact, it still doesnt work and does the start position of the Player and the Minimap maters?

Godot_Starter | 2020-05-22 08:07

i cant view ur image

supper_raptor | 2020-05-22 08:09

have u assigned texture in textureRect?

supper_raptor | 2020-05-22 08:10

Can I add a .tscn to a comment? Because then I would be able to send you my scene

Godot_Starter | 2020-05-22 08:16

you cannot send .tscn directly , you need to host it somewhere and send link in comment.
Upload here http://s000.tinyupload.com/ and paste the link in comment

supper_raptor | 2020-05-22 08:20

Does this works? (I make it in a new project, where nothing else then the MiniMap and a FakePlayer is.)

http://s000.tinyupload.com/?file_id=28969992982839350570

Godot_Starter | 2020-05-22 08:31

It is a 7zip file, I hope you can deal with it. If not write me. The important thing is not to move the MiniMap, the important thing is that I cant see the MiniMap outside of the Frame. Thats why I made a fix Node2d as a Player, in order to test it.

Godot_Starter | 2020-05-22 08:36

I have modified your minimap
Move player with arrow keys
Try moving towards right you will see minimap moving with player

http://s000.tinyupload.com/?file_id=05176349353160326372

supper_raptor | 2020-05-22 08:52

To create minimap.png like i have done , you need to-
1.)Make a level
2.)capture screen shot

use this image as ur minimap

supper_raptor | 2020-05-22 08:56

Ok, thank you!

Godot_Starter | 2020-05-22 09:16

But the problem was less that my map is empty and more that you can see the rest of the map outside the frame

Godot_Starter | 2020-05-22 09:27

I will make another question on this later

Godot_Starter | 2020-05-22 09:28

Its my function to generate minimap of a level.

func captureMap():
	#minimap to actual map ratio
	var ratio = 8	
	
	#64 is tile size
	var size = $TileMap.get_used_rect().size * Vector2(64,64)
	OS.window_size = size / Vector2(ratio,ratio)
	var max_xy = min(size.x,size.y)
	var ratio
	if max_xy == size.x:
		ratio = OS.window_size.x / max_xy
	else:
		ratio = OS.window_size.y / max_xy
	
	self.scale = Vector2(ratio,ratio)
	
	yield(get_tree(), "idle_frame")
	yield(get_tree(), "idle_frame")
	yield(get_tree(), "idle_frame")
	yield(get_tree(), "idle_frame")
	# Retrieve the captured Image using get_data()
	var img = get_viewport().get_texture().get_data()
	# Flip on the y axis
	# You can also set "V Flip" to true if not on the Root Viewport
	img.flip_y()
	# Convert Image to ImageTexture
	img.save_png("res://minimap.png")

supper_raptor | 2020-05-22 09:30

and btw

worldsize = $TileMap.get_used_rect().size * Vector2(64,64)

64 is default tile size

supper_raptor | 2020-05-22 09:32