Question 2D Car game: Rotate image, how do i get the corners?

nik_ahp

New member
Joined
Jan 16, 2010
Messages
2
Programming Experience
3-5
Hey, I have created a little "top-down" 2D game where you drive around a
little car witch i wrote just to try out the System.Drawing.Graphics stuff in
vb.net, such as rotate image, transparancy and other stuff.

Things are working pretty fine, however I have bumped into a little math
problem.

I have a picture of a car witch are rotated when you stear the car, and the
picture is beeing rotated relative to the cente of the back tire axel of the car
as shown below:

car.jpg


What I'm trying to do now is to calculate where the corners of the car are
when I rotate it relativly to the top left of the form. I need this information to
build a hitbox for the car witch I will use for a collision detection routine later
on.

I know the car's width and height, it's X and Y position relativly to the top left
of the corner and direction angle.

Anyone able to figure out how to calculate this?
 
Last edited:
GDI+ will do the calculation for you, assuming you have used Graphics methods such as TranslateTransform and RotateTransform to do the rotation. If p1, p2, p3 and p4 are the coordinates of the car's corners before transformation, you use TransformPoints after the transformations:

VB.NET:
'Put the points in an array:
 Dim pts() As Point = {p1, p2, p3, p4}
'Translate the "world" (untransformed image) coordinates _ 
'into "device" (client area) coordinates:
e.Graphics.TransformPoints _ 
(Drawing2D.CoordinateSpace.Device, Drawing2D.CoordinateSpace.World, pts)

The array pts will then contain the positions where the corners have ended up on the screen relative to the control's origin.

bye, Vic
 
e.Graphics.TransformPoints (Drawing2D.CoordinateSpace.Device, Drawing2D.CoordinateSpace.World, pts)

Took awhile before I understood how this was working. When I finaly did, I had
to rewrite much of the code to be able to use this, but now its finaly working as
I want.

Now I just need to see if I can use this information to do some collision
detection, after some googeling it seem to be a quite complicated task, but I'm
up for a challange. ;) Thanks for the help tough.
 
Back
Top