I truly want to know. How do i get the mouse position to a part in roblox? Like, i want to get the mouse position, on screen, aligned with a part. Let's say the part position is 10,10,10 and i want my mouse to point at that exact position, how can i do that? (I just need to get the position, ex: 1093x899, 1789x305. Didn't understand? Here, i simply want to know the position, on which my mouse would say that there is the 'part' that i want my mouse to point at. Any help is nice!
Take a look at the docs for Camera:WorldToScreenPoint.
I've adapted the code sample from there:
local camera = workspace.CurrentCamera
-- get the position of the part you care about
local targetPart = game.Workspace.YOUR_PART_NAME
local worldPoint = targetPart.Position
-- get the screen coordinates
local vector, onScreen = camera:WorldToScreenPoint(worldPoint)
if onScreen then
local screenPoint = Vector2.new(vector.X, vector.Y)
print(screenPoint)
else
print("the part isn't on your screen right now")
end
You'll need to hook this up to a loop of some kind to repeated get the coordinates.
Related
I worked on a small fishing game in Godot for a Game Jam and as it was my first time creating something in Godot, for much of it I followed the Your first game tutorial from the Godot docs. In this tutorial, mobs are spawned from all sides of the screen, but for my game I wanted the fish to spawn only from the left and right side. Because of this change, instead of using one Path2d for the mob spawning, I used two straight lines on either side.
The lines were placed with the points in a clockwise order, so on the right side, the first point was the top and the second was the bottom, and on the left side, the first point was the bottom and the second was the bottom.
Here is a picture that shows the spawning lines.
The mobs spawn and move in the correct direction, but no matter what I tried, the fish that spawned from the right side spawned with an incorrectly placed hitbox and with the sprite upside-down.
This is an example of a fish that spawned from the left side, while this is an example of a fish that spawned from the right side.
Here is the code for spawning fish. Side is a variable that passed in and is simply 0 if the fish is spawning from the left and 1 if the fish is spawning from the right.
var fish = Fish.instance()
add_child(fish)
var fishSide
if (side == 0):
fishSide = $FishPathLeft/FishFollowLeft
else:
fishSide = $FishPathRight/FishFollowRight
fishSide.offset = randi()
fish.linear_velocity = Vector2(fish.speed, 0)
var direction = fishSide.rotation + PI / 2
fish.rotation = direction
fish.position = fishSide.position
fish.linear_velocity = fish.linear_velocity.rotated(direction)
When a fish is created, they are given a random base sprite from 4 possible bases, all of which have specific settings for the collision shape. The fish sprites default to facing left.
I assumed that the issue was because the fish is rotated 180 degrees when spawned on the right, but the problem is that even if I try to flip the fish, it still appears upside-down. I've tried flipping the fish with:
fish.scale.y = -fish.scale.y
But it doesn't seem to do anything, so I'm kind of at a loss as to what would fix it.
What I see here is that you are rotating the fish. Which also explains the pictures:
fish.rotation = direction
But that is not what you want.
Yes, you could use scale (fish.scale.y = -fish.scale.y), However, it is not recommended to scale them.
The sprite has flip_v and flip_h properties, that will solve the visual part. But they have nothing to do with the collision shape.
So you could do this:
fish.get_node("sprite").flip_h = (side == 1)
Where sprite is a sprite child of the fish.
What I'm going to suggest for the collision shape is to have two, one for the left and one for the right, and the disable the one the fish will not use (shape.disabled = true).
You could do something like this:
fish.get_node("left_collision").disabled = (side == 1)
fish.get_node("right_collision").disabled = (side == 0)
Where left_collision and right_collision are collision shapes children of the fish.
As an alternative, consider rotating the collision shape a quarter turn clockwise (-TAU / 4, a.k.a -PI / 2):
fish.get_node("collision").rotation = -TAU / 4 if (side == 1) else 0
By the way, in the long run you might benefit from placing all this logic in a function in a script attached to the root node of fish scene. Have the function take the velocity as input, and using it decide if it is going to flip and rotate the sprite, and if it is going to disable or rotate collision shapes. That way, if you later decide you want the fish to be able to change direction, it is a matter of calling that function again (in fact, you could do that in _physics_process of the fish).
Something else (I know you do it the way the tutorial does it, but): give position the node before adding it as child. If you add it first, and place it second, it might register some collisions before you change the position.
I try to setting camera position of pptk, with this method:
xyz = pptk.rand(100, 3)
v = pptk.viewer(xyz)
v.set(lookat=[0,0,0])
this working well, the camera position change.
But when I left-clik on it, to move the point cloud, the camera position return to the original position
Do you have an idea how to maintain the camera position
Thank you
Please send the look-at coordinates as a tuple (0,0,0) instead of a list [0,0,0].
v.set(lookat=(0,0,0))
I found this bug too. I'm currently working around it by first using the mouse wheel to move in/out on the point cloud before clicking; then the look at position doesn't change.
Recently ive been trying to create a small game using Pygame, Ive managed to display an image which can move side to side, the trouble ive been having is enabling it to fire bullets using the space bar.
Heres my code so far:
http://pastebin.com/24xDYwY7
Ive now got the bullet to display however it doesnt come out from the "turret.png", and just sits in the top left of the screen.
Is anyone able to help me with this? I am extremely new to python and would appreciate the help
It looks like you're using a technique I like employing in PyGame - using different sprite Groups for separating good guys, bad guys, bullets, blocks, etc… with a single unified 'all the things' Group.
What I don't see is the all_sprites.update() call that makes the whole thing work, though I do see player.update(). PyGame groups are designed to let you call group.update() in place of a for x in y call such as:
for sprite in my_sprites:
sprite.update()
EDIT: Not seeing your images in the right place? If they're being set to the upper-left corner, this is usually because nothing is setting the surface of the drawn image to appear where you want it to.
One thing I've found handy about PyGame's get_rect() call is that it allows you to pass arguments to set attributes of the rect you get back. For example, my sprite.draw(self) methods tend to look something like this:
def draw(self):
"""Sets the image's Rect to the middle of the sprite,
then blits to the screen.
"""
draw_rect = self.drawn_image.get_rect(center=self.rect.center)
SCREEN.blit(self.drawn_image, draw_rect)
This assumes that the sprite object's self.x and self.y are being updated correctly as well, but it's essentially the right thing to do; get the rect for the object you're drawing, set it to the right coordinates just as you do the actual sprite, then pass the image and the rect to SCREEN.blit().
I wanted to allow the Emacs cursor to move around freely outside of actual text (similar to virtualedit=all in Vim).
"Oh," I thought, "I'll just keep track of a virtual cursor and draw it to the screen myself."
But it turns out the actual native C drawing routines (such as draw_glyphs) seem to refer back to the buffer contents to decide what to draw (I could be wrong though).
My next idea was to make a giant overlay of all spaces so I'd have complete freedom where to put stuff. But an overlay only goes over ranges of actual text, so again, this does not seem to give me what I'm looking for.
Is this a reasonable goal without hacking the C code?
I believe the writeable area of a window is intrinsically limited to the buffer with which it is associated, i.e. you have to draw in an area where buffer content exists.
(One example of this limitation is the impossibility of drawing a vertical guide line in the 80th column to help the user identify long lines; currently the best possible implementation of such a feature is to highlight the "overflow" of each too-long line.)
You can do the same as what artist-mode does without adding spaces to the buffer:
when trying to place the cursor after the end of the line, just use an overlay with an after-string property which adds the spaces in the display without modifying the buffer.
Have a look at "artist-mode" (M-xartist-modeRET) - it allows you to draw in Emacs.
From the function documentation: "Artist lets you draw lines, squares, rectangles and poly-lines, ellipses and circles with your mouse and/or keyboard."
You can look at popup.el from the auto-complete package, which can pop up tooltips and menus and such at any position, including positions outside the contents of the buffer. Maybe that will show you how you can do it.
I am making a game in Cocos2d. I want there to be a dotted line that follows the user's finger. I want the line to be straight. The problem is, how do I check to see how many 'dots' will fit in the distance between the ball and where the user is touching? And make it follow in a STRAIGHT line between the ball's position and the finger's position? So here's a re-clarification:
The ball sits still on the left side of the screen, and is halfway up the screen. The user drags their finger, and a dotted line is drawn between the ball's position and the touch's position. I have a 'dot' image to be used, and I would like it to be used as the dots in the line. So it will have to recreate the sprite as many times as it will fit in the area between the two points. Please tell me if you want me to clarify further, Thanks!!
I would make a CCNode object called dottedLine or something.
The dot image would be a sprite that gets added as a child of the node (multiple times).
I would work out the path from ball to finger touch using Trigonometry/Pythagoras theorem.
For creating the line:
From point 0, the ball, i would add 15-20 pixels towards the touch point along the path, and place a dot, I'd repeat this until I reached the end of the line.
Every dot that was placed I would increment a counter, and set that number as the integer tag of that sprite for use in an update method.
Every time the cctouchesmoved method gets called, i'd call an update method on the dottedLine object.
This method would check the distance between the ball and touch point, divide it by the number of dots currently children of the object and remove or add any that are needed. Recreating the sprites every time you move your finger would be messy and wasteful, so reusing your dots and just setting them new positions as the path between ball and touch point changes would probably be best.
I'm not going to provide you code, i think i've explained more than enough of the working out for you to do some googling and work this out.