i need help in understanding rotation values in webots. How do I calculate and set them?
I want my robot to rotate in direction of the certain object.
For example, if the ball is rolling around the robot, the robot tries to get the position of the ball and rotate to it, so that the robot is always facing the ball.
Does anybody have an idea how can I do it?
My thoughts on coding it:
Get position of the ball
Get position of the robot
Calculate the angle between them
Rotate the robot by calculated angle
Thanks in advance!
You don't have to calculate the angle, it is enough to find the position of the ball in a 2D image plane. If the ball is left from the image center the robot should rotate left and if the ball is right from the image center the robot should rotate right.
You can find an example here:
https://github.com/lukicdarkoo/webots-example-visual-tracking
and you can see the result here:
https://lukicdarkoo.github.io/webots-example-visual-tracking/
I am currently working on a 2D platformer and the sprites that I have animate from the bottom left point of the animation and when I draw the animation using a x and y point it still animates from the bottom left, so when I draw the animation to the screen the sprite should get shorter but the sprites feet just lift up of the ground like this https://www.dropbox.com/s/ofeggmlcp4f6qsk/Animation_probs_video.mp4
I know the video is not high quality but so what.
His head should go up and down not his feet. If you guy's can help me I would be most greatful. I could also use a program that fixes that I have a Linux computer with a windows xp virtual box and I am using python 2.7 and pygame.
Thanks.
Assuming you are animating a series of rectangular sprites each being an instance of pygame.Surface, you will be adding the difference between the surface with the greatest height and the current sprite's surface to the y position every time you blit.
Find the height of the tallest sprite only once:
max_height = tallest_sprite.get_height()
Now while you are cycling through your sprints each frame with current_sprite:
screen.blit(current_sprite, (x, y+(max_height - current_sprite.get_height())
If framerate is an issue, you may want to calculate these differences beforehand and associate them with each sprite so you have one less get_height() call per frame.
I've just started learning some pygame (quite new to programming overall), and I have some very basic questions about how it works.
I haven't found a place yet that explains when I need to blit or not to include a certain surface on the screen. For example, when drawing a circle:
circle = pygame.draw.circle(screen, (0, 0, 0), (100, 100), 15, 1)
I don't need to do screen.blit(circle), but when displaying text:
text = font.render("TEXT", 1, (10, 10, 10))
textpos = text.get_rect()
textpos.centerx = screen.get_rect().centerx
screen.blit(text, textpos)
If I don't blit, the text won't appear.
To be honest, I really don't know what blitting is supposed to do, apart from "pasting" the desired surface onto the screen. I hope I have been clear enough.
The short answer
I haven't found a place yet that explains when I need to blit or not to include a certain surface on the screen.
Each operation will behave differently, and you'll need to read the documentation for the function you're working with.
The long answer
What Is Blitting?
First, you need to realize what blitting is doing. Your screen is just a collection of pixels, and blitting is doing a complete copy of one set of pixels onto another. For example, you can have a surface with an image that you loaded from the hard drive, and can display it multiple times on the screen in different positions by blitting that surface on top of the screen surface multiple times.
So, you often have code like this...
my_image = load_my_image()
screen.blit(my_image, position)
screen.blit(my_image, another_position)
In two lines of code, we copied a ton of pixels from the source surface (my_image) onto the screen by "blitting".
How do the pygame.draw.* functions blit?
Technically, the pygame.draw.* methods could have been written to do something similar. So, instead of your example...
pygame.draw.circle(screen, COLOR, POS, RADIUS, WIDTH)
...they COULD have had you do this...
circle_surface = pygame.draw.circle(COLOR, RADIUS, WIDTH)
screen.blit(circle_surface, POS)
If this were the case, you would get the same result. Internally, though, the pygame.draw.circle() method directly manipulates the surface you pass to it rather than create a new surface. This might have been chosen as the way to do things because they could have it run faster or with less memory than creating a new surface.
So which do I do?
So, to your question of "when to blit" and "when not to", basically, you need to read the documentation to see what the function actually does.
Here is the pygame.draw.circle() docs:
pygame.draw.circle():
draw a circle around a point
circle(Surface, color, pos, radius, width=0) -> Rect
Draws a circular shape on the Surface. The pos argument is the center of the circle, and radius is the size. The width argument is the thickness to draw the outer edge. If width is zero then the circle will be filled.
Note that it says that "draws a shape on the surface", so it has already done the pixel changes for you. Also, it doesn't return a surface (it returns a Rect, but that just tells you where the pixel changes were done).
Now let's look at the pygame.font.Font.render() documentation:
draw text on a new Surface
render(text, antialias, color, background=None) -> Surface
This creates a new Surface with the specified text rendered on it. Pygame provides no way to directly draw text on an existing Surface: instead you must use Font.render() to create an image (Surface) of the text, then blit this image onto another Surface.
...
As you can see, it specifically says that the text is drawn on a NEW Surface, which is created and returned to you. This surface is NOT your screen's surface (it can't be, you didn't even tell the render() function what your screen's surface is). That's a pretty good indication that you will need to actually blit this surface to the screen.
Blit means 'BL'ock 'I'mage 'T'ranfser
When you are displaying things on the screen you will, in some way, use screen because that's where you are putting it.
When you do:
pygame.draw.circle(screen, (0, 0, 0), (100, 100), 15, 1)
you are still using screen but you are just not blitting because pygame is drawing it for you.
And when you use text, pygame renders it into an image then you have to blit it.
So basically you blit images, but you can also have pygame draw them for you. But remember when you blit an image, say over a background, you need to loop it back and fourth; so that it blits the background, then the image, then the background etc...
You dont need to know much more than that, but you can read all about it here Pygame Blit
I hope this helped. Good Luck!
Imagine that you are a painter:
You have a canvas, and a brush.
Let's say that your main screen surface will be your canvas, and all the other surfaces, are "in your head" - you know how to draw them already.
When you call blit, you paint on top of the surface, covering any pixels that were overlapped. That is why you need to repaint the whole screen black so that you won't have any smudges on the painting while moving an object.
As Mark already said, you can draw a circle with a function, or first blit it to a new surface, and blit that on the screen surface.
If you have a more complicated surface - curves, text etc. you wouldn't need to have a surface for that, so you don't have to do any expensive calculations, just drawing. The setback is that your program takes up more memory, so you have to choose between those 2.
I'm making a simple game to get my knowledge of Python and Pygame going, but, since I haven't used rotation before, I am encountering a problem. Every time my rectangle rotates, it gets bigger and smaller, and my game needs a centered rotating object.
I have two solutions, maybe three. Will any of these work? If not, do you have a solution of your own?
Move the coods of my rectangle back and forth depending on the angle - This would be hard work.
Is there a way to blit an object by the middle of it, rather than the top left corner? This would be perfect
Use sprites??? I'm not sure if it would help at all, I haven't looked into or learnt anything about sprites at all yet.
It would clarify your question if you post some code. Not having seen your code, I suggest the following: draw the rectangle as usual onto a Surface and then rotate the Surface using pygame.transform.rotate
I'm working on some code involving use of the pygame library to draw a circle to a surface. The code to draw the circle looks something like the following:
surf = pygame.Surface((2000,2000))
pygame.draw.circle(surf,
pygame.Color("white"),
(1000,1000),
508,
50)
The problem is that the resulting circle has artifacts
Is there anyway to draw a circle like this without getting these artifacts?
What you see is most likely a problem with pygame.draw.circle implementation. Most likely pygame developer foolishly assumed that you can draw thick circle by drawing several circles in sequence, increasing radius by one for every circle. That's not going to work in practice and you'll get moire pattern like the one you see. Try increasing circle line thickness by 100 or 200 and see what happens. If it gets worse, then it is pygame.draw.circle's fault.
A solution would be to provide your own circle rendering routine. This can be certainly be done, but for optimum performance it makes sense to do it in C/C++ extension module for pygame OR using OpenGL (with or without fragment shaders).
To get a perfect circle for every scanline you'll have to calculate start and end of filled area for every scanline. It might be possible to do this in python with tolerable performance by using pygame.draw.line to draw individual scanlines.
This bug is mentioned in the comments for the circle and arc methods. There's one workaround in the comments of the draw.circle documentation. This is adapted from that workaround:
def draw_outlined_circle(surf, color, origin, radius, thickness):
width = radius * 2 + thickness * 2
background = (0, 0, 0, 0)
circle = pygame.Surface((width, width)).convert_alpha()
rect = circle.get_rect()
circle.fill(background)
pygame.draw.circle(circle, color, rect.center, radius)
pygame.draw.circle(circle, background, rect.center, radius - thickness)
surf.blit(circle, (origin[0] - (rect.w / 2), origin[1] - (rect.w / 2)))
This method uses an extra Surface and draws two circles: a smaller circle inside of a larger one to achieve the same effect. The smaller circle's fill color has an alpha of 0, so the full circle's center appears transparent.
EDIT: From the above comment, I see the behavior is mentioned in the pygame FAQ as well.
I fixed this issue, and it will be in the next release of pygame (1.9.4). How do you draw circles like that without artifacts? Use the newer version of pygame.
This might have happened with a problem on how pygame.draw.circle is implementing.
You can try the arcade library for better performance. Also, the arcade library is very easy to use while I find pygame quite complicated.