Trying to get model to follow mouse

All topics about ZGameEditor goes here.

Moderator: Moderators

daz
Posts: 8
Joined: Fri Feb 12, 2010 2:46 am

Trying to get model to follow mouse

Post by daz »

Basically, I'm trying to create a cursor model to follow the mouse pos.

I have
OnStart (Not code)
Curs: SpawnModel CursorModel

OnRender (& OnUpdate since OnRendering didn't work) (Code)
Curs.Position.X = App.MousePosition.X;
Curs.Position.Y = App.MousePosition.Y;

but the position of the model doesn't change :( What am I doing wrong?
User avatar
VilleK
Site Admin
Posts: 2280
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Post by VilleK »

Hi daz,

Try setting SpawnStyle to "Reference" on SpawnModel-component. When it is set to Clone (the default) it will spawn a copy of the model. The code in your script will still update the position of the original, which means you will see no effect.

Another approach is to move the expression to OnUpdate in your model instead. Then it will update the position of the current clone.

Let us know if you need more help!
daz
Posts: 8
Joined: Fri Feb 12, 2010 2:46 am

Post by daz »

After getting the MouseModelController working, I think it might provide an interesting smoothness.. but why I originally wanted to do it manually (and still might) is because this might not be fast enough for some shmuppers.

Currently what I guess I'd need help with is this whole odd coordinate system. 0,0 is the middle of the screen, and all in all it seems to be 3x3... and I only know that from outputting the cursor's position, which doesn't seem to manage to output a float? Surely when I move the cursor 16px higher (approx.) it wouldn't still have a Y value of 2, and yet it shows it as such. Does RenderText not render the full float?

Anyway.. I was trying to use the good ole formula atan2(y2-y1,x2-x1)*(180/pi) to get the model to point at the cursor. Now aside from the possible precision problems (unless it is precise and RenderText rounds), I'm not sure how to access other models from a model..
-> For instance. I have CursorModel and PlayerModel. In CursorModel's OnRender I have a RenderText (float): PlayerModel.Position.Y which always shows 0. This seems to tell me that I'm not accessing the other model's properties correctly, which I need to be able to do along with getting more precision and hoping the formula will work ;)

Anywho, this is (aside from the coordinate system oddity, which is centered and isn't pixel based), a pretty interesting game engine. I've been too lazy to really download the source and poke around, but I'd be surprised if you weren't using some assembler for optimization. Coding some basic GUI apps with Free Pascal myself, I know apps are quite often chubby.

I hope I can finish a game with this ;) I'm aiming for 50-100 levels with bosses and 5-10 unique level styles (based on number of levels / 10) to go along with it. But, I've mostly stuck to very small games so far, so I'm thinking I'm going to force myself to make public alpha releases quite often to keep myself motivated to finish.
User avatar
y offs et
Posts: 418
Joined: Wed Apr 22, 2009 4:26 pm
Location: BC, Canada

Post by y offs et »

The co-ords are x -1 to +1, Y -1 to +1

RenderText renders whole numbers, so make FloatMultiply 10.0

As to model to model, I suggest using a more global approach using app/states.AppState's and using ZExpressions from there.
User avatar
Kjell
Posts: 1891
Joined: Sat Feb 23, 2008 11:15 pm

Post by Kjell »

Hi daz,

The mouse coordinate system is actually quite convenient for most 3D games. And when you do need the traditional pixel-based system, it's just a matter of converting the values .. for example

Code: Select all

int X = (App.MousePosition.X+1)*ViewportWidth/2;
int Y = (1-App.MousePosition.Y)*ViewportHeight/2;
When it comes to doing your own mouse cursor, there are quite a number of ways to achieve this. Attached is a example that doesn't use a Model at all.

Welcome to the club and feel free to ask away ~

Kjell
Attachments
Cursor.zgeproj
(925 Bytes) Downloaded 606 times
daz
Posts: 8
Joined: Fri Feb 12, 2010 2:46 am

Post by daz »

y offs et wrote:The co-ords are x -1 to +1, Y -1 to +1

RenderText renders whole numbers, so make FloatMultiply 10.0

As to model to model, I suggest using a more global approach using app/states.AppState's and using ZExpressions from there.
Ok. I guess that seemed to give RenderText a more accurate depiction of the actual values.

Anyway, I went with the global approach and this seems to have worked as far as retrieving a position.

And I got it pointing at the mouse just fine now! :D
User avatar
diki
Posts: 140
Joined: Thu Sep 11, 2008 7:53 pm
Location: GMT+1
Contact:

Post by diki »

y offs et wrote:The co-ords are x -1 to +1, Y -1 to +1
take care that this depends on the Camera Settings. a camera with FOV 45 situated at position [0, 0, 1] and a square viewport (window) will look at the area between -1/+1 for x and y. when you put it further away on the z-axis or enlarge the FOV, the visible area will enlarge.
daz
Posts: 8
Joined: Fri Feb 12, 2010 2:46 am

Post by daz »

diki wrote:
y offs et wrote:The co-ords are x -1 to +1, Y -1 to +1
take care that this depends on the Camera Settings. a camera with FOV 45 situated at position [0, 0, 1] and a square viewport (window) will look at the area between -1/+1 for x and y. when you put it further away on the z-axis or enlarge the FOV, the visible area will enlarge.
I figured that. I haven't changed the camera from the default, and don't plan on changing it, so hopefully nothing will break during development ;)

Another question: is there a built in friction system? Or will I have to roll my own via velocity settings?
User avatar
diki
Posts: 140
Joined: Thu Sep 11, 2008 7:53 pm
Location: GMT+1
Contact:

Post by diki »

physics are not yet a part of zge, so i'm afraid you'll have to script your own.
daz
Posts: 8
Joined: Fri Feb 12, 2010 2:46 am

Post by daz »

diki wrote:physics are not yet a part of zge, so i'm afraid you'll have to script your own.
Unfortunately, I believe I'd need a KeyUp component for that...

(Now, if I were really persistent, I'd setup a variable that's set on press, with a timer constantly overriding that variable to something else. This way, hopefully I could create a hacked up KeyUp test of my own).

Thoughts?
User avatar
y offs et
Posts: 418
Joined: Wed Apr 22, 2009 4:26 pm
Location: BC, Canada

Post by y offs et »

Umbrella
Posts: 4
Joined: Tue Feb 16, 2010 9:46 pm

Post by Umbrella »

Hello everyone,
I am new to this forum as well and strangely enough I also have a question about the same problem daz had, however I am neither as good as programming nor as well at maths as he is.
Before I visited this forum I also went for the manual approach which didn't work out so I tried working with turshijas center mouse thing and found it most useful, but the crosshair model kept causing problems and while trying to solve it I turned the entire project into a bloody mess, even two piles of mess (two different projects at different points of frustration).
To sum up my problem with the crosshair:
It is at fixed position, neither changing it's position nor it's rotation although it is supposed to be.
My "coding" has become more or less unreadable, but I'll torture you with an excerpt of it anyway^^

Code: Select all

CurrentModel.Position.X = PlayerModel.Position.X;
CurrentModel.Position.Y = PlayerModel.Position.Y;
CurrentModel.Position.Z = PlayerModel.Position.Z -2;
CurrentModel.Rotation = PlayerModel.Rotation;
//CurrentModel.Rotation = App.CameraRotation;
centerMouse();
I just messed with it as far as I could, hoping to make it work anyway, but after a while I realised there was no progress to be made that way anymore and I decided to make a fool of myself to get things going :D
User avatar
y offs et
Posts: 418
Joined: Wed Apr 22, 2009 4:26 pm
Location: BC, Canada

Post by y offs et »

Welcome Umbrella,

Well, I'll tell ya, the torture is -
1)"the same problem daz had" - which one?
2)" turshijas center mouse thing" - umm, post the link, please.
3)"My "coding" has become more or less unreadable," - it certainly is, taken out of context.

Just zip up your project file and attach it.
And again, welcome.
"great expectations"
User avatar
VilleK
Site Admin
Posts: 2280
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Post by VilleK »

Welcome to the forums Umbrella.

This could also be a problem with clones vs. references. I assume your code is in OnUpdate on the model you want to move. Then it is necessary to spawn PlayerModel using "Reference" instead of "Clone" (SpawnStyle to "Reference" on SpawnModel-component). Otherwise you will keep reading the stationary values of the original model and nothing seems to happen. I you can't get it working then post the whole project here so we can help you further.
Umbrella
Posts: 4
Joined: Tue Feb 16, 2010 9:46 pm

Post by Umbrella »

Hello,
thank you for answering that quickly and sorry for expressing myself not clear enough, but I'm new to ZGE and to this forum, so I hope you will be patient with me.

Basically, I tried to create a crosshair which followed the mouse position while keeping away from the player model all the time so that I could use the Steering Controller on the player model.
I started working with this file provided by turshija which was very helpful.
I'll enclose my own project file here.
Attachments
Approach2.zgeproj
(4.29 KiB) Downloaded 487 times
Post Reply