Einrastfunktion bei Raster das sich bewegt?
Übersicht

![]() |
DeVILBetreff: Einrastfunktion bei Raster das sich bewegt? |
![]() Antworten mit Zitat ![]() |
---|---|---|
Hey Leute,
Ich mache grade einen Tilemap editor die Blöcke rasten zwar im moment bei jedem 64sten pixel ein allerdings nicht mehr wenn ich scrolle, da das raster ja dann verschoben ist im moment sieht die formel so aus: rasterX = Floor(MouseX()/64)*64 rasterY = Floor(MouseY()/64)*64 |
||
![]() |
SpionAtom |
![]() Antworten mit Zitat ![]() |
---|---|---|
Addiere einfach mal MouseX() MOD 64 bzw MouseY() MOD 64 | ||
os: Windows 10 Home cpu: Intel Core i7 6700K 4.00Ghz gpu: NVIDIA GeForce GTX 1080 |
Lador |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
Was genau meinst du mit einem "Raster" bzw. mit "einrasten"?
Wenn du einen Block um das Tile willst, über dem gerade deine Maus liegt, dann solltest du einfach... Code: [AUSKLAPPEN] rasterX = (mausX/64) rasterY = (mausY/64) ...bei einer Tilegröße von 64x64 Pixeln machen. Rein mathematisch betrachtet würden sich doch deine /64 und deine *64 sowieso wegkürzen, oder? Und Floor() ist überflüssig. Beim scrollen müsstest du uns schon ein bisschen mehr Code geben, wenn wir helfen sollen. MFG Lador |
||
Mein aktuelles Projekt:
2D-Rollenspiel "Iliran" Screenshot | Worklog Fortschritt: ca. 70% |
![]() |
SpionAtom |
![]() Antworten mit Zitat ![]() |
---|---|---|
Das mausX/64 liefert aber eine Ganzzahl ab, insofern ist das Floor-cast wirklich überflüssig, macht aber deutlich, dass das beabsichtigt ist. mausx/64 gibt nämlich an, in welchem RasterFeld sich die Maus befindet. Multipliziert man das ganze mit 64, so weiß man, wo dieses Feld anfängt...
Darum ist das (mausx/64)*64 nicht gleich dem mathematisch angenommenen. Und hier mal eine Standard-Rangehensweise von mir: *kram* *find* Code: [AUSKLAPPEN] Const xr = 1024, yr = 768, fps = 60
Const map_width = 100, map_height = 100, map_g = 30 Const cam_x = 0, cam_y = 0, cam_w = 1024, cam_h = 600 Global map_x = 0, map_y = 0, mouse_x, mouse_y, mouse_hit, mmX, mmY Dim map(100, 100) Graphics xr, yr, 0, 2 SetBuffer BackBuffer() fps_timer = CreateTimer(fps) Repeat mouse_x = MouseX(): mouse_y = MouseY(): mouse_hit = MouseHit(1) If drag = 0 And MouseDown(3) Then drag = 1: old_map_x = map_x: old_map_y = map_y: dragX = mouse_x: dragY = mouse_y If drag = 1 And MouseDown(3) Then map_x = old_map_x + (mouse_x - dragX): map_y = old_map_y + (mouse_y - dragY) If drag = 1 And (Not MouseDown(3)) Then drag = 0 If drag = 0 And mouse_x = 0 Then map_x = map_x + 5 If drag = 0 And mouse_y = 0 Then map_y = map_y + 5 If drag = 0 And mouse_x = xr - 1 Then map_x = map_x - 5 If drag = 0 And mouse_y = yr - 1 Then map_y = map_y - 5 Cls drawmap Color 255, 255, 255 Rect cam_x, cam_y, cam_w, cam_h, 0 Text 0, 700, map_x + "," + map_y Text 0, 716, mmX + "," + mmY Flip(0) WaitTimer fps_timer Until KeyDown(1) End Function drawmap() show_x = -map_x / map_g show_y = -map_y / map_g Color 50, 50, 50 mmX = -1: mmY = -1 Viewport cam_x, cam_y, cam_w, cam_h For i = show_x To show_x + cam_w / map_g + 2 For j = show_y To show_y + cam_h / map_g + 2 If i >= 0 And j >= 0 And i < map_width And j < map_width Then Rect map_x + i * map_g, map_y + j * map_g, map_g, map_g, 0 ;Text map_x + i * map_g, map_y + j * map_g, i + "," + j If mouseInRect(cam_x, cam_y, cam_w, cam_h) And mouseInRect(map_x + i * map_g, map_y + j * map_g, map_g, map_g) Then mmX = i: mmY = j End If Next Next Viewport 0, 0, xr, yr End Function ;Prüft, ob sich die Maus in einem angegebenen Rechteck befindet Function mouseInRect(x, y, w, h) If mouse_x < x Then Return False If mouse_y < y Then Return False If mouse_x > x + w Then Return False If mouse_y > y + h Then Return False Return True End Function ;Gibt einen Text zentriert auf dem Bildschirm aus Function Center(txt$, y) Text (GraphicsWidth() - StringWidth(txt$)) / 2, y, txt$ End Function Ich sehe gerade, ist ein schlechteres Beispiel. Hier ein besseres: Hier wird sogar berechnet, welche Felder sichtbar sind, also gezeichnet werden müssen, und welche nicht Code: [AUSKLAPPEN] Graphics 1024, 768, 0, 2
SetBuffer BackBuffer() mapx = 100 mapy = 100 mapXtiles = 120 mapYtiles = 120 tileW = 50 tileH = 40 mapw = mapXtiles * tileW maph = mapYtiles * tileH camx = 200 camy = 100 camw = GraphicsWidth() - 2 * camx camh = GraphicsHeight() - 2 * camy Repeat ;Eingaben mouse_x = MouseX() mouse_y = MouseY() ;Dragdropgeschichte zum Map bewegen If MouseDown(1) And dragstart = 0 Then dragx = mapx - mouse_x dragy = mapy - mouse_y dragstart = 1 End If If MouseDown(1) And dragstart = 1 Then mapx = dragx + mouse_x mapy = dragy + mouse_y End If If Not MouseDown(1) And dragstart = 1 Then dragstart = 0 ;Berechnung der MausTilekoordinaten mIntileX = Floor((mouse_x - mapx) / Float(tileW)) mIntileY = Floor((mouse_y - mapy) / Float(tileH)) ;Berechnung der show-Koordinaten showXtile = (camx - mapx) / tileW If showXtile < 0 Then showXtile = 0 showYtile = (camy - mapy) / tileH If showYtile < 0 Then showYtile = 0 showx = mapx + showXtile * tileW showy = mapy + showYtile * tileH showXtiles = (camx - showx + camw) / tileW + 1 If showXtile + showXtiles > mapXtiles Then showXtiles = mapXtiles - showXtile showYtiles = (camy - showy + camh) / tileH + 1 If showYtile + showYtiles > mapYtiles Then showYtiles = mapYtiles - showYtile If showx > camx + camw Then showXtiles = 0 If showy > camy + camh Then showYtiles = 0 Cls ;Camera Color 255, 255, 255 Rect camx, camy, camw, camh, 0 Oval camx - 2, camy - 2, 4, 4 Text camx + 10, camy - 17, "Camera camx, camy, camw, camh" ;Map Color 50, 100, 255 Oval mapx - 2, mapy - 2, 4, 4 Text mapx + 10, mapy - 17, "Map mapx, mapy, mapw, maph" For xx = 0 To mapXtiles - 1 For yy = 0 To mapYtiles - 1 Rect mapx + xx * tileW, mapy + yy * tileH, tileW, tileH, 0 Next Next ;show Color 255, 100, 55 Oval showx - 2, showy - 2, 4, 4 Text showx + 10, showy - 17, "Show showXtile, showYtile, showXtiles, showYtiles" For xx = 0 To showXtiles - 1 For yy = 0 To showYtiles - 1 Rect showx + xx * tileW, showy + yy * tileH, tileW, tileH, 0 Text showx + xx * tileW, showy + yy * tileH, (showXtile + xx) + "," + (showYtile + yy) Text showx + xx * tileW, showy + yy * tileH + 16, ((showXtile + xx) + (showYtile + yy) * mapXtiles) Next Next Color 0, 255, 0 Text 0, 650, "Maus in Feld: " + mIntileX + ", " + mIntileY Text 0, 670, "Map: " + mapx + ", " + mapy Text 0, 690, "Show: " + showx + ", " + showy + " ShowTile: " + showXtile + ", " + showYtile Flip() Until KeyDown(1) End Ich berechnete die Tile-Koordinaten anscheinend so: Code: [AUSKLAPPEN] ;Berechnung der MausTilekoordinaten
mIntileX = Floor((mouse_x - mapx) / Float(tileW)) mIntileY = Floor((mouse_y - mapy) / Float(tileH)) |
||
os: Windows 10 Home cpu: Intel Core i7 6700K 4.00Ghz gpu: NVIDIA GeForce GTX 1080 |
![]() |
DeVIL |
![]() Antworten mit Zitat ![]() |
---|---|---|
Nein Lador Floor und *64 sind nicht überflüssig
Erst Wird MausX()/64 gerundet die /64 sind damit die zahl keine natürliche Zahl mehr ist dann wird es wieder * 64 genommen damit rasterX immer ein Teiler von 64 ist Und den anderen vielen dank habs hinbekommen |
||
Lador |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
Naja, wie SpionAtom aber auch gemeint hat, ist Floor() überflüssig. Und das mit dem *64 hab ich inzwischen auch verstanden. ^^
MFG Lador |
||
Mein aktuelles Projekt:
2D-Rollenspiel "Iliran" Screenshot | Worklog Fortschritt: ca. 70% |
Übersicht


Powered by phpBB © 2001 - 2006, phpBB Group