small medium large xlarge

Generic-user-small
22 Aug 2017, 15:07
Abdulrhman (3 posts)

Greetings Mr. Mark,

You mentioned in page 47 that the method offset_x can be done easily by us. If you have the time, can you please explain to me the method definition?

I only know how to find offset between two object, and not one object like our ship. Using the method will give me the result I want, but I want to understand the logic behind it.

Regards,

Mark_pragsmall
22 Aug 2017, 16:16
Mark Sobkowicz (19 posts)

Take a look at the documentation for Gosu. http://www.rubydoc.info/github/gosu/gosu/Gosu. The offset_x method just does the trigonometry calculation for you. x = r * cos(Theta).

Generic-user-small
23 Aug 2017, 07:03
Abdulrhman (3 posts)

Thanks sir for the information.

I tried to implement it this way now and I just can’t seem to know the mistake I did. The behavior is different than using offset_x method.

offestX = ACCELERATION * Math.cos(@angle); since, we are passing constant value ACCELERATION, I thought that will be our r.

this is my current code.

offestX = ACCELERATION * Math.cos(@angle);
@velocityX += offestX # Gosu.offset_x(@angle,ACCELERATION);
@velocityY += Gosu.offset_y(@angle,ACCELERATION);

Can you please, point me to where I was wrong.

Mark_pragsmall
23 Aug 2017, 12:30
Mark Sobkowicz (19 posts)

I think probably the problem is that offset_x( ) uses degrees, and Math.cos( ) uses radians. Try converting your angle to radians. For instance, use Math.cos(@angle * (Math.PI / 180)) instead of Math.cos(@angle).

Generic-user-small
23 Aug 2017, 15:41
Abdulrhman (3 posts)

Thanks for the information.

I tried it and it didn’t work at first, but I noticed that somehow it adds 90 degree to the movement. So, I tried to subtract 90 from the angle and it worked. I’m still not sure why, but at least I learned how to read degree from game view.

This is the final code, in case anyone wants it in the future.

offsetX = ACCELERATION * Math.cos((@angle-90) * (Math::PI / 180))
offsetY = ACCELERATION * Math.sin((@angle-90) * (Math::PI / 180))

Thanks for the help sir, and for this awesome book. I will use it as my reference When I want to learn new languages in the future. The learning curve is superb.

Regards,

You must be logged in to comment