forums

TNT Basic Forums > Programming Q&A
Hardware Graphics Line is
< Last Thread     Next Thread >
Author
Thread        Post A Reply

12-23-2007 17:52

Posted by:
m15399

Find more posts by m15399

I'm just getting into programming, so please forgive any stupidity that follows.
I'm trying to write a program that will move a character to where you click (like most 2d PC RPGs).

I've written a program that does the following:
-When the mouse is clicked, the program finds the midpoint between the characters location and the location where the mouse was clicked.
-The character moves to this midpoint.
-The character moves to the point where the mouse was clicked. This is to make the character take a more diagonal route to the destination (doesn't matter if you don't understand)

But when I run the program and click at the destination, the character (just a line at this point, no sprites) will sometimes move along a jaggy path like this:

This only happens when the character is moving horizontally, and does not happen all the time, as you can see on the bottom left of the image.
Everything is in integers, so I don't think it has anything to do with averaging.

Here is my code
If you decide to run it, make sure to change your screen resolution.

This post is probably way too long for any of you to bother to read, but if you can help me I would deeply appreciate it.

12-23-2007 23:34

Posted by:
m15399

Find more posts by m15399

Sorry, the threat title is too long!
Also, I meant to say that you should change the screen size in the program, not your computer's screen resolution.

12-23-2007 23:55

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

what doesn't work in your code is that you consider X and Y speeds independently, and this works only when your character walks horizontally or vertically.

Here is a very implified version :


window mode 800,600
set pen color white
float locX=mouse x,locY=mouse y
int mouseX,mouseY,charSpeed=2
float speedX, speedY

load images 128

sprite 1,locX,locY,0
draw frame

repeat
wait mouse click
mouseX=mouse x
mouseY=mouse y

' find path length using pythagoras' formulae
int path_length = sqrt( (mouseX-locX)*(mouseX-locX) + (mouseY-locY)*(mouseY-locY) )

' find in how many steps to move if we want to stay at the same speed
int steps = path_length / charSpeed

' find X and y speed
speedX = abs(mouseX-locX) / steps
speedY = abs(mouseY-locY) / steps

repeat
repeat
if mouse button then break 2
if mouseX>locX then locX=locX+speedX
if mouseY>locY then locY=locY+speedY
if mouseX<locX then locX=locX-speedX
if mouseY<locY then locY=locY-speedY
sprite 1,locX,locY,0
draw frame
until mouseX>locX-charSpeed and mouseY>locY-charSpeed and mouseX<locX+charSpeed and mouseY<locY+charSpeed
forever
forever


For a full-fledged approach, see the directory, there is a more complete example.

Another way to go is to use angles with sin and cos

12-24-2007 00:05

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I did a small mistake while rewriting part of your code, I left two repeat inside each other and that was not necessary (but it worked anyway)

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.