Godot - Change tiles within an area - godot

I'm trying to make it so if a function is called an area covered by a Collisionshape2D gets its tiles removed in a TileMap node. My problem is the area deleted doesn't match the Collisionshape2D's. Any insight is appreciated! Thank you.
My code:
func changeArea(collionshape):
var corridorTile = $CorridorTiles
var rect = Rect2(collionshape.position, collionshape.shape.extents*2)
var topleft = corridorTile.world_to_map(rect.position)
var bottomright = corridorTile.world_to_map(rect.end)
for x in range(topleft.x, bottomright.x):
for y in range(topleft.y, bottomright.y):
corridorTile.set_cell(x, y, -1)
Edit1*
Upon changing the code to:
func changeCorridorTile(collionshape):
var corridorTile = $CorridorTiles
var extents:Vector2 = collionshape.shape.extents
var topleft = corridorTile.world_to_map(collionshape.position - extents)
var bottomright = corridorTile.world_to_map(collionshape.position + extents)
for x in range(topleft.x, bottomright.x):
for y in range(topleft.y, bottomright.y):
corridorTile.set_cell(x, y, -1)
corridorTile.update_bitmask_region()
The cells in the tileMap which the collisionShape2D area covers get deleted. And I update them using .bitmask_region() method.

First thing I notice, from the top of my head: The Rect2 position is a corner, but the CollisionShape position is the center.
You would have to do it like this:
var extents:Vector2 = collionshape.shape.extents
var rect := Rect2(collionshape.position - extents, extents * 2)
Now, on a second look, you don't need the Rect2 at all:
var extents:Vector2 = collionshape.shape.extents
var topleft = corridorTile.world_to_map(collionshape.position - extents)
var bottomright = corridorTile.world_to_map(collionshape.position + extents)
One more thing: I believe that is in local space of the parent of the CollisionShape2D (an Area2D, I guess). So, if that is not working, it might be because you need to do it in global space. Hopefully you don't have any rotation (which would just mess the whole thing up) or scaling, so that would only be a matter of using global_position.
Addendum: I forgot something, world_to_map does not take global coordinates. Try this instead (assuming no rotation or scaling):
var extents:Vector2 = collionshape.shape.extents
var position := corridorTile.to_local(collionshape.global_position)
var topleft = corridorTile.world_to_map(position - extents)
var bottomright = corridorTile.world_to_map(position + extents)

Related

I tried to shoot an object towards cursor position, but it has a problem

I'm a beginner in Godot I want to shoot an object that is already in the game towards where the cursor's position is, and the method I created for it works fine but depending on the distance between the object and the cursor the speed changes. can anyone please help me make the speed constant?
I used this:
func _process(_delta):
if Input.is_action_just_released("tap"):
var mousepos = get_viewport().get_mouse_position()
var ballpos = self.get_position()
var x = mousepos.x - ballpos.x
var y = mousepos.y - ballpos.y
velocity = Vector2(x,y)
You can normalize your vector, which gives you a vector of unit length:
velocity = Vector2(x,y).normalize()
And then scale it by the speed you want:
velocity = Vector2(x,y).normalize() * speed
Where speed is a previously defined variable or constant. Something like this will do:
var speed := 100.0
You, of course, will want to tweak the value. So perhaps you want to export it so you can set it form the inspector:
export var speed := 100.0
By the way, you can rewrite the code you have to this:
var mousepos = get_viewport().get_mouse_position()
var ballpos = self.get_position()
velocity = mousepos - ballpos
Adding the changes suggested above we have:
var mousepos = get_viewport().get_mouse_position()
var ballpos = self.get_position()
velocity = (mousepos - ballpos).normalize() * speed
Which you can rewrite to this:
var mousepos = get_viewport().get_mouse_position()
var ballpos = self.get_position()
velocity = ballpos.direction_to(mousepos) * speed

Kinematic Body 2D not moving

I'm trying to game a game using the Godot Engine but I'm stuck at the beginning! I can't make my KinematicBody2D move!
This is my Player.GD script
extends KinematicBody2D
var velocity = Vector2.ZERO
var move_speed = 480
var gravity = 1200
var jump_force = -720
var right = Input.is_action_pressed("move_right")
var left = Input.is_action_pressed("move_left")
var jump = Input.is_action_pressed("jump")
func _ready():
pass
func _physics_process(_delta):
var move_direction = int(right) - int(left)
velocity.x = move_speed * move_direction
move_and_collide(velocity)
Can someone, please, help me?
All this code will run when the KinematicBody2D is initialized:
var velocity = Vector2.ZERO
var move_speed = 480
var gravity = 1200
var jump_force = -720
var right = Input.is_action_pressed("move_right")
var left = Input.is_action_pressed("move_left")
var jump = Input.is_action_pressed("jump")
In consequence, it will not be taking input in real time. Instead you want the last three lines here:
func _physics_process(_delta):
var right = Input.is_action_pressed("move_right")
var left = Input.is_action_pressed("move_left")
var jump = Input.is_action_pressed("jump")
# …
Those are boolean, by the way. You can get a float from 0.0 to 1.0 if you use Input.get_action_strength instead. Which will also let your code ready for analog input.
I also want to point out that move_and_collide does not take a velocity, but a displacement vector. So to call it correctly, you want to multiply the velocity by delta:
func _physics_process(delta):
# …
move_and_collide(velocity * delta)
Or use move_and_slide, which does take a velocity. By the way, the up parameter that move_and_slide takes is to discern between floor, ceiling, and walls. Without, everything is considered a wall.

Is there a way so that I can keep the square but resize it in openlayers?

I got four different coordinates that is making a square in the openlayers map. But I want to know if it is possible that instead of breaking the square shape, is it possible to just grab one of the corners and resize (make bigger/smaller) the square?
My coordinates are as follows:
var x_1 = 28.0244307;
var y_1 = -25.8635238;
var x_2 = 28.0244307;
var y_2 = -25.8835238;
var x_3 = 28.0444307;
var y_3 = -25.8835238;
var x_4 = 28.0444307;
var y_4 = -25.8635238;
And then the code to resize it, but it is breaking the square shape:
var coords = [
[x_1, y_1],
[x_2, y_2],
[x_3, y_3],
[x_4, y_4],
[x_1, y_1]
];
var polygon = new ol.geom.Polygon([coords]);
var feature = new ol.Feature(polygon);
polygon.transform('EPSG:4326', 'EPSG:3857');
var vectorSource = this.vectorSource;
vectorSource.addFeature(feature);
var select = new ol.interaction.Select();
var modify = new ol.interaction.Modify({
features: select.getFeatures(),
});
var snap = new ol.interaction.Snap({
source: vectorSource,
});
this.map.addInteraction(select);
this.map.addInteraction(modify);
this.map.addInteraction(snap);
Just create a polygon with your 4 points an use a GeometryFunction in your style to only display the points.
Then use ol-ext ol/interaction/Transform to modify the shape.

Raphael -- object rotation at arbitrary position

I'm using Raphael_2.01 and would like to rotate an object at arbitrary position.
(WindowsXP, Firefox3.6)
example: http://uproda11.2ch-library.com/326446b6u/11326446.png
This rectangle (rect0) rotates thirty degrees at its lower right point.
The parameters are:
var rectX = rect0.getBBox().x;
var rectY = rect0.getBBox().y;
var rectW = rect0.getBBox().width;
var rectH = rect0.getBBox().height;
var rot = 30;// rotation
var rotX, rotY;// arbitrary position
What code should I use ? I can't image suitable method.
thanks,
If I understand the question correctly, it's rect0.rotate(30, rotX, rotY);.

Yui Mouse Coordinates over Element

I'm trying to determine if the mouse is over an element preferably with YUI if there is a method for that already.
Basically something like
function bool IsMouseOver(Element);
I ended up checking it myself.
I have the region from the target element
var region = YAHOO.util.Dom.getRegion(this.element);
var top = region.top;
var left = region.left;
var bottom = region.bottom;
var right = region.right;
Then the mouse coordinates
var mouseXY = YAHOO.util.Event.getXY(e);
var mX = mouseXY[0];
var mY = mouseXY[1];
And then a simple if statement to check if the mouse coordinates were in the region
(mX > left && mX < right && mY > top && mY < bottom)

Resources